/**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     drupal_set_message($this->t('The static context %label has been removed.', ['%label' => $this->pageVariant->getStaticContext($this->staticContext)['label']]));
     $this->pageVariant->removeStaticContext($this->staticContext);
     $this->pageVariant->save();
     $form_state->setRedirectUrl($this->getCancelUrl());
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $this->pageVariant->removeSelectionCondition($this->selectionCondition->getConfiguration()['uuid']);
     $this->pageVariant->save();
     drupal_set_message($this->t('The selection condition %name has been removed.', ['%name' => $this->selectionCondition->getPluginDefinition()['label']]));
     $form_state->setRedirectUrl($this->getCancelUrl());
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     parent::submitForm($form, $form_state);
     $configuration = $this->condition->getConfiguration();
     // If this selection condition is new, add it to the page.
     if (!isset($configuration['uuid'])) {
         $this->pageVariant->addSelectionCondition($configuration);
     }
     // Save the page entity.
     $this->pageVariant->save();
     $form_state->setRedirectUrl($this->pageVariant->toUrl('edit-form'));
 }
示例#4
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;
 }
 /**
  * @covers ::access
  */
 public function testAccess()
 {
     $this->storage->load('id_exists')->willReturn($this->pageVariant->reveal());
     $this->storage->load('doesnt_exist')->willReturn(NULL);
     $account = $this->prophesize(AccountInterface::class);
     $this->pageVariant->access('read', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
     $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager->reveal());
     // Test the access condition.
     $this->assertEquals(AccessResult::allowed(), $panels_storage->access('id_exists', 'read', $account->reveal()));
     // Should be forbidden if it doesn't exist.
     $this->assertEquals(AccessResult::forbidden(), $panels_storage->access('doesnt_exist', 'read', $account->reveal()));
 }
 /**
  * Compiles settings needed for the IPE to function.
  *
  * @param array $regions
  *   The render array representing regions.
  * @param \Drupal\layout_plugin\Plugin\Layout\LayoutInterface $layout
  *   The current layout.
  * @param \Drupal\page_manager\PageVariantInterface $page_variant
  *   The current path's page variant.
  *
  * @return array|bool
  *   An associative array representing the contents of drupalSettings, or
  *   FALSE if there was an error.
  */
 protected function getDrupalSettings(array $regions, LayoutInterface $layout, PageVariantInterface $page_variant)
 {
     $settings = ['regions' => []];
     // Add current block IDs to settings sorted by region.
     foreach ($regions as $region => $blocks) {
         $settings['regions'][$region] = ['name' => $region, 'label' => '', 'blocks' => []];
         if (!$blocks) {
             continue;
         }
         /** @var \Drupal\Core\Block\BlockPluginInterface[] $blocks */
         foreach ($blocks as $block_uuid => $block) {
             $configuration = $block->getConfiguration();
             $setting = ['uuid' => $block_uuid, 'label' => $block->label(), 'id' => $block->getPluginId()];
             $settings['regions'][$region]['blocks'][$block_uuid] = NestedArray::mergeDeep($configuration, $setting);
         }
     }
     // Add the layout information.
     $layout_definition = $layout->getPluginDefinition();
     $settings['layout'] = ['id' => $layout->getPluginId(), 'label' => $layout_definition['label'], 'original' => true];
     // Add the display variant's config.
     $settings['display_variant'] = ['label' => $page_variant->label(), 'id' => $page_variant->id(), 'uuid' => $page_variant->uuid()];
     return $settings;
 }
 /**
  * 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;
 }
 /**
  * Gets the variant plugin for this page variant entity.
  *
  * @return \Drupal\ctools\Plugin\BlockVariantInterface
  */
 protected function getVariantPlugin()
 {
     return $this->pageVariant->getVariantPlugin();
 }
示例#9
0
 /**
  * {@inheritdoc}
  */
 public function addVariant(PageVariantInterface $variant)
 {
     $this->variants[$variant->id()] = $variant;
     return $this;
 }
示例#10
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $this->entity->delete();
     drupal_set_message($this->t('The variant %label has been removed.', ['%label' => $this->entity->label()]));
     $form_state->setRedirectUrl($this->getCancelUrl());
 }
 /**
  * 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]);
 }
 /**
  * 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;
 }