Page List Filter by Attribute

Permalink
I put three different custom attributes to aprox 250 pages.

Now I would like to display three different page-lists on three different pages.
One should display all pages where the attribute 1 ist selected, one for the pages with the attibute 2 and one for the pages with the attibute 3.

If there are two ore all attributes selected in a page it should be displayed in two lists.

Unfortunately all depending forum-blogs are pretty old (2009) and the suggestions are related to an old version of C5 I think.

Can anybody help me please?

MathiasB
 
drbiskit replied on at Permalink Reply 1 Attachment
drbiskit
Hi - You can just do something like this (Page list file also attached):

<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$th = Loader::helper('text');
?>
<ul>
   <?php  foreach ($pages as $page):
      // Prepare data for each page being listed...
      $title = $th->entities($page->getCollectionName());
      $blue = $page->getAttribute('page_blue');
      $red = $page->getAttribute('page_red');
      $green = $page->getAttribute('page_green');
    ?>
      <? if($page->getCollectionAttributeValue('page_blue')){ ?> 
         <li>
            <h2><?php echo $title ?></h2>


I haven't tested this, but it should work. This basically tests for the existence of data against 3 attributes (page_blue, page_red, page_green), if it exists, then it prints out that content.

To use the attached file, you need to change the file extension to .php, and add it here:
root/blocks/page_list/templates/

Then select it from the 'custom template' options available against the page list when in edit mode.

Not knowing what type of attribute you're using, or any other details etc I can't be any more specific, but hopefully this will help!
MathiasB replied on at Permalink Reply
MathiasB
Dear DrBiskit,
thank you for the quick help. I guess I forgot to tel, that the attributes are only checkboxes. So the "if-than-else" actually have to look if the box is checked or not.

To display only the pages with the checked attribut "autograph" I modified the code like this:

<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$th = Loader::helper('text');
?>
<ul>
   <?php  foreach ($pages as $page):
      // Prepare data for each page being listed...
      $title = $th->entities($page->getCollectionDescription());
      $ag = $page->getAttribute('autograph');
      $sv = $page->getAttribute('stichvorlage');
      $ed = $page->getAttribute('erstdruck');
    ?>
       <?php if($page->getCollectionAttributeValue('autograph'))?>
         <li>
            <h2><?php echo $title ?></h2>


Unfortunately it doesn't work. It displays all pages below the parent page. Maybe there must be an "endif" or something?

Best Mathias
drbiskit replied on at Permalink Best Answer Reply
drbiskit
Nearly there! You just need to add in the opening + closing curly braces ( i.e. these: { } ) from the if statement - try this:

<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$th = Loader::helper('text');
?>
<ul>
   <?php  foreach ($pages as $page):
      // Prepare data for each page being listed...
      $title = $th->entities($page->getCollectionDescription());
      $ag = $page->getAttribute('autograph');
      $sv = $page->getAttribute('stichvorlage');
      $ed = $page->getAttribute('erstdruck');
    ?>
       <?php if($page->getCollectionAttributeValue('autograph')) { ?>
      <li>
            <h2><?php echo $title ?></h2>
MathiasB replied on at Permalink Reply
MathiasB
Cool! Thank You! That's it.
I just had to change
<? } ?>

to
<?php } ?>


Otherwise there was displayed a php alert "unexpectet endforeach".
MathiasB replied on at Permalink Reply
MathiasB
STOP I was too quick :-)

Suddenly I am not able anymore to get the link to a listed page.

I tried:
<?php  echo $nh->getLinkToCollection($cobj)?>

It doesn't work. The list is empty.
Than I added:
$link = $nh->getLinkToCollection($cobj);
.
But that causes an alert:
Fatal error: Call to a member function isExternalLink() on a non-object

Here ist the complete code
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$th = Loader::helper('text');
?>
<div class="ccm-page-list">
<ul>
   <?php  foreach ($pages as $page):
      $title = $th->entities($page->getCollectionDescription());
     $link = $nh->getLinkToCollection($cobj); 
      $ag = $page->getAttribute('autograph');
      $sv = $page->getAttribute('stichvorlage');
      $ed = $page->getAttribute('erstdruck');
    ?>
       <?php if($page->getCollectionAttributeValue('autograph')) { ?>
      <li>
MathiasB replied on at Permalink Reply
MathiasB
Got it!!!
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$th = Loader::helper('text');
$nh = Loader::helper('navigation');
$page = Page::getCurrentPage();  <-- this must be here
?>
<div class="ccm-page-list">
<ul>
   <?php  foreach ($pages as $page):
      $title = $th->entities($page->getCollectionDescription());
      $ag = $page->getAttribute('autograph');
      $sv = $page->getAttribute('stichvorlage');
      $ed = $page->getAttribute('erstdruck');
    ?>
       <?php if($page->getCollectionAttributeValue('autograph')) { ?>