/** * Tests creating a Panels variant with the IPE. */ public function testPanelsIPE() { // Create new page. $this->drupalGet('admin/structure/page_manager/add'); $edit = ['id' => 'foo', 'label' => 'foo', 'path' => 'testing']; $this->drupalPostForm(NULL, $edit, 'Save'); // Add a Panels variant which uses the IPE. $this->clickLink('Add new variant'); $this->clickLink('Panels'); $edit = ['id' => 'panels_1', 'label' => 'Default', 'variant_settings[builder]' => 'ipe']; $this->drupalPostForm(NULL, $edit, 'Save'); /** @var \Drupal\page_manager\PageVariantInterface $page_variant */ $page_variant = PageVariant::load('panels_1'); /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display */ $panels_display = $page_variant->getVariantPlugin(); // Make sure the storage type and id were set to the right value. $this->assertEqual($panels_display->getStorageType(), 'page_manager'); $this->assertEqual($panels_display->getStorageId(), 'panels_1'); }
/** * {@inheritdoc} */ public function build(array $regions, array $contexts, LayoutInterface $layout = NULL) { // Load our PageVariant. /** @var \Drupal\page_manager\PageVariantInterface $page_variant */ $page_variant = \Drupal::request()->attributes->get('page_manager_page_variant'); // Check to see if the current user has permissions to use the IPE. $has_permission = $this->account->hasPermission('access panels in-place editing'); // Attach the Panels In-place editor library based on permissions. if ($page_variant && $has_permission) { // This flag tracks whether or not there are unsaved changes. $unsaved = FALSE; // If a temporary configuration for this variant exists, use it. $temp_store_key = 'variant.' . $page_variant->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { // Reload the PageVariant. This is required to set variant plugin // configuration correctly. $page_variant = PageVariant::load($page_variant->id()); /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $variant_plugin */ $variant_plugin = $page_variant->getVariantPlugin(); $variant_plugin->setConfiguration($variant_config); // Override our initial values with what's in TempStore. $layout = $variant_plugin->getLayout(); $regions = $variant_plugin->getRegionAssignments(); // Indicate that the user is viewing un-saved changes. $unsaved = TRUE; } $build = parent::build($regions, $contexts, $layout); foreach ($regions as $region => $blocks) { // Wrap each region with a unique class and data attribute. $region_name = Html::getClass("block-region-{$region}"); $build[$region]['#prefix'] = '<div class="' . $region_name . '" data-region-name="' . $region . '">'; $build[$region]['#suffix'] = '</div>'; if ($blocks) { foreach ($blocks as $block_id => $block) { $build[$region][$block_id]['#attributes']['data-block-id'] = $block_id; } } } // Attach the required settings and IPE. $build['#attached']['library'][] = 'panels_ipe/panels_ipe'; $build['#attached']['drupalSettings']['panels_ipe'] = $this->getDrupalSettings($regions, $layout, $page_variant); // Add our custom elements to the build. $build['#prefix'] = '<div id="panels-ipe-content">'; // Indicate if the current user is viewing temp store. $tray_classes = $unsaved ? 'unsaved' : ''; $build['#suffix'] = '</div><div id="panels-ipe-tray" class="' . $tray_classes . '"></div>'; } else { $build = parent::build($regions, $contexts, $layout); } return $build; }
/** * Finds a block based on its variant and block label. * * @param string $page_variant_id * The ID of the page variant entity. * @param string $block_label * The label of the block. * * @return \Drupal\Core\Block\BlockPluginInterface|null * Either a block plugin, or NULL. */ protected function findBlockByLabel($page_variant_id, $block_label) { if ($page_variant = PageVariant::load($page_variant_id)) { /** @var \Drupal\ctools\Plugin\BlockVariantInterface $variant_plugin */ $variant_plugin = $page_variant->getVariantPlugin(); foreach ($variant_plugin->getRegionAssignments() as $blocks) { /** @var \Drupal\Core\Block\BlockPluginInterface[] $blocks */ foreach ($blocks as $block) { if ($block->label() == $block_label) { return $block; } } } } return NULL; }
/** * 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($form_state) { /** @var \Drupal\page_manager\PageVariantInterface $page_variant */ $page_variant = PageVariant::load($form_state->getValue('variant_id')); // If a UUID is provided, the Block should already exist. if ($uuid = $form_state->getValue('uuid')) { /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $variant_plugin */ $variant_plugin = $page_variant->getVariantPlugin(); // If a temporary configuration for this variant exists, use it. $temp_store_key = 'variant.' . $page_variant->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { $variant_plugin->setConfiguration($variant_config); } // Load the existing 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($form_state->getValue('plugin_id')); } // Add context to the block. if ($block_instance instanceof ContextAwarePluginInterface) { $this->contextHandler->applyContextMapping($block_instance, $page_variant->getContexts()); } return $block_instance; }