You are here

public function BlockFormBase::submitForm in Context 8

Same name and namespace in other branches
  1. 8.4 src/Reaction/Blocks/Form/BlockFormBase.php \Drupal\context\Reaction\Blocks\Form\BlockFormBase::submitForm()
  2. 8.0 src/Reaction/Blocks/Form/BlockFormBase.php \Drupal\context\Reaction\Blocks\Form\BlockFormBase::submitForm()

Form submission handler.

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 FormInterface::submitForm

File

src/Reaction/Blocks/Form/BlockFormBase.php, line 269

Class

BlockFormBase

Namespace

Drupal\context\Reaction\Blocks\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $settings = (new FormState())
    ->setValues($form_state
    ->getValue('settings'));

  // Call the plugin submit handler.
  $this->block
    ->submitConfigurationForm($form, $settings);

  // Update the original form values.
  $form_state
    ->setValue('settings', $settings
    ->getValues());

  // Add available contexts if this is a context aware block.
  if ($this->block instanceof ContextAwarePluginInterface) {
    $this->block
      ->setContextMapping($form_state
      ->getValue([
      'settings',
      'context_mapping',
    ], []));
  }
  $configuration = array_merge($this->block
    ->getConfiguration(), [
    'custom_id' => $form_state
      ->getValue('custom_id'),
    'region' => $form_state
      ->getValue('region'),
    'theme' => $form_state
      ->getValue('theme'),
    'css_class' => $form_state
      ->getValue('css_class'),
    'unique' => $form_state
      ->getValue('unique'),
    'context_id' => $this->context
      ->id(),
  ]);

  // Add/Update the block.
  if (!isset($configuration['uuid'])) {
    $this->reaction
      ->addBlock($configuration);
  }
  else {
    $this->reaction
      ->updateBlock($configuration['uuid'], $configuration);
  }
  $this->context
    ->save();
  $form_state
    ->setRedirectUrl(Url::fromRoute('entity.context.edit_form', [
    'context' => $this->context
      ->id(),
  ]));
}