Select List options

Permalink
I am creating a custom template for the Page Attribute Display block type to show a Select List attribute.

I have managed to override the default controller to return an array rather than a string where a select list is the attribute type. I can then loop through the options to output as an unordered list in the custom template.

Is it possible to get the original Select List attribute from within the Custom Template? I would like be able to output the whole list in the custom template and highlight the items which have been selected.

I have managed to find some explanations of how to display a Select List in a page template but I have not managed to figure out how to translate this to a Custom Template. Any pointers would be gratefully received.

Many thanks.

 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi phowie74,

What version of concrete5 are you using?
phowie74 replied on at Permalink Reply
Sorry - kind of important information I omitted. I am using 8.2.1

Thanks for taking the time to look at my query.
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
@phowie74

You can try this in your custom template.
<?php defined('C5_EXECUTE') or die('Access Denied.');
if ($controller->getTitle()) {
    echo '<span class="ccm-block-page-attribute-display-title">' . $controller->getTitle() . '</span>';
}
// Concrete\Core\Page\Page
$currentPage = Page::getCurrentPage();
// Concrete\Core\Entity\Attribute\Value\Value\SelectValue
$attribute = $currentPage->getAttribute($controller->attributeHandle);
// Concrete\Core\Entity\Attribute\Key\PageKey
$attributeKey = CollectionAttributeKey::getByHandle($controller->attributeHandle);
if (is_object($attributeKey)) {
    echo '<ul>';
    // Concrete\Core\Entity\Attribute\Type
    $attributeType = $attributeKey->getAttributeType();
    if ($attributeType->getAttributeTypeHandle() == 'select') {

This code assumes that the "Property to Display" attribute selected in the Page Attribute form was a "select" attribute. The title code is optional and can be removed if you don't need it.
phowie74 replied on at Permalink Reply
Thank you so much - that was exactly what I was after.

I have tweaked it slightly as it was not highlighting the selected options for me for some reason? My php skills are not great but the $attribute was returning an object containing all select options and each $option in the loop was a single select option also as an object so they never matched. I have made sure both of them are strings and then used a strpos comparison to hopefully achieve the same effect. This does rely on the select options being unique I guess but it works in my situation.

<?php defined('C5_EXECUTE') or die('Access Denied.');
if ($controller->getTitle()) {
    echo '<span class="ccm-block-page-attribute-display-title">' . $controller->getTitle() . '</span>';
}
// Concrete\Core\Page\Page
$currentPage = Page::getCurrentPage();
// Concrete\Core\Entity\Attribute\Value\Value\SelectValue
$attribute = $currentPage->getAttribute($controller->attributeHandle);
// Concrete\Core\Entity\Attribute\Key\PageKey
$attributeKey = CollectionAttributeKey::getByHandle($controller->attributeHandle);
if (is_object($attributeKey)) {
    echo '<ul>';
    // Concrete\Core\Entity\Attribute\Type
    $attributeType = $attributeKey->getAttributeType();
    if ($attributeType->getAttributeTypeHandle() == 'select') {


Thanks for taking the time to look into my query - you have been a great help.

Can I also check, where you have the (as I saw them) commented lines e.g.
// Concrete\Core\Entity\Attribute\Key\PageKey

is that the equivalent of a "use" statement in a Custom Template? e.g.
use CollectionAttributeKey;


Thanks
Paul
MrKDilkington replied on at Permalink Reply
MrKDilkington
@phowie74

Were you using a select or a multiple select? If you are using a multiple select, then it would probably use different code.

I added the commented namespaces to show what is returned.

Example:
// Concrete\Core\Entity\Attribute\Key\PageKey
$attributeKey = CollectionAttributeKey::getByHandle($controller->attributeHandle);

CollectionAttributeKey::getByHandle($controller->attributeHandle) returns a Concrete\Core\Entity\Attribute\Key\PageKey object ($attributeKey).

CollectionAttributeKey is an alias of \Concrete\Core\Attribute\Key\CollectionKey. I think it might be inherited somewhere.
https://github.com/concrete5/concrete5/blob/develop/concrete/config/...
phowie74 replied on at Permalink Reply
It was indeed a multiple select list - I was playing with adding a kind of basic todo list to my composer setup but couldn't figure out how to show what was outstanding as well as the completed tasks on the page itself. It is only an experiment on my own site but it always frustrating when you know something should be possible but just can't quite put it into practice!

The namespaces also make perfect sense now and your comments are very helpful - I have not had much experience working with the latest versions of Concrete5 and I had been using the excellent webli.us cheat sheet to try to figure this out -http://www.webli.us/cheatsheet/doku.php#display_get_multiple_values...

use CollectionAttributeKey;
  $ak = CollectionAttributeKey::getByHandle('attribute_handle');


in the Custom template becomes:

$attributeKey = CollectionAttributeKey::getByHandle($controller->attributeHandle);



I can see now that my tweak also renders your $currentPage page object redundant as my selected options are already available from the block controller. I have updated the custom template to

// Concrete\Core\Entity\Attribute\Key\PageKey
$attributeKey = CollectionAttributeKey::getByHandle($controller->attributeHandle);
if (is_object($attributeKey)) {
   echo '<ul>';
   // Concrete\Core\Entity\Attribute\Type
   $attributeType = $attributeKey->getAttributeType();
   if ($attributeType->getAttributeTypeHandle() == 'select') {
       // Concrete\Attribute\Select\Controller
       $attributeController = $attributeType->getController();
       $attributeController->setAttributeKey($attributeKey);
       $selectValueOptions = $attributeController->getOptions();
$checkme = $controller->getContent();
       foreach ($selectValueOptions as $option){
          if (strpos($checkme, (string) $option) !== false) {
               echo "<li class=\"selected\">$option</li>";


Thanks again for all your help - every day is a school day!