1

I want to programmatically get all the values contained in a field of type category.

I am doing this on Elements::EVENT_AFTER_SAVE_ELEMENT.

If I dump $event->element->getFieldValue('productTheme') I get the field but I did't find how to go from there.

My final goal is to have the categories from the category field in an array (not all the categories in the category group, but only those selected in the field).

1

Your field contains an object of type ElementQuery in order to get all Categories you'll need to execute it.

$categories = $element->getfieldValue('productTheme')->all();
$categoryTitles = [];
for($categories as $category){
    $categoryTitles[] = $category->title;
}

Please read the documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.