You are here

function _datalayer_field_get_value in dataLayer 8

Get an array of values from a field object.

Parameters

object $field_item: Field containing the values.

string $field_type: The type of field the value belongs to.

Return value

array Numeric array of values.

1 call to _datalayer_field_get_value()
_datalayer_get_field_values in ./datalayer.module
Get values for exposed fields.

File

./datalayer.module, line 519
Client-side data space.

Code

function _datalayer_field_get_value($field_item, $field_type) {
  $value = [];
  switch ($field_type) {
    case 'entity_reference':
      if (!$field_item
        ->isEmpty() && $field_item->entity instanceof EntityInterface) {
        $entity = $field_item->entity;
        $value = [
          'id' => $entity
            ->id(),
          'label' => $entity
            ->label(),
          'bundle' => $entity
            ->bundle(),
        ];
      }
      break;
    case 'metatag':
      $field = $field_item
        ->getValue();
      $values = unserialize($field['value']);
      foreach ($values as $key => $thisvalue) {
        $value[$key] = $thisvalue;
      }
      break;
    default:
      $value = $field_item
        ->getValue();
      if (count($value) == 1 && array_key_exists('value', $value)) {
        $value = $value['value'];
      }
      break;
  }

  // Allow modules to alter field values hook_datalayer_field_alter().
  \Drupal::moduleHandler()
    ->alter('datalayer_field', $value, $field_item, $field_type);
  return $value;
}