We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5628
    • 44 Posts
    Probably being a bit thick but is there a way to query/return the number of documents that a Ditto call will return without returning the actual documents or links to them?

    Cole
    • I don’t find any reference to Ditto ever returning the number of documents found. You may need to use a different snippet for that information.

      Wait a minute... if pagination is being used, there is a [+total+] placeholder. But how to use only that value without anything else may be another matter. Perhaps set its parameters to get all, but only display one, and have your template chunk just display [+total+]?
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 7231
        • 4,205 Posts
        All you want is the total of docs? You do not want to display any docs but just the total?

        If this is what you want I can think of a slick way to get this by using Ditto to generate the doc list of a given parent and then transform the results into an array and then use the count() function to do the math and return a number.

        This would be the snippet code:
        <?php
        // totalDocs
        $parent = isset($parent) ? $parent : $modx -> documentObject['parent'];
        $depth = isset($depth) ? $depth : "1";
        $showPublishedOnly = isset($showPublishedOnly) ? $showPublishedOnly : "1";
        $seeThruUnpub = isset($seeThruUnpub) ? $seeThruUnpub : "0";
        $showInMenuOnly = isset($showInMenuOnly) ? $showInMenuOnly : "1"; 
        
        // Use Ditto to generate document list
        $documents = $modx->runSnippet("Ditto", array(
        	"parents" => $parent,
        	"display" => "all",
        	"depth" => $depth,
        	"showPublishedOnly" => $showPublishedOnly,
        	"seeThruUnpub" => $seeThruUnpub,
        	"showInMenuOnly" => $showInMenuOnly,
        	"tpl" => "@CODE [+id+]," ));
        	
        $documents = explode( ',', $documents);
        
        $totalDocs = (count($documents) - 1);
        
        return $totalDocs;
        
        ?>


        And use it like this:
        This container has [!countDocs? &parent=`0` !] documents

        And the returned value will be the number of published not hidden documents in a given container.

        Note: this has limited testing, you may need to adjust/add parameters to get the desired results. I had to reduce the count by one in order to get the right number, not sure why, maybe somone else could enlighten us. grin

        Note2: To get the showPublishedOnly to work correctly you will need the latest Ditto beta release. Currently it will select only the published or unpublished docs, in the new relese it will also count the total of both pub and unpub docs.

        Anyway, hope this works out.

        Edit: you may be able to get the results using the getChildIds API function since it returns an array of ids for the children of a given doc. However it does not seem to have parameters for published or hidden documents. So IMO using Ditto will allow more flexibility.
        http://wiki.modxcms.com/index.php/API:getChildIds
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don&#39;t know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 10449
          • 956 Posts
          You can use the save parameter:

          snippet: dittoSaveVars:

          <?php
          if($dittoID) {
          	$resource = $modx->getPlaceholder($dittoID . "_ditto_resource");
          	$c = count($resource);
          	return($c);
          }
          ?>
          


          In the document:

          [!Ditto? &id=`dittoTest` &save=`3` &parents=`0` &depth=`5` &display=`100` &total=`100` &debug=`1`!]
          The above (invisible) Ditto call returned [!dittoSaveVars? &dittoID=`dittoTest`!] documents.
          



          Further reading:
          http://ditto.modxcms.com/files/snippet-ditto-php.html#save
          http://modxcms.com/forums/index.php?topic=20929.0
            • 5628
            • 44 Posts
            Thanks everyone for your suggestions - plenty of scope to achieve this.
            Had a go at creating a snippet which pulled out the TV am using to filter the Ditto call and used a mysql query then to cross-reference this with the relevant instances in the ’modx_site_tmplvar_contentvalues’ table of the database, although this seemed quite crude given the APIs available.
            Will work through your suggestions to see if can improve on what had worked on

            Thanks again

            Cole