You are here

public function FieldsHelper::extractFieldValues in Search API 8

Extracts field values from a typed data object.

Parameters

\Drupal\Core\TypedData\TypedDataInterface $data: The typed data object.

Return value

array An array of values.

Overrides FieldsHelperInterface::extractFieldValues

1 call to FieldsHelper::extractFieldValues()
FieldsHelper::extractField in src/Utility/FieldsHelper.php
Extracts value and original type from a single piece of data.

File

src/Utility/FieldsHelper.php, line 187

Class

FieldsHelper
Provides helper methods for dealing with Search API fields and properties.

Namespace

Drupal\search_api\Utility

Code

public function extractFieldValues(TypedDataInterface $data) {
  $definition = $data
    ->getDataDefinition();

  // Process list data types.
  if ($definition
    ->isList()) {
    $values = [];
    foreach ($data as $piece) {
      $values[] = $this
        ->extractFieldValues($piece);
    }
    return $values ? call_user_func_array('array_merge', $values) : [];
  }

  // Process complex data types.
  if ($definition instanceof ComplexDataDefinitionInterface) {
    $main_property_name = $definition
      ->getMainPropertyName();
    $data_properties = $data
      ->getProperties(TRUE);
    if (isset($data_properties[$main_property_name])) {
      return $this
        ->extractFieldValues($data_properties[$main_property_name]);
    }
    return [];
  }

  // Process simple (scalar) data types.
  $value = $data
    ->getValue();
  if (is_array($value)) {
    return array_values($value);
  }
  return [
    $value,
  ];
}