public function Index::addField in Search API 8
Adds a field to this index.
Parameters
\Drupal\search_api\Item\FieldInterface $field: The field to add.
Return value
$this
Throws
\Drupal\search_api\SearchApiException Thrown if the field could not be added, either because a field with the same field ID already exists, or because the field identifier is one of the reserved field IDs of pseudo-fields that can be used in search queries.
Overrides IndexInterface::addField
File
- src/
Entity/ Index.php, line 682
Class
- Index
- Defines the search index configuration entity.
Namespace
Drupal\search_api\EntityCode
public function addField(FieldInterface $field) {
$field_id = $field
->getFieldIdentifier();
$reserved = \Drupal::getContainer()
->get('search_api.fields_helper')
->isFieldIdReserved($field_id);
if ($reserved) {
throw new SearchApiException("'{$field_id}' is a reserved value and cannot be used as the machine name of a normal field.");
}
// This will automatically call getFields(), thus initializing
// $this->fieldInstances, if that hasn't been done yet.
$old_field = $this
->getField($field_id);
if ($old_field && $old_field != $field) {
throw new SearchApiException("Cannot add field with machine name '{$field_id}': machine name is already taken.");
}
$this->fieldInstances[$field_id] = $field;
return $this;
}