/**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->panelsDisplay = $this->prophesize(PanelsDisplayVariant::class);
     $this->pageVariant = $this->prophesize(PageVariantInterface::class);
     $this->pageVariant->getVariantPlugin()->willReturn($this->panelsDisplay->reveal());
     $this->pageVariantNotPanels = $this->prophesize(PageVariantInterface::class);
     $this->pageVariantNotPanels->getContexts()->shouldNotBeCalled();
     $non_panels_variant = $this->prophesize(HttpStatusCodeDisplayVariant::class);
     $this->pageVariantNotPanels->getVariantPlugin()->willReturn($non_panels_variant->reveal());
     $this->storage = $this->prophesize(EntityStorageInterface::class);
     $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
     $this->entityTypeManager->getStorage('page_variant')->willReturn($this->storage->reveal());
 }
Esempio n. 2
0
 /**
  * Gets the variant plugin for this page variant entity.
  *
  * @return \Drupal\Core\Display\VariantInterface
  */
 protected function getVariantPlugin()
 {
     if (!$this->variantPlugin) {
         $this->variantPlugin = $this->entity->getVariantPlugin();
     }
     return $this->variantPlugin;
 }
 /**
  * Gets the variant plugin for this page variant entity.
  *
  * @return \Drupal\ctools\Plugin\BlockVariantInterface
  */
 protected function getVariantPlugin()
 {
     return $this->pageVariant->getVariantPlugin();
 }
 /**
  * Gets a given layout with empty regions and relevant metadata.
  *
  * @param \Drupal\page_manager\PageVariantInterface $page_variant
  *   The page variant entity.
  * @param string $layout_id
  *   The machine name of the requested layout.
  *
  * @return \Symfony\Component\HttpFoundation\JsonResponse
  */
 public function getLayout(PageVariantInterface $page_variant, $layout_id)
 {
     /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $variant_plugin */
     $variant_plugin = $page_variant->getVariantPlugin();
     // Build the requested layout.
     $configuration = $variant_plugin->getConfiguration();
     $configuration['layout'] = $layout_id;
     $variant_plugin->setConfiguration($configuration);
     // Inherit our PageVariant's contexts before rendering.
     $variant_plugin->setContexts($page_variant->getContexts());
     $regions = $variant_plugin->getRegionNames();
     $region_data = [];
     $region_content = [];
     // Compile region content and metadata.
     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 = $variant_plugin->getLayout()->build($region_content);
     // Get the current layout.
     $current_layout = $variant_plugin->getLayout()->getPluginId();
     // Get a list of all available layouts.
     $layouts = $this->layoutPluginManager->getLayoutOptions();
     $data = ['id' => $layout_id, 'label' => $layouts[$layout_id], 'current' => $current_layout == $layout_id, 'html' => $this->renderer->render($build), 'regions' => $region_data];
     // Update temp store.
     $this->savePageVariant($page_variant);
     // Return a structured JSON response for our Backbone App.
     return new JsonResponse($data);
 }
 /**
  * Builds a form that constructs a unsaved instance of a Block for the IPE.
  *
  * @param array $form
  *   An associative array containing the structure of the form.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  * @param string $plugin_id
  *   The requested Block Plugin ID.
  * @param \Drupal\page_manager\PageVariantInterface $page_variant
  *   The current PageVariant ID.
  * @param string $uuid
  *   An optional Block UUID, if this is an existing Block.
  *
  * @return array
  *   The form structure.
  */
 public function buildForm(array $form, FormStateInterface $form_state, $plugin_id = NULL, PageVariantInterface $page_variant = NULL, $uuid = NULL)
 {
     // We require these default arguments.
     if (!$plugin_id || !$page_variant) {
         return FALSE;
     }
     /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $variant_plugin */
     $variant_plugin = $page_variant->getVariantPlugin();
     // Grab the current layout's regions.
     $regions = $variant_plugin->getRegionNames();
     // If $uuid is present, a block should exist.
     if ($uuid) {
         /** @var \Drupal\Core\Block\BlockBase $block_instance */
         $block_instance = $variant_plugin->getBlock($uuid);
     } else {
         // Create an instance of this Block plugin.
         /** @var \Drupal\Core\Block\BlockBase $block_instance */
         $block_instance = $this->blockManager->createInstance($plugin_id);
     }
     // Determine the current region.
     $block_config = $block_instance->getConfiguration();
     if (isset($block_config['region']) && isset($regions[$block_config['region']])) {
         $region = $block_config['region'];
     } else {
         $region = reset($regions);
     }
     // Wrap the form so that our AJAX submit can replace its contents.
     $form['#prefix'] = '<div id="panels-ipe-block-plugin-form-wrapper">';
     $form['#suffix'] = '</div>';
     // Add our various card wrappers.
     $form['flipper'] = ['#type' => 'container', '#attributes' => ['class' => 'flipper']];
     $form['flipper']['front'] = ['#type' => 'container', '#attributes' => ['class' => 'front']];
     $form['flipper']['back'] = ['#type' => 'container', '#attributes' => ['class' => 'back']];
     $form['#attributes']['class'][] = 'flip-container';
     // Get the base configuration form for this block.
     $form['flipper']['front']['settings'] = $block_instance->buildConfigurationForm([], $form_state);
     // Add the block ID, variant ID to the form as values.
     $form['plugin_id'] = ['#type' => 'value', '#value' => $plugin_id];
     $form['variant_id'] = ['#type' => 'value', '#value' => $page_variant->id()];
     $form['uuid'] = ['#type' => 'value', '#value' => $uuid];
     // Add a select list for region assignment.
     $form['flipper']['front']['settings']['region'] = ['#title' => $this->t('Region'), '#type' => 'select', '#options' => $regions, '#required' => TRUE, '#default_value' => $region];
     // Add an add button, which is only used by our App.
     $form['submit'] = ['#type' => 'button', '#value' => $uuid ? $this->t('Update') : $this->t('Add'), '#ajax' => ['callback' => '::submitForm', 'wrapper' => 'panels-ipe-block-plugin-form-wrapper', 'method' => 'replace', 'progress' => ['type' => 'throbber', 'message' => '']]];
     // Add a preview button.
     $form['preview'] = ['#type' => 'button', '#value' => $this->t('Toggle Preview'), '#ajax' => ['callback' => '::submitPreview', 'wrapper' => 'panels-ipe-block-plugin-form-wrapper', 'method' => 'replace', 'progress' => ['type' => 'throbber', 'message' => '']]];
     return $form;
 }