We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I think the IF would look something like this:

    $projectDetail = $page->getTVValue('tv.ProjectDetail');
    if (!empty($projectDetail)) {
        $output .= '<p>' . $projectDetail . '</p>';
    }


    For calling Gallery, you'd do something like this:

    $fields = array(
       $album => $page->getTVValue('tv.ActivityAlbumTitle'),
       $toPlaceholder = 'gallery',
       $limit = 1,
       $tag = 'lead',
       /* etc. */
    );
    
    $output .= $modx->runSnippet('Gallery', $fields);
      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 36924 ☆ A M B ☆
      • 298 Posts
      Thank you for your patience. I never expected achieving this will be this cumbersome.

      Some things are not happening. Depending on your sortby (ASC/DESC) the TV values of the first output is repeated across all Projects under the same category. Some TVs are not been displayed at all even though they are not empty, especially ProjectDetail and ProjectDetailTwo.

      The Gallery snippet call is not displaying anything.

      Here is the complete code:

      $tvId = 21; /* change this to the ID of the category TV */
       
      function getDocs($category, $tvId) {
          global $modx;
          $tvrs = $modx->getCollection('modTemplateVarResource', array(
              'tmplvarid' => $tvId,
              'value'     => $category
          ));
          $docs = array();
          foreach ($tvrs as $tvr) {
              $docs[] = $tvr->get('contentid');
          }
       
          $c = $modx->newQuery('modResource');
          $c->where(
            array('id:in' => $docs)
          );
          $c->sortby('menuindex', 'ASC'); /* I changed this from pagetitle */
       
          $resources = $modx->getCollection('modResource', $c);
          $page = $modx->getObject('modResource', $c); /* I added to be able to use $page to get all the TVs as required */
      
          $output = '<h2><b>' . $category . '</b></h2>';
      
          foreach ($resources as $resource) {
      
      $Intro = $resource->get('introtext');
      
      if (!empty($Intro)) {/* trying to ensure no paragraph tag output if introtext is empy */
          $Intro = '<p><strong>Overview</strong><br /> ' . $resource->get('introtext') . '</p>';
      }
      
      /*start formatting output */
      $output .= '<div class="sbu-details">
      <h2><strong>' . $resource->get('pagetitle') . '</strong></h2>
      <ul>
      <li><strong>Project:</strong> ' . $page->getTVValue('ProjectTitle') . '</li>
      <li><strong>Location: </strong> ' . $page->getTVValue('ProjectLocation') . '</li>
      <li><strong>Client:</strong> ' .  $page->getTVValue('ProjectClient') . '</li>
      <li><strong>Terrain:</strong> ' . $page->getTVValue('ProjectTerrain') . '</li>
      </ul>' . $Intro; 
      
      $projectDetail = $page->getTVValue('ProjectDetail');
      if (!empty($projectDetail)) {    /* trying to ensure no paragraph tag output if ProjectDetail is empy */
          $output .= '<p>' . $projectDetail . '</p>';
      }
      
      $projectDetailTwo = $page->getTVValue('ProjectDetailTwo');
      if (!empty($projectDetailTwo)) {  /* trying to ensure no paragraph tag output if ProjectDetailTwo is empy */
          $output .= '<p>' . $ProjectDetailTwo. '</p>';
      }
      
      $output .= '<p  class="galleryIMGs">';
      
      $fields = array(    /* to hold Gallery snippet call */
         $album => $page->getTVValue('ActivityAlbumTitle'),
         $toPlaceholder = 'gallery',
         $limit = 1,
         $tag = 'lead',
         $thumbTpl = 'nest_galItemLink',
         $linkToImage = 1
      );
      
      $fields2 = array(     /* to hold Gallery snippet call */
         $album => $page->getTVValue('ActivityAlbumTitle'),
         $toPlaceholder = 'gallery2',
         $tag = 'follow',
         $thumbTpl = 'nest_galItemFollowLink',
         $linkToImage = 1
      );
       
      $output .= $modx->runSnippet('Gallery', $fields);   /* to output Gallery snippet call */
      $output .= $modx->runSnippet('Gallery', $fields2);   /* to output Gallery snippet call */
      
      $output .= '</p></div>';
      
      /*end formatting output */
      
              /* Use this instead if you want links to the resources:
              $url = $modx->makeUrl($doc->get('id'), "", "", "full");
              $output .= '<li><a href="' . $url . '">' .  $doc->get('pagetitle'). '</a></li>';
              */
          }
       
          return $output;
      }
       
      /* Get all possible categories */
      $c = $modx->newQuery('modTemplateVarResource');
      $c->distinct();
      $c->where(array('tmplvarid' => $tvId));
       
      $tvrs = $modx->getCollection('modTemplateVarResource', $c);
       
      $output = '';
      /* Get the resources in each category */
      $usedCategories = array();
      foreach ($tvrs as $tvr) {
          $category = $tvr->get('value');
          if (! in_array($category, $usedCategories)) {
              $usedCategories[] = $category;
              $output .= getDocs($category, $tvId);
             }
          }
       
      return $output;
        • 3749
        • 24,544 Posts
        You shouldn't need the $page variable. It only points to one resource so it will always result in TV values for that resource (hence the duplication). Just delete the getObject() call that sets the $page variable, then change all instances of $page to $resource (which will always be the resource being processed) and you should be in business.

          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 36924 ☆ A M B ☆
          • 298 Posts
          I see. But the Galley snippet call doesn't output anything either.
            • 3749
            • 24,544 Posts
            Does the regular Gallery tag output anything?

            Setting the $fields array to the properties in the tag and then calling runSnippet('Gallery', $fields) is exactly the same as using the tag.

            Try hard-coding the property values in the $fields array and see if that makes a difference. If it does, replace them one-by-one with the dynamic version and see where it breaks.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 36924 ☆ A M B ☆
              • 298 Posts
              Thank you. Replacing $page with $resource across all TVs did the magic.

              Thought there is still not output with the Gallery snippet.

              This was the Gallery call I used previously and works as desired:
              [[!Gallery? &album=`[[+tv.ActivityAlbumTitle]]` &toPlaceholder=`gallery`  &limit=`1` &tag=`lead` &thumbTpl=`nest_galItemLink` &linkToImage=`1`]]
              [[!Gallery? &album=`[[+tv.ActivityAlbumTitle]]` &toPlaceholder=`gallery2`  &tag=`follow` &thumbTpl=`nest_galItemFollowLink` &linkToImage=`1`]]
              
              [[+gallery]]
              [[+gallery2]]
              


              I guess by hardcoding you meant this:
              $output .= $modx->runSnippet('Gallery', array(    
                 $album => $resource->getTVValue('ActivityAlbumTitle'),
                 $toPlaceholder = 'gallery',
                 $limit = 1,
                 $tag = 'lead',
                 $thumbTpl = 'nest_galItemLink',
                 $linkToImage = 1
              ));
              


              Still no output. 'nest_galItemLink' Tpl is suppose produce a link to Project pictures.
                • 3749
                • 24,544 Posts
                Wrong token for the array members, except the first one (=> not = );


                $output .= $modx->runSnippet('Gallery', array(   
                   $album => $resource->getTVValue('ActivityAlbumTitle'),
                   $toPlaceholder => 'gallery',
                   $limit => 1,
                   $tag => 'lead',
                   $thumbTpl => 'nest_galItemLink',
                   $linkToImage => 1,
                ));


                I make still make this mistake occasionally. It's = when creating the array this way:

                $a = array();
                $a['limit'] =  1;
                $a['tag'] = 'lead';
                


                but it's => when doing it as you are.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 36924 ☆ A M B ☆
                  • 298 Posts
                  Yes I tried => also before my last post. No joy.

                  I just did a google search and seems runSnippet might be buggy:
                  http://stackoverflow.com/questions/21155209/modxrunsnippet-method-sometimes-works-sometimes-not [ed. note: ojchris last edited this post 12 years, 3 months ago.]
                    • 3749
                    • 24,544 Posts
                    The OP in that link is running the code outside of MODX. You're not doing that are you?

                    If so, see this: http://bobsguides.com/blog.html/2013/07/13/using-modx-outside-of-modx/
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 36924 ☆ A M B ☆
                      • 298 Posts
                      No I'm not. Everything I'm doing is within MODx.

                      Here is the content of the 'nest_galItemLink' chunk/Tpl:
                      <a class="ActivityAnchor [[+album]] leadlink" href="[[+linkToImage:if=`[[+linkToImage]]`:is=`1`:then=`[[+image_absolute]]`:else=`[[~[[*id]]?
                                  &[[+imageGetParam]]=`[[+id]]`
                                  &[[+albumRequestVar]]=`[[+album]]`
                                  &[[+tagRequestVar]]=`[[+tag]]` ]]`]]" title="[[+description]]" [[+link_attributes]]>
                      View Project Gallery
                      </a>


                      Is there any thing here that could prevent the runSnippet from working?