Beispiel #1
0
 /**
  * Returns instance of the layout plugin used by this page variant.
  *
  * @return \Drupal\layout\Plugin\Layout\LayoutInterface
  *   Layout plugin instance.
  */
 public function getLayout()
 {
     if (!isset($this->layout)) {
         $this->layout = Layout::layoutPluginManager()->createInstance($this->configuration['layout'], array());
     }
     return $this->layout;
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public function blockForm($form, FormStateInterface $form_state)
 {
     // Fish the page object from the form args.
     // Prevent serialization error.
     $form['admin_label']['#markup'] = (string) $form['admin_label']['#markup'];
     foreach ($form_state->getBuildInfo()['args'] as $arg) {
         if ($arg instanceof Page) {
             $this->page = $arg->getExecutable();
         }
     }
     $block_plugins = \Drupal::service('plugin.manager.block')->getDefinitionsForContexts($this->getContexts());
     $block_options = array();
     foreach ($block_plugins as $plugin_id => $block_definition) {
         $block_options[(string) $block_definition['category']][$plugin_id] = (string) $block_definition['admin_label'];
     }
     $widget_blocks = $form_state->hasValue(array('settings', 'blocks')) ? $form_state->getValue(array('settings', 'blocks')) : $this->configuration['blocks'];
     $layout = $form_state->hasValue(array('settings', 'layout')) ? $form_state->getValue(array('settings', 'layout')) : $this->configuration['layout'];
     $classes = $form_state->hasValue(array('settings', 'classes')) ? $form_state->getValue(array('settings', 'classes')) : $this->configuration['classes'];
     $ajax_properties = array('#ajax' => array('callback' => array($this, 'widgetBlockAJAXCallback'), 'wrapper' => 'widget-block-wrapper', 'effect' => 'fade'));
     $form = parent::blockForm($form, $form_state);
     $layouts = array();
     foreach (Layout::layoutPluginManager()->getDefinitions() as $id => $definition) {
         if ($definition['type'] == 'partial') {
             $layouts[$id] = $definition['label'];
         }
     }
     $form['layout'] = array('#type' => 'select', '#required' => TRUE, '#title' => t('Widget layout'), '#options' => $layouts, '#default_value' => $layout) + $ajax_properties;
     $form['blocks'] = array('#type' => 'container', '#prefix' => '<div id="widget-block-wrapper">', '#suffix' => '</div>');
     $form['classes'] = array('#type' => 'textfield', '#title' => t('CSS Classes'), '#default_value' => $classes);
     if (!$layout) {
         return $form;
     }
     if ($layout != $this->configuration['layout']) {
         $this->configuration['layout'] = $layout;
         $this->configuration['regions'] = NULL;
         $this->layoutRegionBag = NULL;
     }
     foreach ($this->getLayoutRegions() as $region_id => $region_definition) {
         $block_config = isset($widget_blocks[$region_id]) ? $widget_blocks[$region_id] : array();
         $form['blocks'][$region_id] = array('#type' => 'details', '#title' => $region_definition->getConfiguration()['label'], '#open' => TRUE);
         $form['blocks'][$region_id]['id'] = array('#type' => 'select', '#title' => t('Block'), '#options' => $block_options, '#default_value' => isset($block_config['id']) ? $block_config['id'] : NULL, '#empty_option' => t('- None -')) + $ajax_properties;
         $form['blocks'][$region_id]['region'] = array('#type' => 'value', '#value' => $region_id);
         if (!empty($block_config['id'])) {
             $block_plugin = \Drupal::service('plugin.manager.block')->createInstance($block_config['id'], $block_config);
             $form['blocks'][$region_id] += $block_plugin->buildConfigurationForm(array(), $form_state);
             if ($block_plugin instanceof ContextAwarePluginInterface) {
                 $form['blocks'][$region_id]['context_mapping'] = $this->addContextAssignmentElement($block_plugin, $this->getContexts());
             }
             // @todo Support per-block caching and visibility. Breaks UI right now.
             unset($form['blocks'][$region_id]['cache']);
             unset($form['blocks'][$region_id]['visibility']);
             unset($form['blocks'][$region_id]['visibility_tabs']);
         } else {
             //unset($form['blocks'][$region_id]);
         }
     }
     return $form;
 }
Beispiel #3
0
 /**
  * Returns current Layout plugin instance.
  *
  * @todo: allow for configuration to be saved (not just the pluginId).
  *
  * @return \Drupal\layout\Plugin\Layout\LayoutInterface
  */
 public function getLayout($reset = FALSE)
 {
     if (isset($this->layout) && !$reset) {
         return $this->layout;
     }
     $template_plugin_id = $this->getLayoutId();
     if (!$template_plugin_id) {
         throw new \Exception('Missing layout id');
     }
     $this->layout = Layout::layoutPluginManager()->createInstance($template_plugin_id);
     // @todo: we need to get some kind of reference for the nested plugins, see views' PluginBase::init().
     $this->layout->pageVariant = $this;
     return $this->layout;
 }