/** * Ensures that a field with certain properties is indexed on the index. * * Can be used as a helper method in preIndexSave(). * * @param string|null $datasource_id * The ID of the field's datasource, or NULL for a datasource-independent * field. * @param string $property_path * The field's property path on the datasource. * @param string|null $type * (optional) If set, the field should have this type. * * @return \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. */ protected function ensureField($datasource_id, $property_path, $type = NULL) { $field = $this->findField($datasource_id, $property_path, $type); if (!$field) { $property = Utility::retrieveNestedProperty($this->index->getPropertyDefinitions($datasource_id), $property_path); if (!$property) { $args['%property'] = Utility::createCombinedId($datasource_id, $property_path); $args['%processor'] = $this->label(); $message = new FormattableMarkup('Could not find property %property which is required by the %processor processor.', $args); throw new SearchApiException($message); } $field = Utility::createFieldFromProperty($this->index, $property, $datasource_id, $property_path, NULL, $type); } $field->setIndexedLocked(); if (isset($type)) { $field->setTypeLocked(); } $this->index->addField($field); return $field; }