/** * {@inheritdoc} */ 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(); $first_region = reset(array_keys($region_definitions)); // 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' => $layout_config['label'], '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; }