/**
  * Compiles a render array for the given Block instance based on the form.
  *
  * @param \Drupal\Core\Block\BlockBase $block_instance
  *   The Block instance you want to render.
  *
  * @return array $build
  *   The Block render array.
  */
 protected function buildBlockInstance($block_instance)
 {
     // Get the new block configuration.
     $configuration = $block_instance->getConfiguration();
     // Add context to the block.
     if ($block_instance instanceof ContextAwarePluginInterface) {
         $this->contextHandler->applyContextMapping($block_instance, $this->panelsDisplay->getContexts());
     }
     // Build the block content.
     $content = $block_instance->build();
     // Disable any nested forms from the render array.
     $content = $this->removeFormWrapperRecursive($content);
     // Compile the render array.
     $build = ['#theme' => 'block', '#attributes' => [], '#contextual_links' => [], '#configuration' => $configuration, '#plugin_id' => $block_instance->getPluginId(), '#base_plugin_id' => $block_instance->getBaseId(), '#derivative_plugin_id' => $block_instance->getDerivativeId(), 'content' => $content];
     return $build;
 }
 /**
  * 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\BlockBase
  *   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'));
     }
     // Add context to the block.
     if ($block_instance instanceof ContextAwarePluginInterface) {
         $this->contextHandler->applyContextMapping($block_instance, $this->panelsDisplay->getContexts());
     }
     return $block_instance;
 }
 /**
  * Build the render array for a single panelized entity.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display
  * @param string $view_mode
  * @param string $langcode
  *
  * @return array
  */
 protected function buildPanelized(EntityInterface $entity, PanelsDisplayVariant $panels_display, $view_mode, $langcode)
 {
     $contexts = $panels_display->getContexts();
     $entity_context = new Context(new ContextDefinition('entity:' . $this->entityTypeId, NULL, TRUE), $entity);
     $contexts['@panelizer.entity_context:' . $this->entityTypeId] = $entity_context;
     $panels_display->setContexts($contexts);
     $build = $panels_display->build();
     // @todo: I'm sure more is necessary to get the cache contexts right...
     CacheableMetadata::createFromObject($entity)->applyTo($build);
     $this->getPanelizerPlugin()->alterBuild($build, $entity, $panels_display, $view_mode);
     return $build;
 }
 /**
  * {@inheritdoc}
  */
 public function build(PanelsDisplayVariant $panels_display)
 {
     $regions = $panels_display->getRegionAssignments();
     $contexts = $panels_display->getContexts();
     $layout = $panels_display->getLayout();
     $regions = $this->buildRegions($regions, $contexts);
     if ($layout) {
         $regions = $layout->build($regions);
     }
     return $regions;
 }