You are here

public function FieldValueExtension::getRawValues in Twig Field Value 8

Same name and namespace in other branches
  1. 2.0.x src/Twig/Extension/FieldValueExtension.php \Drupal\twig_field_value\Twig\Extension\FieldValueExtension::getRawValues()

Twig filter callback: Return specific field item(s) value.

Parameters

array|null $build: Render array of a field.

string $key: The name of the field value to retrieve.

Return value

array|null Single field value or array of field values. If the field value is not found, null is returned.

File

src/Twig/Extension/FieldValueExtension.php, line 92

Class

FieldValueExtension
Provides field value filters for Twig templates.

Namespace

Drupal\twig_field_value\Twig\Extension

Code

public function getRawValues($build, $key = '') {
  if (!$this
    ->isFieldRenderArray($build)) {
    return NULL;
  }
  if (!isset($build['#items']) || !$build['#items'] instanceof TypedDataInterface) {
    return NULL;
  }
  $item_values = $build['#items']
    ->getValue();
  if (empty($item_values)) {
    return NULL;
  }
  $raw_values = [];
  foreach ($item_values as $delta => $values) {
    if ($key) {
      $raw_values[$delta] = isset($values[$key]) ? $values[$key] : NULL;
    }
    else {
      $raw_values[$delta] = $values;
    }
  }
  return count($raw_values) > 1 ? $raw_values : reset($raw_values);
}