/**
  * {@inheritdoc}
  */
 public function validateConfigurationForm(array &$form, FormStateInterface $form_state)
 {
     parent::validateConfigurationForm($form, $form_state);
     // Validate layout settings.
     if ($form_state->hasValue('layout_settings')) {
         $layout = $this->layoutManager->createInstance($form_state->getValue('layout'), $this->configuration['layout_settings']);
         list($layout_settings_form, $layout_settings_form_state) = $this->getLayoutSettingsForm($form, $form_state);
         $layout->validateConfigurationForm($layout_settings_form, $layout_settings_form_state);
         // Save the layout plugin for later (so we don't have to instantiate again
         // on submit.
         $form_state->set('layout_plugin', $layout);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function setConfiguration(array $configuration)
 {
     if (empty($configuration['uuid'])) {
         $configuration['uuid'] = $this->uuidGenerator()->generate();
     }
     // Make sure blocks are mapped to valid regions, and if not, map them to the
     // first available region. This is a work-around the fact that we're not
     // totally in control of the block placement UI from page_manager.
     // @todo Replace after https://www.drupal.org/node/2550879
     if (!empty($configuration['layout']) && !empty($configuration['blocks'])) {
         $layout_definition = $this->layoutManager->getDefinition($configuration['layout']);
         $valid_regions = $layout_definition['regions'];
         $first_region = array_keys($valid_regions)[0];
         foreach ($configuration['blocks'] as &$block) {
             if (!isset($valid_regions[$block['region']])) {
                 $block['region'] = $first_region;
             }
         }
     }
     return parent::setConfiguration($configuration);
 }