You are here

public function Index::renameField in Search API 8

Changes the field ID of a field.

Parameters

string $old_field_id: The old ID of the field.

string $new_field_id: The new ID of the field.

Return value

$this

Throws

\Drupal\search_api\SearchApiException Thrown if no field with the old ID exists, or because the new ID is already taken, or because the new field ID is one of the pseudo-fields that can be used in search queries.

Overrides IndexInterface::renameField

File

src/Entity/Index.php, line 706

Class

Index
Defines the search index configuration entity.

Namespace

Drupal\search_api\Entity

Code

public function renameField($old_field_id, $new_field_id) {
  if (!isset($this
    ->getFields()[$old_field_id])) {
    throw new SearchApiException("Could not rename field with machine name '{$old_field_id}': no such field.");
  }
  $reserved = \Drupal::getContainer()
    ->get('search_api.fields_helper')
    ->isFieldIdReserved($new_field_id);
  if ($reserved) {
    throw new SearchApiException("'{$new_field_id}' is a reserved value and cannot be used as the machine name of a normal field.");
  }
  if (isset($this
    ->getFields()[$new_field_id])) {
    throw new SearchApiException("'{$new_field_id}' already exists and can't be used as a new field id.");
  }
  $this->fieldInstances[$new_field_id] = $this->fieldInstances[$old_field_id];
  unset($this->fieldInstances[$old_field_id]);
  $this->fieldInstances[$new_field_id]
    ->setFieldIdentifier($new_field_id);
  return $this;
}