You are here

public function BlockForm::submitForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/block/src/BlockForm.php \Drupal\block\BlockForm::submitForm()
  2. 10 core/modules/block/src/BlockForm.php \Drupal\block\BlockForm::submitForm()

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

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

File

core/modules/block/src/BlockForm.php, line 342

Class

BlockForm
Provides form for block instance forms.

Namespace

Drupal\block

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $entity = $this->entity;

  // The Block Entity form puts all block plugin form elements in the
  // settings form element, so just pass that to the block for submission.
  $sub_form_state = SubformState::createForSubform($form['settings'], $form, $form_state);

  // Call the plugin submit handler.
  $block = $entity
    ->getPlugin();
  $this
    ->getPluginForm($block)
    ->submitConfigurationForm($form, $sub_form_state);

  // If this block is context-aware, set the context mapping.
  if ($block instanceof ContextAwarePluginInterface && $block
    ->getContextDefinitions()) {
    $context_mapping = $sub_form_state
      ->getValue('context_mapping', []);
    $block
      ->setContextMapping($context_mapping);
  }
  $this
    ->submitVisibility($form, $form_state);

  // Save the settings of the plugin.
  $entity
    ->save();
  $this
    ->messenger()
    ->addStatus($this
    ->t('The block configuration has been saved.'));
  $form_state
    ->setRedirect('block.admin_display_theme', [
    'theme' => $form_state
      ->getValue('theme'),
  ], [
    'query' => [
      'block-placement' => Html::getClass($this->entity
        ->id()),
    ],
  ]);
}