You are here

public function IndexAddFieldsForm::addField in Search API 8

Form submission handler for adding a new field to the index.

Parameters

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

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

File

src/Form/IndexAddFieldsForm.php, line 476

Class

IndexAddFieldsForm
Provides a form for adding fields to a search index.

Namespace

Drupal\search_api\Form

Code

public function addField(array $form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();
  if (!$button) {
    return;
  }

  /** @var \Drupal\Core\TypedData\DataDefinitionInterface $property */
  $property = $button['#property'];
  list($datasource_id, $property_path) = Utility::splitCombinedId($button['#name']);
  $field = $this->fieldsHelper
    ->createFieldFromProperty($this->entity, $property, $datasource_id, $property_path, NULL, $button['#data_type']);
  $field
    ->setLabel($button['#prefixed_label']);
  $this->entity
    ->addField($field);
  if ($property instanceof ConfigurablePropertyInterface) {
    $parameters = [
      'search_api_index' => $this->entity
        ->id(),
      'field_id' => $field
        ->getFieldIdentifier(),
    ];
    $options = [];
    $route = $this
      ->getRequest()->attributes
      ->get('_route');
    if ($route === 'entity.search_api_index.add_fields_ajax') {
      $options['query'] = [
        MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
        'modal_redirect' => 1,
      ];
    }
    $form_state
      ->setRedirect('entity.search_api_index.field_config', $parameters, $options);
  }
  $args['%label'] = $field
    ->getLabel();
  $this->messenger
    ->addStatus($this
    ->t('Field %label was added to the index.', $args));
}