You are here

public function SynonymImportForm::buildForm in Search API Synonym 8

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/SynonymImportForm.php, line 71

Class

SynonymImportForm
Class SynonymImportForm.

Namespace

Drupal\search_api_synonym\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // File.
  $form['file_upload'] = [
    '#type' => 'file',
    '#title' => $this
      ->t('File'),
    '#description' => $this
      ->t('Select the import file.'),
    '#required' => FALSE,
  ];

  // Update
  $form['update_existing'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Update existing'),
    '#description' => $this
      ->t('What should happen with existing synonyms?'),
    '#options' => [
      'merge' => $this
        ->t('Merge'),
      'overwrite' => $this
        ->t('Overwrite'),
    ],
    '#default_value' => 'merge',
    '#required' => TRUE,
  ];

  // Synonym type.
  $form['synonym_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Type '),
    '#description' => $this
      ->t('Which synonym type should the imported data be saved as?'),
    '#options' => [
      'synonym' => $this
        ->t('Synonym'),
      'spelling_error' => $this
        ->t('Spelling error'),
      'mixed' => $this
        ->t('Mixed - Controlled by information in the source file'),
    ],
    '#default_value' => 'synonym',
    '#required' => TRUE,
  ];
  $message = $this
    ->t('Notice: the source file must contain information per synonym about the synonym type. All synonyms without type information will be skipped during import!');
  $message = Markup::create('<div class="messages messages--warning">' . $message . '</div>');
  $form['synonym_type_notice'] = [
    '#type' => 'item',
    '#markup' => $message,
    '#states' => [
      'visible' => [
        ':radio[name="synonym_type"]' => [
          'value' => 'mixed',
        ],
      ],
    ],
  ];

  // Activate.
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Activate synonyms'),
    '#description' => $this
      ->t('Mark import synonyms as active. Only active synonyms will be exported to the configured search backend.'),
    '#default_value' => TRUE,
    '#required' => TRUE,
  ];

  // Language code.
  $form['langcode'] = [
    '#type' => 'language_select',
    '#title' => $this
      ->t('Language'),
    '#description' => $this
      ->t('Which language should the imported data be saved as?'),
    '#default_value' => \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId(),
  ];

  // Import plugin configuration.
  $form['plugin'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Import format'),
    '#description' => $this
      ->t('Choose the import format to use.'),
    '#options' => [],
    '#default_value' => key($this->availablePlugins),
    '#required' => TRUE,
  ];
  $form['plugin_settings'] = [
    '#tree' => TRUE,
  ];
  foreach ($this->availablePlugins as $id => $instance) {
    $definition = $instance
      ->getPluginDefinition();
    $form['plugin']['#options'][$id] = $definition['label'];
    $form['plugin_settings'][$id] = [
      '#type' => 'details',
      '#title' => $this
        ->t('@plugin plugin', [
        '@plugin' => $definition['label'],
      ]),
      '#open' => TRUE,
      '#tree' => TRUE,
      '#states' => [
        'visible' => [
          ':radio[name="plugin"]' => [
            'value' => $id,
          ],
        ],
      ],
    ];
    $form['plugin_settings'][$id] += $instance
      ->buildConfigurationForm([], $form_state);
  }

  // Actions.
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Import file'),
    '#button_type' => 'primary',
  ];
  return $form;
}