You are here

function paragraphs_field_property_get in Paragraphs 7

Entity property info getter callback for the paragraph items.

Like entity_metadata_field_property_get(), but additionally supports getting not-yet saved collection items from

$item['entity'];

.

1 string reference to 'paragraphs_field_property_get'
paragraphs_entity_metadata_property_callback in ./paragraphs.module
Callback for generating entity metadata property info for a field instance.

File

./paragraphs.module, line 1077
Paragraphs hooks and common functions.

Code

function paragraphs_field_property_get($entity, array $options, $name, $entity_type, $info) {
  $field = field_info_field($name);
  $langcode = field_language($entity_type, $entity, $name, isset($options['language']) ? $options['language']->language : NULL);
  $values = array();
  if (isset($entity->{$name}[$langcode])) {
    foreach ($entity->{$name}[$langcode] as $delta => $data) {

      // Wrappers do not support multiple entity references being revisions or
      // not yet saved entities. In the case of a single reference we can return
      // the entity object though.
      if ($field['cardinality'] == 1) {
        $values[$delta] = paragraphs_field_get_entity($data);
      }
      elseif (isset($data['value'])) {
        $values[$delta] = $data['value'];
      }
    }
  }

  // For an empty single-valued field, we have to return NULL.
  return $field['cardinality'] == 1 ? $values ? reset($values) : NULL : $values;
}