You are here

public function TextExtractorFormSettings::buildTextExtractorConfigForm in Search API attachments 8

Same name and namespace in other branches
  1. 9.0.x src/Form/TextExtractorFormSettings.php \Drupal\search_api_attachments\Form\TextExtractorFormSettings::buildTextExtractorConfigForm()

Subform.

It will be updated with Ajax to display the configuration of an extraction plugin method.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

1 call to TextExtractorFormSettings::buildTextExtractorConfigForm()
TextExtractorFormSettings::buildForm in src/Form/TextExtractorFormSettings.php
Form constructor.

File

src/Form/TextExtractorFormSettings.php, line 222

Class

TextExtractorFormSettings
Configuration form.

Namespace

Drupal\search_api_attachments\Form

Code

public function buildTextExtractorConfigForm(array &$form, FormStateInterface $form_state) {
  $form['text_extractor_config'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'search-api-attachments-extractor-config-form',
    ],
    '#tree' => TRUE,
  ];
  $config = $this
    ->config(static::CONFIGNAME);
  if ($form_state
    ->getValue('extraction_method') != '') {

    // It is due to the ajax.
    $extractor_plugin_id = $form_state
      ->getValue('extraction_method');
  }
  else {
    $extractor_plugin_id = $config
      ->get('extraction_method');
    $ajax_submitted_empty_value = $form_state
      ->getValue('form_id');
  }
  $form['text_extractor_config']['#type'] = 'details';
  $form['text_extractor_config']['#open'] = TRUE;

  // If the form is submitted with ajax and the empty value is chosen or if
  // there is no configuration yet and no extraction method was chosen in the
  // form.
  if (isset($ajax_submitted_empty_value) || $extractor_plugin_id == '') {
    $form['text_extractor_config']['#title'] = $this
      ->t('Please make a choice');
    $form['text_extractor_config']['#description'] = $this
      ->t('Please choose an extraction method in the list above.');
  }
  else {
    $configuration = $config
      ->get($extractor_plugin_id . '_configuration');
    $extractor_plugin = $this
      ->getTextExtractorPluginManager()
      ->createInstance($extractor_plugin_id, $configuration);
    $form['text_extractor_config']['#title'] = $this
      ->t('@extractor_plugin_label configuration', [
      '@extractor_plugin_label' => $this
        ->getExtractionPluginInformations()['labels'][$extractor_plugin_id],
    ]);
    $text_extractor_form = $extractor_plugin
      ->buildConfigurationForm([], $form_state);
    $form['text_extractor_config']['extraction_method'] = [
      '#type' => 'value',
      '#value' => $extractor_plugin_id,
    ];
    $form['text_extractor_config'] += $text_extractor_form;
  }
}