We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16182
    • 56 Posts
    I’d like to use the ditto filter to filter out documents that have a certain template variable associated to them vs. documents that don’t have this variable associated. I don’t care for the content of that variable - it could even be empty.

    Looking at the code in [tt]filter.class.inc.php[/tt] I understand that comparison mode 1 not only checks for [tt]!=[/tt] but also for [tt]!isset()[/tt], which means I can filter out documents, that don’t have a certain template variable set by using:
    [!Ditto? ... &filter=`my_TV,,1`!]
    


    However, how can I exclude all those documents, that HAVE the [tt]my_TV[/tt] set (empty or not)?
      • 16182
      • 56 Posts
      I suggest to add a new comparison mode [tt]12[/tt] to ditto to make this job simple and consistent with the possibilities of comparison type [tt]1[/tt].

      Mode [tt]12[/tt] would check if a given key exists. That only makes sense for template variables as they don’t exist for every document but only for those to which template they’re associated. A use case would be to filter for multilingual vs. monolingual documents managed by YAMS as all the multilingual documents come with TVs like [tt]pagetitle_(yams_id)[/tt]:

      [!Ditto? &language=`(yams_mname)` &parents=`1` &tpl=`UrlsetURL` &depth=`5` &dateSource=`editedon`
               &dateFormat=`%Y-%m-%d` &display=`all` &showInMenuOnly=`0` &seeThroughUnpub=`1`
               &filter=`type,reference,2|searchable,1,1|pagetitle_(yams_id),,12`!]


      This would give me a list of the monolingual documents using template [tt]UrlsetURL[/tt].

      Find the hacked file attached, feel free to use and if it makes sense feel free to include in some future ditto release.
        • 16182
        • 56 Posts
        sorry, forget the attachment of the hack
          • 1122
          • 209 Posts
          The fact whether specific variable is attached to document or not is unambiguously determined by a template that the document is based on. So, instead of introducing new comparison mode, why not to filter documents by a template (that the TV is assigned to)?
          &filter=`...|template,<template-id>,1` /* get docs with specific TV */
          &filter=`...|template,<template-id>,2` /* throw out ... */
          
            • 16182
            • 56 Posts
            alik,

            thanks for the reply. Unfortunately, this is rather a workaround. It requires that I update all my filters every time I change the template setup.

            To give you an example: In YAMS you can configure certain templates to be multilingual. YAMS then creates specific template variables to hold the multilingual content and automatically attaches them to the respective templates. When you change that setup, template variables get detached from templates or attached to new templates. This should automatically work with the Ditto filters which can test for monolingual vs. multilingual. Otherwise, I would have to manually find all the filters and update them every time I change a template allocation.

            I guess the whole purpose of the filter is to programmatically support the content developer so I rather stick to my hack wink
              • 1122
              • 209 Posts
              Hi,
              I replied because I regard hacking the core as a last resort. The most significant disadvantage of the hack is the fact that it is not able to survive either MODx or Ditto upgrade (unless it will be included into further distribution).
              Filtering by TV assignment is not very obvious task, but with a little effort it is possible to make a solution workable with both previous and further releases of MODx/Ditto. Here is a proposal of such solution -- tvFilter extender written within framework given by Ditto itself. Extract file to "/assets/snippets/ditto/extenders" folder and run your Ditto as
              [[Ditto? ... &extenders=`tvFilter` &tvFilterBy=`tvname` &tvFilterMode=`1` ... ]]
              [[Ditto? ... &extenders=`tvFilter` &tvFilterBy=`tvname` &tvFilterMode=`0` ... ]]
              

              where filter’s mode=1 excludes documents with tvname assigned while mode=0 excludes documents that have not tvname assigned.
                • 16182
                • 56 Posts
                Alik,

                thanks a lot! I fully agree regarding the "survival" of hacks. They’re only good for oneself (carry forward manually if needed) and for the community (integrate into core). I’d love to know how to make it part of ditto - but I guess that’s up to the original author.

                I’ll try your suggestion! Thanks again.