Get Express Values from page attribute

Permalink 1 user found helpful
Hi,
I created a page attribute (express object type). How can I programmatically retrieve data in object after getting the value with this code?
$obj = $c->getAttribute('my_attribute_handle');
...

 
shahroq replied on at Permalink Reply
shahroq
$content = $c->getAttributeValue('my_attribute_handle')->getDisplayValue();
Herkool replied on at Permalink Reply
This fetches the whole record as a string. Is there a way to access a field inside object separately?
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi Sepehr,

Here is an example of retrieving an Express Entity page attribute and working with its Express Entry.
- "test_express" is an Express Entity attribute applied to a page
$c = Page::getCurrentPage();
// Concrete\Core\Entity\Attribute\Value\Value\ExpressValue
$testExpressValue = $c->getAttribute('test_express');
if ($testExpressValue) {
    // array of Concrete\Core\Entity\Express\Entry
    $testExpressEntryArray = $testExpressValue->getSelectedEntries();
    // Concrete\Core\Entity\Express\Entry
    $testExpressEntry = $testExpressEntryArray[0];
    // array of Concrete\Core\Entity\Attribute\Value\ExpressValue
    $attributes = $testExpressEntry->getAttributes();
    foreach ($attributes as $attribute) {
        // Concrete\Core\Entity\Attribute\Value\ExpressValue
        echo '<p>' . $attribute->getAttributeKey()->getAttributeKeyName() . ': ' . $attribute->getDisplayValue() . '</p>';
    }
}
Herkool replied on at Permalink Reply
Hi Karl,
Thank you very much for the answer. Another thing, if I know the Express fields handle, is there a way to get it directly without looping through attributes? e.g I know this Express Object has a first_name and last_name field. Can I get the value with something like:
$attributes['first_name']
MrKDilkington replied on at Permalink Reply
MrKDilkington
@Sepehr

Once you have the Express Entry, you can use the Express object magic methods to get attributes.
$c = Page::getCurrentPage();
// Concrete\Core\Entity\Attribute\Value\Value\ExpressValue
$testExpressValue = $c->getAttribute('test_express');
if ($testExpressValue) {
    // array of Concrete\Core\Entity\Express\Entry
    $testExpressEntryArray = $testExpressValue->getSelectedEntries();
    // Concrete\Core\Entity\Express\Entry
    $testExpressEntry = $testExpressEntryArray[0];
    // get attributes using express magic methods
    // Example: default install Contact express object
    // - contact_question_first_name
    // - contact_question_last_name
    // - contact_question_email_address
    // - contact_question_subject
    // - contact_question_message
simonchilton replied on at Permalink Reply
simonchilton
For anyone trying to work with images in Express objects:

$photoFileObject = $entry->getImageAttributeHandle();
if (is_object($photoFileObject)) {
    $photoFileObjectVersion = $photoFileObject->getVersion();
    $photoRelativePath = $photoFileObjectVersion->getRelativePath();
}


** replace ImageAttributeHandle with the handle of your image