Initially I was doing $entry->getContent()->getAttributes(), but noticed my Matrix fields were all null.

What is the right way to do this?

I see that $entry->myMatrixField gives me the ElementCriteriaModel for my Matrix that I need, I thought that would be the same as using `getAttribute('myMatrixField'), but i guess not.


ContentModel will only have the values in the actual content table, and the values that are actually there still may not be what you’re after, because accessing those values directly won’t give the field types a chance to do any preprocessing on their data.

So the best way to reliably get an array of all of an element’s custom field content is to go through its Field Layout, via BaseElementModel::getFieldLayout():

$values = array();

foreach ($element->getFieldLayout()->getFields() as $fieldLayoutField)
    // $fieldLayoutField is a FieldLayoutFieldModel, *not* a FieldModel
    // http://buildwithcraft.com/classreference/models/FieldLayoutFieldModel
    // so get the actual FieldModel here
    $field = $fieldLayoutField->getField();

    // Now get the prepped field value
    $value = $element->getFieldValue($field->handle);

    $values[$field->handle] = $value;

I had the same problem a couple of weeks ago: apparently there isn't a simple function that can be called as getAttributes only returns data from the content table.

I wrote the code below to get data for all matrix fields within an entry, but later on I realised that there are other fields which appear null from getAttributes such as asset and entry fields.

Anyone else have a rock-solid method for all data?

$content = $entry->getContent();

$data = $content->getAttributes(null,true);

    // Getting content / attributes doesn't appear to work for Matrix fields? - get content and re-populate data array
    $matrixFields = craft()->db->createCommand()->select(array('handle'))->where( array('type' => 'Matrix') )->from('fields')->queryAll();

    foreach( $matrixFields as $matrix )
        $handle = $matrix['handle'];

        if( $entry->$handle )
            $attributes = array();
            foreach( $entry->$handle as $matrix )
                $attr =  $matrix->getContent()->getAttributes();

                // Attribute Formatting (this is formatting for my use-case - feel free to amend as you need)
                unset( $attr['id'] , $attr['elementId'] , $attr['locale'] , $attr['title'] );
                $attr = array_merge( $attr , array("___type" => $matrix->type->handle ) );

                $attributes[] = $attr;
            $data[$handle] = $attributes;

Reference: What is getContent() / the ContentModel, and how do I use it to get and set custom field values?

In my case, I wanted to get all attributes, and all content attributes. Here's my approach: http://gist.github.com/timkelty/2a221ac232e5ecf1b090

Curious to see if there are alternate solutions, though.

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.