Skip to main content
Update for Craft 3+
Source Link
Brandon Kelly
  • 34.4k
  • 2
  • 74
  • 139

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():

// Craft 2
$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;
}

Update: As of Craft 3, you can get all the custom field values using getFieldValues():

$values = $element->getFieldValues();

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;
}

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():

// Craft 2
$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;
}

Update: As of Craft 3, you can get all the custom field values using getFieldValues():

$values = $element->getFieldValues();
Source Link
Brandon Kelly
  • 34.4k
  • 2
  • 74
  • 139

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;
}