/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, PageVariantInterface $page_variant = NULL, $block_id = NULL) { $this->pageVariant = $page_variant; $this->block = $this->prepareBlock($block_id); $form_state->set('page_variant_id', $page_variant->id()); $form_state->set('block_id', $this->block->getConfiguration()['uuid']); $form['#tree'] = TRUE; $form['settings'] = $this->block->buildConfigurationForm([], $form_state); $form['settings']['id'] = ['#type' => 'value', '#value' => $this->block->getPluginId()]; $form['region'] = ['#title' => $this->t('Region'), '#type' => 'select', '#options' => $this->getVariantPlugin()->getRegionNames(), '#default_value' => $this->getVariantPlugin()->getRegionAssignment($this->block->getConfiguration()['uuid']), '#required' => TRUE]; if ($this->block instanceof ContextAwarePluginInterface) { $form['context_mapping'] = $this->addContextAssignmentElement($this->block, $this->pageVariant->getContexts()); } $form['actions']['submit'] = ['#type' => 'submit', '#value' => $this->submitText(), '#button_type' => 'primary']; return $form; }
/** * @covers ::load */ public function testLoad() { // Make sure that the contexts are passed down (or not). $this->pageVariant->getContexts()->willReturn([]); $this->panelsDisplay->setContexts([])->shouldBeCalledTimes(1); $this->storage->load('id_exists')->willReturn($this->pageVariant->reveal()); $this->storage->load('doesnt_exist')->willReturn(NULL); $this->storage->load('not_a_panel')->willReturn($this->pageVariantNotPanels->reveal()); $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager->reveal()); // Test the success condition. $this->assertSame($this->panelsDisplay->reveal(), $panels_storage->load('id_exists')); // Should be NULL if it doesn't exist. $this->assertNull($panels_storage->load('doesnt_exist')); // Should also be NULL if it's not a PanelsDisplayVariant. $this->assertNull($panels_storage->load('not_a_panel')); }
/** * Presents a list of blocks to add to the variant. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request. * @param \Drupal\page_manager\PageVariantInterface $page_variant * The page entity. * * @return array * The block selection page. */ public function selectBlock(Request $request, PageVariantInterface $page_variant) { // Add a section containing the available blocks to be added to the variant. $build = ['#type' => 'container', '#attached' => ['library' => ['core/drupal.ajax']]]; $available_plugins = $this->blockManager->getDefinitionsForContexts($page_variant->getContexts()); // Order by category, and then by admin label. $available_plugins = $this->blockManager->getSortedDefinitions($available_plugins); foreach ($available_plugins as $plugin_id => $plugin_definition) { // Make a section for each region. $category = $plugin_definition['category']; $category_key = 'category-' . $category; if (!isset($build[$category_key])) { $build[$category_key] = ['#type' => 'fieldgroup', '#title' => $category, 'content' => ['#theme' => 'links']]; } // Add a link for each available block within each region. $build[$category_key]['content']['#links'][$plugin_id] = ['title' => $plugin_definition['admin_label'], 'url' => Url::fromRoute('page_manager.variant_add_block', ['page' => $page_variant->get('page'), 'page_variant' => $page_variant->id(), 'block_id' => $plugin_id, 'region' => $request->query->get('region')]), 'attributes' => $this->getAjaxAttributes()]; } return $build; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, PageVariantInterface $page_variant = NULL, $condition_id = NULL) { $this->pageVariant = $page_variant; return parent::buildForm($form, $form_state, $condition_id, $page_variant->getContexts()); }
/** * 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); }
/** * Determines if a context with that name already exists. * * @param string $name * The context name. * * @return bool * TRUE if the format exists, FALSE otherwise. */ public function contextExists($name) { return isset($this->pageVariant->getContexts()[$name]); }