/**
  * Updates the current Panels display based on the changes done in our app.
  *
  * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display
  *   The current Panels display.
  * @param array $layout_model
  *   The decoded LayoutModel from our App.
  *
  * @return \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant
  */
 private static function updatePanelsDisplay(PanelsDisplayVariant $panels_display, array $layout_model)
 {
     // Set our weight and region based on the metadata in our Backbone app.
     foreach ($layout_model['regionCollection'] as $region) {
         $weight = 0;
         foreach ($region['blockCollection'] as $block) {
             /** @var \Drupal\Core\Block\BlockBase $block_instance */
             $block_instance = $panels_display->getBlock($block['uuid']);
             $block_instance->setConfigurationValue('region', $region['name']);
             $block_instance->setConfigurationValue('weight', ++$weight);
             $panels_display->updateBlock($block['uuid'], $block_instance->getConfiguration());
         }
     }
     return $panels_display;
 }
 /**
  * Loads or creates a Block Plugin instance suitable for rendering or testing.
  *
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  *
  * @return \Drupal\Core\Block\BlockPluginInterface
  *   The Block Plugin instance.
  */
 protected function getBlockInstance(FormStateInterface $form_state)
 {
     // If a UUID is provided, the Block should already exist.
     if ($uuid = $form_state->getValue('uuid')) {
         // 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);
         }
         // Load the existing Block instance.
         $block_instance = $this->panelsDisplay->getBlock($uuid);
     } else {
         // Create an instance of this Block plugin.
         /** @var \Drupal\Core\Block\BlockBase $block_instance */
         $block_instance = $this->blockManager->createInstance($form_state->getValue('plugin_id'));
     }
     return $block_instance;
 }
 /**
  * Updates the current Panels display based on the changes done in our app.
  *
  * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display
  *   The current Panels display.
  * @param array $layout_model
  *   The decoded LayoutModel from our App.
  *
  * @return \Symfony\Component\HttpFoundation\JsonResponse
  */
 protected function updatePanelsDisplay(PanelsDisplayVariant $panels_display, array $layout_model)
 {
     // Set our weight and region based on the metadata in our Backbone app.
     foreach ($layout_model['regionCollection'] as $region) {
         $weight = 0;
         foreach ($region['blockCollection'] as $block) {
             /** @var \Drupal\Core\Block\BlockBase $block_instance */
             $block_instance = $panels_display->getBlock($block['uuid']);
             $block_instance->setConfigurationValue('region', $region['name']);
             $block_instance->setConfigurationValue('weight', ++$weight);
             $panels_display->updateBlock($block['uuid'], $block_instance->getConfiguration());
         }
     }
     // Remove blocks that need removing.
     // @todo We should do this on the fly instead of at on save.
     foreach ($layout_model['deletedBlocks'] as $uuid) {
         $panels_display->removeBlock($uuid);
     }
     // Save the variant and remove temp storage.
     $this->savePanelsDisplay($panels_display, FALSE);
     return new JsonResponse(['deletedBlocks' => []]);
 }