We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23334
    • 2 Posts
    MODX Revolution 2.1.3-pl (traditional)

    I have a related pages TV to display all children of a resources ultimate parent.
    The TV input option is
    @EVAL return $modx->runSnippet('RelatedPages.TV', array('id' => $modx->resource->get('id')));

    In the snippet, UltimateParent v1.3 always returns the id of the calling resource. I copied UltimateParent into the snippet and added some log messages to identify whats happening. Based upon the messages in the log, line 13 in the snippet (line 40 in UltimateParent) $pids = $modx->getParentIds($id) is returning an empty array. I am testing with Resource(76) in this directory structure:

    [tt]HOME(1)
    Dir(26)
    Resource(29)
    Dir(2)
    Resource(30)
    Resource(3)
    Dir(28)
    Resource(5)
    Resource(21)
    Resource(4)
    Resource(76)
    [/tt]
    This is the RelatedResources.TV snippet:
    <?php
    $modx->setLogLevel(modX::LOG_LEVEL_INFO);
    
    if (empty($id)) { return "Resource ID not provided, cannot identify parents"; }
    
    $resid = $id;
    $parent = 0;
    $top = 0;
    $topLevel = 0;
    $output = "";
    
    $modx->log(modX::LOG_LEVEL_INFO, 'RelatedPages.TV resource='.$resid);
    
    if ($id && $id != $top) {
      $pid = $id;
      [b]$pids = $modx->getParentIds($id);[/b]
      $modx->log(modX::LOG_LEVEL_INFO, 'RelatedPages.TV Number of Parents='.count($pids));
      foreach ($pids as $key => $value) {
        $modx->log(modX::LOG_LEVEL_INFO, 'RelatedPages.TV parentID'.$key.'='.$value);
      }
      if (!$topLevel || count($pids) >= $topLevel) {
        while ($parentIds = $modx->getParentIds($id, 1)) {
          $pid = array_pop($parentIds);
          if ($pid == $top) {
            break;
          }
          $modx->log(modX::LOG_LEVEL_INFO, 'RelatedPages.TV current parentID='.$pid);
          $id = $pid;
          $parentIds = $modx->getParentIds($id);
          if ($topLevel && count($parentIds) < $topLevel) {
            break;
          }
        }
      }
    }
    
    $parent = $id;
    
    $modx->log(modX::LOG_LEVEL_INFO, 'RelatedPages.TV parent='.$parent);
    
    $modx->runSnippet('getResources',array(
              'parents' => $parent, 
                'limit' => 0, 
       'hideContainers' => 1, 
                  'tpl' => "RelatedPages.TVlist.Tpl", 
      'outputSeparator' => "||",
        'toPlaceholder' => "reslist" 
    ));
    $output .= $modx->getPlaceholder('reslist');
    $modx->log(modX::LOG_LEVEL_INFO, 'RelatedPages.TV output='.$output);
    			   
    return $output;
    

    And the resulting Log entries:

    [2011-08-18 12:47:42] (INFO @ /manager/index.php) RelatedPages.TV resource=76
    [2011-08-18 12:47:42] (INFO @ /manager/index.php) RelatedPages.TV Number of Parents=0
    [2011-08-18 12:47:42] (INFO @ /manager/index.php) RelatedPages.TV parent=76
    [2011-08-18 12:47:42] (INFO @ /manager/index.php) RelatedPages.TV output=

    Am I misunderstanding the TV working environment? I assume that $modx->getParentIds(76) will return
    array ( 0 => 28, 1 => 26, 2 => 0, )

    Can you help me get this working?
      • 22303 MODX Staff
      • 10,725 Posts
      getParentIds() only returns the parents of Resources in the current Context. When editing in the manager, you are in the mgr context which has no map of Resources. You need to add additional parameters to the method call to specify a Context to get the parents from, e.g.
      <?php
      $pids = $modx->getParentIds($id, 10, array('context' => 'web'));
      
        • 23334
        • 2 Posts
        Thanks Jason, this works perfectly.