You are here

public function Item::getFields in Search API 8

Returns the item's fields.

Parameters

bool $extract: (optional) If FALSE, only returns the fields that were previously set or extracted. Defaults to extracting the fields from the original object if necessary.

Return value

\Drupal\search_api\Item\FieldInterface[] An array with the fields of this item, keyed by field identifier.

Overrides ItemInterface::getFields

3 calls to Item::getFields()
Item::getField in src/Item/Item.php
Retrieves a single field of this item.
Item::getIterator in src/Item/Item.php
Item::__toString in src/Item/Item.php
Implements the magic __toString() method to simplify debugging.

File

src/Item/Item.php, line 233

Class

Item
Provides a default implementation for a search item.

Namespace

Drupal\search_api\Item

Code

public function getFields($extract = TRUE) {
  if ($extract && !$this->fieldsExtracted) {
    $data_type_fallback_mapping = \Drupal::getContainer()
      ->get('search_api.data_type_helper')
      ->getDataTypeFallbackMapping($this->index);
    foreach ([
      NULL,
      $this
        ->getDatasourceId(),
    ] as $datasource_id) {
      $fields_by_property_path = [];
      $processors_with_fields = [];
      $properties = $this->index
        ->getPropertyDefinitions($datasource_id);
      foreach ($this->index
        ->getFieldsByDatasource($datasource_id) as $field_id => $field) {

        // Don't overwrite fields that were previously set.
        if (empty($this->fields[$field_id])) {
          $this->fields[$field_id] = clone $field;
          $field_data_type = $this->fields[$field_id]
            ->getType();

          // If the field data type is in the fallback mapping list, then use
          // the fallback type as field type.
          if (isset($data_type_fallback_mapping[$field_data_type])) {
            $this->fields[$field_id]
              ->setType($data_type_fallback_mapping[$field_data_type]);
          }

          // For determining whether the field is provided via a processor, we
          // need to check using the first part of its property path (in other
          // words, the property that's directly on the result item, not
          // nested), since only direct properties of the item can be added by
          // the processor.
          $property = NULL;
          $property_name = Utility::splitPropertyPath($field
            ->getPropertyPath(), FALSE)[0];
          if (isset($properties[$property_name])) {
            $property = $properties[$property_name];
          }
          if ($property instanceof ProcessorPropertyInterface) {
            $processors_with_fields[$property
              ->getProcessorId()] = TRUE;
          }
          elseif ($datasource_id) {
            $fields_by_property_path[$field
              ->getPropertyPath()][] = $this->fields[$field_id];
          }
        }
      }
      try {
        if ($fields_by_property_path) {
          \Drupal::getContainer()
            ->get('search_api.fields_helper')
            ->extractFields($this
            ->getOriginalObject(), $fields_by_property_path, $this
            ->getLanguage());
        }
        if ($processors_with_fields) {
          $processors = $this->index
            ->getProcessorsByStage(ProcessorInterface::STAGE_ADD_PROPERTIES);
          foreach ($processors as $processor_id => $processor) {
            if (isset($processors_with_fields[$processor_id])) {
              $processor
                ->addFieldValues($this);
            }
          }
        }
      } catch (SearchApiException $e) {

        // If we couldn't load the object, just log an error and fail
        // silently to set the values.
        $this
          ->logException($e);
      }
    }
    $this->fieldsExtracted = TRUE;
  }
  return $this->fields;
}