You are here

public function BlockForm::submitForm in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 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 327
Contains \Drupal\block\BlockForm.

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.
  // @todo Find a way to avoid this manipulation.
  $settings = (new FormState())
    ->setValues($form_state
    ->getValue('settings'));

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

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

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

  // Submit visibility condition settings.
  foreach ($form_state
    ->getValue('visibility') as $condition_id => $values) {

    // Allow the condition to submit the form.
    $condition = $form_state
      ->get([
      'conditions',
      $condition_id,
    ]);
    $condition_values = (new FormState())
      ->setValues($values);
    $condition
      ->submitConfigurationForm($form, $condition_values);
    if ($condition instanceof ContextAwarePluginInterface) {
      $context_mapping = isset($values['context_mapping']) ? $values['context_mapping'] : [];
      $condition
        ->setContextMapping($context_mapping);
    }

    // Update the original form values.
    $condition_configuration = $condition
      ->getConfiguration();
    $form_state
      ->setValue([
      'visibility',
      $condition_id,
    ], $condition_configuration);

    // Update the visibility conditions on the block.
    $entity
      ->getVisibilityConditions()
      ->addInstanceId($condition_id, $condition_configuration);
  }

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