Page 1 of 1

UDT Display Child Pages

Posted: Tue Jan 09, 2024 10:00 pm
by WDJames
Hello all,

I'm using pages to add Case Studies to our site. I've been trying to modify the below UDT so that the items include the page's images. I understand that I can just use the Navigator module (which is what I've done in the past) but the reason I want to keep it as a UDT is so that I can give it some parameters like "pagelimit" and "sortorder" ( I'm hoping that the final module tag would look something like {case_studies category="commercial" pagelimit="3" sortby="DESC"}).

Code: Select all


$pagelimit = isset($params['pagelimit']) ? $params['pagelimit'] : '';
$sortby = isset($params['sortby']) ? $params['sortby'] : '';

global $gCms;
$manager =& $gCms->GetHierarchyManager();
$thisPage = isset($params['category']) ? $params['category'] : '';
$currentNode = &$manager->sureGetNodeByAlias($thisPage);

$nodes = $currentNode->getChildren();

if ($currentNode->hasChildren()) {
  echo '<ul class="casestudies">';
  foreach ($nodes|@array_slice:0:'.$pagelimit.' as $node) {
     $content = $node->getContent();
     $url = $content->GetURL();
     
     $image = $content->GetImage(); /* THIS DOESN'T WORK */
  
    if ($url == "#") { /* section header has no link by default */
      $url = "index.php?page=".$content->Alias();
    }
    echo "<li><a href=\"".$url."\">".$content->MenuText()."</a> </li>";

  }
  echo "</ul>";
}
I've tried looking for the method to get the page image but I've had no luck. Anyone have any ideas?

Thanks,

James

Re: UDT Display Child Pages

Posted: Tue Jan 09, 2024 11:14 pm
by Jo Morg
I think you have a mix of issues there:
- it seems you are mixing PHP and Smarty syntax there;
- there is some legacy code that CMSMS doesn't support anymore;
- possibly some PHP typos or syntax errors;

I didn't test it but just corrected it to what it seemed a bit more logical and valid PHP code. Try it and give us some feedback:

Code: Select all

$pagelimit = isset($params['pagelimit']) ? $params['pagelimit'] : '';
$sortby = isset($params['sortby']) ? $params['sortby'] : '';

$manager = CMSMS()->GetHierarchyManager();
$thisPage = $params['category'] ?? '';
$currentNode = $manager->sureGetNodeByAlias($thisPage);

$nodes = $currentNode->getChildren();

if($currentNode->hasChildren())
{
  echo '<ul class="casestudies">';
  $tmp = array_slice($nodes, 0, $pagelimit);
  foreach($tmp  as $node)
  {
    $content = $node->getContent();
    $url     = $content->GetURL();
    
    $image = $content->GetImage();
    
    if('#' == $url)
    { /* section header has no link by default */
      $url = 'index.php?page=' . $content->Alias();
    }
    echo '<li><a href="' . $url . '">' . $content->MenuText() . '</a> </li>';
    
  }
  echo '</ul>';
}

Re: UDT Display Child Pages

Posted: Wed Jan 10, 2024 12:40 am
by WDJames
Hi Jo, thanks for the feedback - I thought I'd save some time by modifying an existing UDT rather than writing from scratch. Your corrections work and the code runs if I comment out the $image line, however if I leave it as it is, the $image = $content->GetImage(); still causes a Fatal error (because I made it up to show where I want to get the image):

<b>Fatal error</b>: Uncaught Error: Call to undefined method Content::GetImage() in ...

Is there a list of CMSMS methods that I can reference?

Thanks,

James

Re: UDT Display Child Pages

Posted: Wed Jan 10, 2024 9:21 am
by WDJames
I figured out how to get the image. I've replaced the code so instead of:

Code: Select all

$image = $content->GetImage();
I used

Code: Select all

$image = $content->GetPropertyValue('image');
Then I used an if statement to display a placeholder image if its blank.

Hope this helps someone else.

Thanks,

James

Re: UDT Display Child Pages

Posted: Wed Jan 10, 2024 1:40 pm
by Jo Morg
Glad you figured it out. Thanks for posting it back to the community. 8)