You are here

public function PanelsIPEBlockPluginForm::submitForm in Panels 8.3

Same name and namespace in other branches
  1. 8.4 panels_ipe/src/Form/PanelsIPEBlockPluginForm.php \Drupal\panels_ipe\Form\PanelsIPEBlockPluginForm::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

panels_ipe/src/Form/PanelsIPEBlockPluginForm.php, line 261

Class

PanelsIPEBlockPluginForm
Provides a form for adding a block plugin temporarily using AJAX.

Namespace

Drupal\panels_ipe\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Return early if there are any errors.
  if ($form_state
    ->hasAnyErrors()) {
    return $form;
  }

  // If a temporary configuration for this variant exists, use it.
  $temp_store_key = $this->panelsDisplay
    ->id();
  if ($variant_config = $this->tempStore
    ->get($temp_store_key)) {
    $this->panelsDisplay
      ->setConfiguration($variant_config);
  }
  $block_instance = $this
    ->getBlockInstance($form_state);

  // Submit the block configuration form.
  $this
    ->submitBlock($block_instance, $form, $form_state);

  // Set the block region appropriately.
  $block_config = $block_instance
    ->getConfiguration();
  $block_config['region'] = $form_state
    ->getValue(array(
    'settings',
    'region',
  ));

  // Determine if we need to update or add this block.
  if ($uuid = $form_state
    ->getValue('uuid')) {
    $this->panelsDisplay
      ->updateBlock($uuid, $block_config);
  }
  else {
    $uuid = $this->panelsDisplay
      ->addBlock($block_config);
  }

  // Set the tempstore value.
  $this->tempStore
    ->set($this->panelsDisplay
    ->id(), $this->panelsDisplay
    ->getConfiguration());

  // Assemble data required for our App.
  $build = $this
    ->buildBlockInstance($block_instance, $this->panelsDisplay);

  // Bubble Block attributes to fix bugs with the Quickedit and Contextual
  // modules.
  $this
    ->bubbleBlockAttributes($build);

  // Add our data attribute for the Backbone app.
  $build['#attributes']['data-block-id'] = $uuid;
  $plugin_definition = $block_instance
    ->getPluginDefinition();
  $block_model = [
    'uuid' => $uuid,
    'label' => $block_instance
      ->label(),
    'id' => $block_instance
      ->getPluginId(),
    'region' => $block_config['region'],
    'provider' => $block_config['provider'],
    'plugin_id' => $plugin_definition['id'],
    'html' => $this->renderer
      ->render($build),
  ];
  $form['build'] = $build;

  // Add Block metadata and HTML as a drupalSetting.
  $form['#attached']['drupalSettings']['panels_ipe']['updated_block'] = $block_model;
  return $form;
}