You are here

protected function ProcessorPluginBase::ensureField in Search API 8

Ensures that a field with certain properties is indexed on the index.

Can be used as a helper method in preIndexSave().

Parameters

string|null $datasource_id: The ID of the field's datasource, or NULL for a datasource-independent field.

string $property_path: The field's property path on the datasource.

string|null $type: (optional) If set, the field should have this type.

Return value

\Drupal\search_api\Item\FieldInterface A field on the index, possibly newly added, with the specified properties.

Throws

\Drupal\search_api\SearchApiException Thrown if there is no property with the specified path, or no type is given and no default could be determined for the property.

1 call to ProcessorPluginBase::ensureField()
ContentAccess::preIndexSave in src/Plugin/search_api/processor/ContentAccess.php
Preprocesses the search index entity before it is saved.

File

src/Processor/ProcessorPluginBase.php, line 212

Class

ProcessorPluginBase
Defines a base class from which other processors may extend.

Namespace

Drupal\search_api\Processor

Code

protected function ensureField($datasource_id, $property_path, $type = NULL) {
  $field = $this
    ->findField($datasource_id, $property_path, $type);
  if (!$field) {
    $properties = $this->index
      ->getPropertyDefinitions($datasource_id);
    $property = $this
      ->getFieldsHelper()
      ->retrieveNestedProperty($properties, $property_path);
    if (!$property) {
      $property_id = Utility::createCombinedId($datasource_id, $property_path);
      $processor_label = $this
        ->label();
      throw new SearchApiException("Could not find property '{$property_id}' which is required by the '{$processor_label}' processor.");
    }
    $field = $this
      ->getFieldsHelper()
      ->createFieldFromProperty($this->index, $property, $datasource_id, $property_path, NULL, $type);
    $this->index
      ->addField($field);
  }
  $field
    ->setIndexedLocked();
  if ($type !== NULL) {
    $field
      ->setTypeLocked();
  }
  return $field;
}