Concrete5's page attributes are great for managing complex page data - on a recent project we created a 'Multi Page Selector Attribute' to be able to relate one page to many. Feel free to download this from our github repository.

In many of our concrete5 projects we create custom page types with page attributes to store structured and interrelated data. We regularly use the composer to set up custom pages, where these custom page attributes are neatly presented.

In some projects where we've wanted to related a page with one or more pages, we have installed the handy Page Select Attribute from the concrete5 marketplace. This attribute allows us to pick any page from the sitemap, and we can programatically fetch and display information about that linked page quite easily.

We came across in a recent project a relationship where it was one to many - we wanted to associate with a page one or more other pages, where we didn't want to set a limit on the number of pages able to be selected. In this case the site had Projects, and each Project needed to refer to one or more Products that had been used in the project (with each product being it's own page).

As the Page Select Attribute only handles one page at a time, we elected to build our own multi page selector that allows an unlimited number of pages to be selected, as well as providing the ability to re-order the selected page via drag and drop.

multi_page_selector.pngAn example of the attribute type set up for use in the composer, allowing a quick selection of multiple pages, with drag and drop re-ordering.

We also identified that in some scenarios we might want to return just a list of links to the pages, while at other times we might want full Page ('Collection') objects, to fetch page attributes, etc, so we created two different ways to retrieve the attribute's value.

How to set up

Once installed, a new attribute type becomes available, 'Multi Page Selector'. Attributes of this type can be created and used with pages and the composer like other attributes.

How to use in page templates

The attribute has two different ways the list of pages can be retrieved from the attribute, one way to return an array of page objects, and another way to return an array of names and links, ready for efficiently turning into something like a unordered list of links.

So say you have a page attribute with the handle products, you could do the following to fetch pages:

$products = $c->getAttribute('products', 'pageArray');
// $products now contains an array of collection (page) objects

// or 

$products = $c->getAttribute('products', 'pageLinkArray');
// $products now contains an array of arrays, each containing 'cID' 'url', and 'name', meaning you can do:

if (!empty($products)) { 
   echo '<ul>';
   foreach($products as $prod) {
       echo '<li><a href="' . $prod['url'] . '">'. $prod['name']. '</a></li>';
   }
   echo '</ul>';
}

Download

This package can be downloaded from our slowly growing github repository. After downloading and un-zipping the package folder into your packages direcory, simply rename the folder to remove the -master suffix. Use it wherever you like.

-Ryan