/** * {@inheritdoc} */ function getRegionDefinitions() { return Layout::getNormalizedLayoutRegionDefinitions($this->pluginDefinition['regions']); }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, array &$form_state) { $form = parent::buildConfigurationForm($form, $form_state); // Do not allow blocks to be added until the page variant has been saved. if (!$this->id()) { $form['layout'] = array('#title' => $this->t('Layout'), '#type' => 'select', '#options' => Layout::getLayoutOptions(), '#default_value' => NULL); return $form; } // Determine the page ID, used for links below. $page_id = $this->executable->getPage()->id(); // Set up the attributes used by a modal to prevent duplication later. $attributes = array('class' => array('use-ajax'), 'data-accepts' => 'application/vnd.drupal-modal', 'data-dialog-options' => Json::encode(array('width' => 'auto'))); $add_button_attributes = NestedArray::mergeDeep($attributes, array('class' => array('button', 'button--small', 'button-action'))); if ($block_assignments = $this->getRegionAssignments()) { // Build a table of all blocks used by this page variant. $form['block_section'] = array('#type' => 'details', '#title' => $this->t('Blocks'), '#open' => TRUE); $form['block_section']['add'] = array('#type' => 'link', '#title' => $this->t('Add new block'), '#route_name' => 'page_manager.display_variant_select_block', '#route_parameters' => array('page' => $page_id, 'display_variant_id' => $this->id()), '#attributes' => $add_button_attributes, '#attached' => array('library' => array('core/drupal.ajax'))); $form['block_section']['blocks'] = array('#type' => 'table', '#header' => array($this->t('Label'), $this->t('Plugin ID'), $this->t('Region'), $this->t('Weight'), $this->t('Operations')), '#empty' => $this->t('There are no regions for blocks.'), '#parents' => array('page_variant', 'blocks')); // Loop through the blocks per region. foreach ($block_assignments as $region => $blocks) { // Add a section for each region and allow blocks to be dragged between // them. $form['block_section']['blocks']['#tabledrag'][] = array('action' => 'match', 'relationship' => 'sibling', 'group' => 'block-region-select', 'subgroup' => 'block-region-' . $region, 'hidden' => FALSE); $form['block_section']['blocks']['#tabledrag'][] = array('action' => 'order', 'relationship' => 'sibling', 'group' => 'block-weight', 'subgroup' => 'block-weight-' . $region); $form['block_section']['blocks'][$region] = array('#attributes' => array('class' => array('region-title', 'region-title-' . $region), 'no_striping' => TRUE)); $form['block_section']['blocks'][$region]['title'] = array('#markup' => $this->getRegionName($region), '#wrapper_attributes' => array('colspan' => 5)); $form['block_section']['blocks'][$region . '-message'] = array('#attributes' => array('class' => array('region-message', 'region-' . $region . '-message', empty($blocks) ? 'region-empty' : 'region-populated'))); $form['block_section']['blocks'][$region . '-message']['message'] = array('#markup' => '<em>' . t('No blocks in this region') . '</em>', '#wrapper_attributes' => array('colspan' => 5)); /** @var $blocks \Drupal\block\BlockPluginInterface[] */ foreach ($blocks as $block_id => $block) { $row = array('#attributes' => array('class' => array('draggable'))); $row['label']['#markup'] = $block->label(); $row['id']['#markup'] = $block->getPluginId(); // Allow the region to be changed for each block. $row['region'] = array('#title' => $this->t('Region'), '#title_display' => 'invisible', '#type' => 'select', '#options' => $this->getRegionNames(), '#default_value' => $this->getRegionAssignment($block_id), '#attributes' => array('class' => array('block-region-select', 'block-region-' . $region))); // Allow the weight to be changed for each block. $configuration = $block->getConfiguration(); $row['weight'] = array('#type' => 'weight', '#default_value' => isset($configuration['weight']) ? $configuration['weight'] : 0, '#title' => t('Weight for @block block', array('@block' => $block->label())), '#title_display' => 'invisible', '#attributes' => array('class' => array('block-weight', 'block-weight-' . $region))); // Add the operation links. $operations = array(); $operations['edit'] = array('title' => $this->t('Edit'), 'route_name' => 'page_manager.display_variant_edit_block', 'route_parameters' => array('page' => $page_id, 'display_variant_id' => $this->id(), 'block_id' => $block_id), 'attributes' => $attributes); $operations['delete'] = array('title' => $this->t('Delete'), 'route_name' => 'page_manager.display_variant_delete_block', 'route_parameters' => array('page' => $page_id, 'display_variant_id' => $this->id(), 'block_id' => $block_id), 'attributes' => $attributes); $row['operations'] = array('#type' => 'operations', '#links' => $operations); $form['block_section']['blocks'][$block_id] = $row; } } } return $form; }
/** * {@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; }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, array &$form_state) { // Adding $adding_variant = !isset($this->configuration['layout']); $form = parent::buildConfigurationForm($form, $form_state); $form['layout'] = array('#title' => t('Layout'), '#type' => 'select', '#default_value' => $this->getLayoutId(), '#options' => Layout::getLayoutOptions(), '#disabled' => !$adding_variant, '#description' => t('Note: change a template would require salvaging blocks from disappearing regions. We will do that ... soon.'), '#required' => TRUE); $page = $form_state['build_info']['args'][0]; if (!$adding_variant) { $page_variant = $page->getVariant($form_state['build_info']['args'][1]); $page_variant->init($page->getExecutable()); $form['links'] = array('#type' => 'markup', '#markup' => l(t('Preview layout'), $page->get('path'), array('attributes' => array('target' => drupal_html_id($page->id()))))); // This is just a quick hack, we need some form of theme_layout_ui call. $form['blocks'] = array('#title' => t('Blocks'), '#markup' => '<label>' . t('Layout UI') . '</label>' . '<div class="layout-configure-form">' . '<div id="layout-app">' . '<div class="operations">' . '<a class="highlight-blocks" href="#blocks">' . $this->t('Focus on blocks') . '</a> ' . '<a class="highlight-regions" href="#regions">' . $this->t('Focus on regions') . '</a> ' . '</div>' . '<div class="layout-app-inner"></div>' . '</div>' . '</div>', '#default_value' => '', '#attached' => array('library' => array('page_layout/layout'), 'js' => array(array('data' => PageLayout::getLayoutPageVariantClientData($page_variant), 'type' => 'setting')))); } return $form; }