/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $form['label'] = ['#type' => 'textfield', '#title' => $this->t('Label'), '#description' => $this->t('The label for this variant.'), '#default_value' => $this->entity->label() ?: (string) $this->getVariantPlugin()->adminLabel(), '#maxlength' => '255']; $form['id'] = ['#type' => 'machine_name', '#disabled' => !$this->entity->isNew(), '#default_value' => !$this->entity->isNew() ? $this->entity->id() : '', '#machine_name' => ['exists' => [$this, 'exists']]]; // Allow the variant to add to the form. $form['variant_settings'] = $this->getVariantPlugin()->buildConfigurationForm([], $form_state); $form['variant_settings']['#tree'] = TRUE; $form['actions'] = ['#type' => 'actions']; $form['actions']['submit'] = ['#type' => 'submit', '#value' => 'Add/Edit', '#button_type' => 'primary']; return $form; }
/** * {@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; }
/** * 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 addVariant(PageVariantInterface $variant) { $this->variants[$variant->id()] = $variant; return $this; }
/** * Removes any temporary changes to the variant. * * @param \Drupal\page_manager\PageVariantInterface $page_variant * The current Page Variant. * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function cancelPageVariant(PageVariantInterface $page_variant) { // If a temporary configuration for this variant exists, use it. $temp_store_key = 'variant.' . $page_variant->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { $this->tempStore->delete($temp_store_key); } // Return an empty JSON response. return new JsonResponse(); }
/** * Set Panels storage information on the variant, if it's a Panels variant. * * @param \Drupal\Core\Display\VariantInterface $variant_plugin */ protected function setPanelsStorage(VariantInterface $variant_plugin) { if ($variant_plugin instanceof PanelsDisplayVariant) { $variant_plugin->setStorage('page_manager', $this->entity->id()); } }
/** * 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; }
/** * 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; }