/** * Test getting layout options. * * @covers ::getLayoutOptions */ public function testGetLayoutOptions() { /** @var LayoutPluginManagerInterface|\PHPUnit_Framework_MockObject_MockBuilder $layout_plugin */ $layout_plugin = $this->getMock('Drupal\\layout_plugin\\Plugin\\Layout\\LayoutPluginManagerInterface'); $layout_plugin->method('getDefinitions')->willReturn(['simple_layout' => ['label' => 'Simple layout', 'category' => 'Test layouts'], 'complex_layout' => ['label' => 'Complex layout', 'category' => 'Test layouts']]); $options = Layout::getLayoutOptions(array(), $layout_plugin); $this->assertEquals(['simple_layout' => 'Simple layout', 'complex_layout' => 'Complex layout'], $options); $options = Layout::getLayoutOptions(array('group_by_category' => TRUE), $layout_plugin); $this->assertEquals(['Test layouts' => ['simple_layout' => 'Simple layout', 'complex_layout' => 'Complex layout']], $options); }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $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(array('group_by_category' => TRUE)), '#default_value' => NULL); return $form; } return $form; }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { // Don't call VariantBase::buildConfigurationForm() on purpose, because it // adds a 'Label' field that we don't actually want to use - we store the // label on the page variant entity. //$form = parent::buildConfigurationForm($form, $form_state); // Allow to configure the page title, even when adding a new display. // Default to the page label in that case. $form['page_title'] = ['#type' => 'textfield', '#title' => $this->t('Page title'), '#description' => $this->t('Configure the page title that will be used for this display.'), '#default_value' => $this->configuration['page_title'] ?: '']; if (empty($this->configuration['builder'])) { $plugins = $this->builderManager->getDefinitions(); $options = array(); foreach ($plugins as $id => $plugin) { $options[$id] = $plugin['label']; } // Only allow the IPE if the storage information is set. if (!$this->getStorageType()) { unset($options['ipe']); } $form['builder'] = ['#title' => $this->t('Builder'), '#type' => 'select', '#options' => $options, '#default_value' => 'standard']; } $form['layout'] = ['#title' => $this->t('Layout'), '#type' => 'select', '#options' => Layout::getLayoutOptions(['group_by_category' => TRUE]), '#default_value' => $this->configuration['layout'] ?: NULL]; if (!empty($this->configuration['layout'])) { $form['layout']['#ajax'] = ['callback' => [$this, 'layoutSettingsAjaxCallback'], 'wrapper' => 'layout-settings-wrapper', 'effect' => 'fade']; // If a layout is already selected, show the layout settings. $form['layout_settings_wrapper'] = ['#type' => 'fieldset', '#title' => $this->t('Layout settings'), '#prefix' => '<div id="layout-settings-wrapper">', '#suffix' => '</div>']; $form['layout_settings_wrapper']['layout_settings'] = []; // Process callback to configure #parents correctly on settings, since // we don't know where in the form hierarchy our settings appear. $form['#process'][] = [$this, 'layoutSettingsProcessCallback']; } return $form; }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); // Allow to configure the page title, even when adding a new display. // Default to the page label in that case. $form['page_title'] = ['#type' => 'textfield', '#title' => $this->t('Page title'), '#description' => $this->t('Configure the page title that will be used for this display.'), '#default_value' => $this->configuration['page_title'] ?: '']; if (empty($this->configuration['builder'])) { $plugins = $this->builderManager->getDefinitions(); $options = array(); foreach ($plugins as $id => $plugin) { $options[$id] = $plugin['label']; } $form['builder'] = ['#title' => $this->t('Builder'), '#type' => 'select', '#options' => $options, '#default_value' => 'standard']; } if (empty($this->configuration['layout'])) { $form['layout'] = ['#title' => $this->t('Layout'), '#type' => 'select', '#options' => Layout::getLayoutOptions(['group_by_category' => TRUE]), '#default_value' => NULL]; } else { $form['layout'] = ['#type' => 'value', '#value' => $this->configuration['layout']]; // If a layout is already selected, show the layout settings. $form['layout_settings_wrapper'] = ['#type' => 'fieldset', '#title' => $this->t('Layout settings')]; $form['layout_settings_wrapper']['layout_settings'] = []; // Get settings form from layout plugin. $layout = $this->layoutManager->createInstance($this->configuration['layout'], $this->configuration['layout_settings'] ?: []); $form['layout_settings_wrapper']['layout_settings'] = $layout->buildConfigurationForm($form['layout_settings_wrapper']['layout_settings'], $form_state); // Process callback to configure #parents correctly on settings, since // we don't know where in the form hierarchy our settings appear. $form['#process'][] = [$this, 'layoutSettingsProcessCallback']; } return $form; }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); // Allow to configure the page title, even when adding a new display. // Default to the page label in that case. $form['page_title'] = array('#type' => 'textfield', '#title' => $this->t('Page title'), '#description' => $this->t('Configure the page title that will be used for this display.'), '#default_value' => !$this->id() ? $this->executable->getPage()->label() : $this->configuration['page_title']); // Do not allow blocks to be added until the display variant has been saved. if (!$this->id()) { $form['layout'] = ['#title' => $this->t('Layout'), '#type' => 'select', '#options' => Layout::getLayoutOptions(['group_by_category' => TRUE]), '#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 = ['class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 'auto'])]; $add_button_attributes = NestedArray::mergeDeep($attributes, ['class' => ['button', 'button--small', 'button-action']]); if ($block_assignments = $this->getRegionAssignments()) { // Build a table of all blocks used by this display variant. $form['block_section'] = ['#type' => 'details', '#title' => $this->t('Blocks'), '#open' => TRUE]; $form['block_section']['add'] = ['#type' => 'link', '#title' => $this->t('Add new block'), '#url' => Url::fromRoute('page_manager.display_variant_select_block', ['page' => $page_id, 'display_variant_id' => $this->id()]), '#attributes' => $add_button_attributes, '#attached' => ['library' => ['core/drupal.ajax']]]; $form['block_section']['blocks'] = ['#type' => 'table', '#header' => [$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' => ['display_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'][] = ['action' => 'match', 'relationship' => 'sibling', 'group' => 'block-region-select', 'subgroup' => 'block-region-' . $region, 'hidden' => FALSE]; $form['block_section']['blocks']['#tabledrag'][] = ['action' => 'order', 'relationship' => 'sibling', 'group' => 'block-weight', 'subgroup' => 'block-weight-' . $region]; $form['block_section']['blocks'][$region] = ['#attributes' => ['class' => ['region-title', 'region-title-' . $region], 'no_striping' => TRUE]]; $form['block_section']['blocks'][$region]['title'] = ['#markup' => $this->getRegionName($region), '#wrapper_attributes' => ['colspan' => 5]]; $form['block_section']['blocks'][$region . '-message'] = ['#attributes' => ['class' => ['region-message', 'region-' . $region . '-message', empty($blocks) ? 'region-empty' : 'region-populated']]]; $form['block_section']['blocks'][$region . '-message']['message'] = ['#markup' => '<em>' . t('No blocks in this region') . '</em>', '#wrapper_attributes' => ['colspan' => 5]]; /** @var $blocks \Drupal\block\BlockPluginInterface[] */ foreach ($blocks as $block_id => $block) { $row = ['#attributes' => ['class' => ['draggable']]]; $row['label']['#markup'] = $block->label(); $row['id']['#markup'] = $block->getPluginId(); // Allow the region to be changed for each block. $row['region'] = ['#title' => $this->t('Region'), '#title_display' => 'invisible', '#type' => 'select', '#options' => $this->getRegionNames(), '#default_value' => $this->getRegionAssignment($block_id), '#attributes' => ['class' => ['block-region-select', 'block-region-' . $region]]]; // Allow the weight to be changed for each block. $configuration = $block->getConfiguration(); $row['weight'] = ['#type' => 'weight', '#default_value' => isset($configuration['weight']) ? $configuration['weight'] : 0, '#title' => t('Weight for @block block', ['@block' => $block->label()]), '#title_display' => 'invisible', '#attributes' => ['class' => ['block-weight', 'block-weight-' . $region]]]; // Add the operation links. $operations = []; $operations['edit'] = ['title' => $this->t('Edit'), 'url' => Url::fromRoute('page_manager.display_variant_edit_block', ['page' => $page_id, 'display_variant_id' => $this->id(), 'block_id' => $block_id]), 'attributes' => $attributes]; $operations['delete'] = ['title' => $this->t('Delete'), 'url' => Url::fromRoute('page_manager.display_variant_delete_block', ['page' => $page_id, 'display_variant_id' => $this->id(), 'block_id' => $block_id]), 'attributes' => $attributes]; $row['operations'] = ['#type' => 'operations', '#links' => $operations]; $form['block_section']['blocks'][$block_id] = $row; } } } return $form; }