You are here

public function PanelsIPELayoutForm::submitForm in Panels 8.3

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

1 call to PanelsIPELayoutForm::submitForm()
PanelsIPELayoutForm::buildForm in panels_ipe/src/Form/PanelsIPELayoutForm.php
Builds a form that configure an existing or new layout for the IPE.

File

panels_ipe/src/Form/PanelsIPELayoutForm.php, line 167

Class

PanelsIPELayoutForm
Provides a form for configuring a layout for use with the IPE.

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;
  }
  $panels_display = $this->panelsDisplay;

  // Submit the layout form.
  $layout_form_state = (new FormState())
    ->setValues($form_state
    ->getValue('settings', []));
  $this->layout
    ->submitConfigurationForm($form, $layout_form_state);
  $layout_config = $this->layout
    ->getConfiguration();

  // Shift our blocks to the first available region. The IPE can control
  // re-assigning blocks in a smarter way.
  $region_definitions = $this->layout
    ->getRegionDefinitions();
  $region_ids = array_keys($region_definitions);
  $first_region = reset($region_ids);

  // For each block, set the region to match the new layout.
  foreach ($panels_display
    ->getRegionAssignments() as $region => $region_assignment) {

    /** @var \Drupal\Core\Block\BlockPluginInterface $block */
    foreach ($region_assignment as $block_id => $block) {
      $block_config = $block
        ->getConfiguration();

      // If the new layout does not have a region with the same name, use the
      // first available region.
      if (!isset($region_definitions[$block_config['region']])) {
        $block_config['region'] = $first_region;
        $panels_display
          ->updateBlock($block_id, $block_config);
      }
    }
  }

  // Have our panels display use the new layout.
  $this->panelsDisplay
    ->setLayout($this->layout, $layout_config);

  // Update tempstore.
  $this->tempStore
    ->set($panels_display
    ->id(), $panels_display
    ->getConfiguration());
  $region_data = [];
  $region_content = [];

  // Compile region content and metadata.
  $regions = $panels_display
    ->getRegionAssignments();
  foreach ($regions as $id => $label) {

    // Wrap the region with a class/data attribute that our app can use.
    $region_name = Html::getClass("block-region-{$id}");
    $region_content[$id] = [
      '#prefix' => '<div class="' . $region_name . '" data-region-name="' . $id . '">',
      '#suffix' => '</div>',
    ];

    // Format region metadata.
    $region_data[] = [
      'name' => $id,
      'label' => $label,
    ];
  }
  $build = $panels_display
    ->getLayout()
    ->build($region_content);
  $form['build'] = $build;
  $data = [
    'id' => $this->layout
      ->getPluginId(),
    'label' => $this->layout
      ->getLabel(),
    'current' => TRUE,
    'html' => $this->renderer
      ->render($build),
    'regions' => $region_data,
  ];

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