[Solved] Determine a list of all selected attributes of the current page in the block controller

Permalink 1 user found helpful
Hi,

does someone knows a way to get a list of all selected attributes (handle, name and value) of the current page in the block controller?

Are there classes or functions for this? Or only via SQL query?

Looking forward to answers / examples.

Greeting
LennDa

LennDa
 
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi LennDa,

You can try this:
<?php
// get the current page object
$currentPageObject = Page::getCurrentPage();
// get the collection attributes of the page
// Concrete\Core\Entity\Attribute\Key\PageKey
$attributes = $currentPageObject->getSetCollectionAttributes();
if ($attributes) {
    $attributeList = array();
    $i = 0;
    foreach ($attributes as $attribute) {
        // get the attribute handle
        $attributeList[$i]['attributeHandle'] = $attribute->getAttributeKeyHandle();
        // get the attribute name
        $attributeList[$i]['attributeName'] = $attribute->getAttributeKeyName();
        // get the attribute type handle

- The attribute handle, attribute name, and attribute type handle will be strings.
- The attribute value could be a string, boolean, integer, object, etc.
- Because the value could be an object, and one of many different types of object (like image_file or select), you need to check its type.
- Using the attribute type handle, you could use a switch to determine what type it is and then how to process it.
*** Some of the values will be doctrine entities and using var_dump() on them will cause the page to run out of memory. When this happens, use var_dump_safe() instead.

// Example: var_dump_safe()
echo '<pre>';
var_dump_safe($attributeList[0]);
var_dump_safe($attributeList[1]);
var_dump_safe($attributeList[2]);
var_dump_safe($attributeList[3]);
echo '</pre>';
LennDa replied on at Permalink Reply
LennDa
Hi MrKDilkington,

this is exactly what I was looking for :)

To read the value of the attribute, the following conversion helped me:
$attVal = $currentPageObject->getAttributeValueObject($attribute->getAttributeKeyHandle());
$attributeList[$i]['attributeValue'] = $attVal->getPlainTextValue();
Thank you very much for your help.

Greetings
LennDa