You are here

public function IndexFieldsForm::submitForm in Search API 8

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides EntityForm::submitForm

File

src/Form/IndexFieldsForm.php, line 448

Class

IndexFieldsForm
Provides a form for configuring the fields of a search index.

Namespace

Drupal\search_api\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $index = $this->entity;

  // Store the fields configuration.
  $fields = $index
    ->getFields();
  $field_values = $form_state
    ->getValue('fields', []);
  $new_fields = [];
  foreach ($field_values as $field_id => $new_settings) {
    if (!isset($fields[$field_id])) {
      $args = [
        '%field_id' => $field_id,
      ];
      $this->messenger
        ->addWarning($this
        ->t('The field with ID %field_id does not exist anymore.', $args));
      continue;
    }
    $field = $fields[$field_id];
    $field
      ->setLabel($new_settings['title']);
    try {
      $field
        ->setType($new_settings['type']);
    } catch (SearchApiException $e) {
      $args = [
        '%field_id' => $field_id,
        '%field' => $field
          ->getLabel(),
      ];
      $this->messenger
        ->addWarning($this
        ->t('The type of field %field (%field_id) cannot be changed.', $args));
    }
    $field
      ->setBoost($new_settings['boost']);
    $field
      ->setFieldIdentifier($new_settings['id']);
    $new_fields[$new_settings['id']] = $field;
  }
  $index
    ->setFields($new_fields);
}