You are here

public function SubmitButtonAjax::configurationForm in Flexiform 8

The configuration form.

Parameters

array $form: The form array.

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

Return value

array The form with any additions.

Overrides ConfigurableFormEnhancerInterface::configurationForm

File

src/Plugin/FormEnhancer/SubmitButtonAjax.php, line 48

Class

SubmitButtonAjax
FormEnhancer for altering the ajax settings of submit buttons.

Namespace

Drupal\flexiform\Plugin\FormEnhancer

Code

public function configurationForm(array $form, FormStateInterface $form_state) {
  foreach ($this
    ->locateSubmitButtons() as $path => $label) {
    $original_path = $path;
    $path = str_replace('][', '::', $path);
    $form['ajax'][$path] = [
      '#type' => 'details',
      '#title' => $this
        ->t('@label Button Ajax', [
        '@label' => $label,
      ]),
      '#description' => 'Array Parents: ' . $original_path,
      '#open' => TRUE,
    ];
    $form['ajax'][$path]['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Submit with Ajax'),
      '#description' => $this
        ->t('Submit the form with Javascript to avoid page reloads.'),
      '#default_value' => !empty($this->configuration[$path]['enabled']),
    ];
    $parents = array_merge($form['#parents'], [
      $path,
      'enabled',
    ]);
    $name = array_shift($parents);
    if (!empty($parents)) {
      $name .= '[' . implode('][', $parents) . ']';
    }
    $form['ajax'][$path]['response'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Response'),
      '#description' => $this
        ->t('What should happen after the form has been successfully submitted.'),
      '#default_value' => !empty($this->configuration[$path]['response']) ? $this->configuration[$path]['response'] : 'refresh',
      '#options' => [
        'refresh' => $this
          ->t('Refresh the Form'),
        'reload' => $this
          ->t('Reload the Page'),
        'redirect' => $this
          ->t('Redirect'),
      ],
      '#states' => [
        'visible' => [
          ':input[name="' . $name . '"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  return $form;
}