We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17412
    • 270 Posts
    I have a TV called technology-category which uses checkbox and a set of values to choose from such as:

    enterprise-linux==50||community-linux==51||non-linux-os==52

    The numbers relate to document ID’s.

    What I need to do is
    (1) Split the [*technology-category*] data into an array of ID’s (Raw data looks like this: 505152)
    (2) Output a link to each of those pages
    (3) Grab the menu title of each page to use as the anchor text

    IE:
    <a href="[~50~]">Menu title of item 50</a>


    From what I can see, this is Modx speak for making URL:
    $url=$modx->makeUrl(intval($id));


    And for getting a value of template variable:

    $document_tvs=$modx->getTemplateVarOutput(true, $id);
    $templatevar_output=$document_tvs['template_variable_name'];


    Now, how does all this get put together :0 Any help gratefully received.

    I guess also the GetField snippet can be used to grab the menu title ?
    <a href="[~50~]">[[GetField?docid=`50` &field=`menutitle`]]</a>


    Thanks.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Give the TV an output widget of "delimited list", with a comma or whatever you like as a delimiter. Then you can use a snippet to get the value (the delimited list), explode() it, and do whatever you want with each item.
        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
        • 20413
        • 2,877 Posts
          @hawproductions | http://mrhaw.com/

          Infograph: MODX Advanced Install in 7 steps:
          http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

          Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
          http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
          • 17412
          • 270 Posts
          Thanks guys. So I think I’ve cracked it (it works though there may be a better way to go about it of course)

          Snippet call:

          [[MakeLinks? &docids=`[*technology-category*]`]]


          Snippet MakeLinks code:

          <?php
          $thelinks = explode(',', $docids);
          reset($thelinks);
          foreach ($thelinks as $linkid) {
          $document_tvs=$modx->getTemplateVarOutput(true, $linkid);
          $templatevar_output=$document_tvs['menutitle'];
          echo '<a href="[~' . $linkid . '~]">' . $templatevar_output . '</a>';
          }
          ?>
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            $document_tvs=$modx->getTemplateVarOutput(true, $linkid);
            $templatevar_output=$document_tvs['menutitle'];
            

            is not quite right; menutitle isn’t a template variable. You should use the getPageInfo() API function:
            $array = $modx->getPageInfo($linkid,1,'pagetitle'); 
            $pagetitle = $array['pagetitle']; 

            Or you could use getDocument.

            http://wiki.modxcms.com/index.php/API:getPageInfo
            http://wiki.modxcms.com/index.php/API:getDocument
              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
              • 17412
              • 270 Posts
              Fantastic thanks. Updated snippet to:

              <?php
              $thelinks = explode(',', $docids);
              reset($thelinks);
              echo '<ul>';
                      foreach ($thelinks as $linkid) 
                      {
                      $array = $modx->getPageInfo($linkid,1,'menutitle'); 
                      $menutitle = $array['menutitle']; 
              	echo '<li><a href="[~' . $linkid . '~]">' . $menutitle . '</a></li>';
                      }
              echo '</ul>';
              ?>
                • 20413
                • 2,877 Posts
                Thank you for sharing that code! smiley
                  @hawproductions | http://mrhaw.com/

                  Infograph: MODX Advanced Install in 7 steps:
                  http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                  Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                  http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                  • 17412
                  • 270 Posts
                  Pleasure smiley

                  In a similar vein (well same part of project anyway),

                  I think I need to write a snippet that can target a section by id and..

                  (1) Can then output the menutitle and id for Tier 2 documents (eg: operating systems==2||email solutions==4||middleware solutions==8)
                  (2) Can output menutitle and id for Tier 3 documents (eg: enterprise linux==50||community linux==51||non linux os==52)

                  Tier 1: Software index
                  Tier 2: Software category
                  Tier 3: Software type

                  The snippet would be run inside a template variable (i.e. @EVAL return $modx->runSnippet(’MakeOptions’); ) and output the menu titles as checkbox options to tick in documents that have the TV assigned.

                  Would also need ability to exclude documents by ID (i.e. exclude document id 50 and all it’s children).

                  There is a snippet out there that is supposed to do some of this (ddTree) though alas it’s not working (with Evo 1.0.3).

                  Any pointers gratefully received as always.



                  ddTree:

                  <?php
                  /*
                  #   Name:    ddTree (DropDown Tree)
                  #   Version: 1.0.2
                  #            Bug fix by enidan: make depth parameter really optional, and also
                  #            make doc parameter become optional (defaults to 0)
                  #   Author:  Urique Dertlian ([email protected])
                  #   Date:    Feb 28, 2008
                  #   Using:   Create TV of kind "DropDown List Menu", feel elements field with string:
                               @EVAL return $modx->runSnippet("ddTree",array('doc'=>0,'depth'=>0));
                               
                               Here 'doc' is identifier of parent document, children of which will be displayed in DropDown menu.
                               Optional parameter 'depth' is used to limit depth of tree, for example, use 0 (zero) - if you want to display only first level of children.
                               If depth parametr is not set, ddTree builds up to 100 levels.
                  */
                  
                  $doc = isset($doc) ? $doc : 0;
                  $depth = isset($depth) ? $depth: 100;
                  if(!function_exists("getItems"))
                  {
                   function getItems($p=0, $d=100, $l=0)
                   {
                    global $modx,$modx_charset;
                    ($modx_charset=='UTF-8') ? $nbsp=chr(0xC2).chr(0xA0) : $nbsp=chr(0xA0);
                    $c=$modx->getDocumentChildren($p);
                    foreach($c as $k)
                    {
                     $out.=str_repeat($nbsp,$l*5).$k['pagetitle']."==".$k['id']."||";
                     if($l<$d) $out.=getItems($k['id'],$d,$l+1);
                    }
                    return $out;
                   }
                  }
                  return getItems($doc,$depth);
                  ?>
                  
                    • 17412
                    • 270 Posts
                    Ok if anyone’s interested in how to do this. Here’s the snippet:

                    Snippet: docOptionifier

                    <?php
                    //specify page id you want to get data for
                    $root = $root;
                    $depth = $depth;
                    $exclude = preg_split('~\s*,\s*~', $exclude);
                    $content = array();
                    
                    foreach ($modx->getChildIds($root,$depth) as $childID)
                    {
                    	if (in_array($childID, $exclude))
                    	{
                    		continue;
                    	}
                    	
                    	$child = $modx->getTemplateVarOutput(array('menutitle','parent','pagetitle'), $childID);
                    	
                    	if ((empty($child['menutitle']) && empty($child['pagetitle'])) || empty($child['parent']))
                    	{
                    		continue;
                    	}
                    	
                    	if ($depth > 1)
                    	{
                    		if ($child['parent'] == $root || in_array($child['parent'], $exclude))
                    		{
                    			continue;
                    		}
                    		
                    		$parent = $modx->getTemplateVarOutput(array('menutitle'), $child['parent']);
                    	}
                    	
                    	$content[] = $child['pagetitle'] . '==' . $childID;
                    }
                    
                    return implode('||',$content);
                    ?>


                    Create your template variables. In my case:

                    Template Variable: technology-area

                    Input Option Values:
                    @EVAL return $modx->runSnippet('docOptionifier', array('root' => 15, 'depth' => 1, 'exclude' => 53));


                    Template Variable: technology-category

                    Input Option Values:
                    @EVAL return $modx->runSnippet('docOptionifier', array('root' => 15, 'depth' => 2, 'exclude' => 53, 'option' => 'id'));

                      • 26931
                      • 2,314 Posts
                      hey lokust, thanks for sharing! smiley