/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $configuration = $this->block->getConfiguration(); $this->megaMenu->removeBlock($form_state->get('link'), $configuration['id']); $this->megaMenu->save(); drupal_set_message($this->t('The @label block has been removed.', ['@label' => $configuration['label']])); $form_state->setRedirectUrl($this->getCancelUrl()); }
/** * Get a list of blocks that can be placed in a mega menu. * * @param Request $request * @param MegaMenuInterface $mega_menu * @return array */ public function blockLibrary(Request $request, MegaMenuInterface $mega_menu) { // Get the query parameters needed. $link = $request->query->get('link'); $region = $request->query->get('region'); // Only add blocks which work without any available context. $blocks = $this->blockManager->getDefinitionsForContexts($this->contextRepository->getAvailableContexts()); // Order by category, and then by admin label. $blocks = $this->blockManager->getSortedDefinitions($blocks); $build['filter'] = ['#type' => 'search', '#title' => $this->t('Filter'), '#title_display' => 'invisible', '#size' => 30, '#placeholder' => $this->t('Filter by block name'), '#attributes' => ['class' => ['block-filter-text'], 'data-element' => '.block-add-table', 'title' => $this->t('Enter a part of the block name to filter by.')]]; $headers = [$this->t('Block'), $this->t('Category'), $this->t('Operations')]; $build['blocks'] = ['#type' => 'table', '#header' => $headers, '#rows' => [], '#empty' => $this->t('No blocks available.'), '#attributes' => ['class' => ['block-add-table']]]; // Add each block definition to the table. foreach ($blocks as $block_id => $block) { $links = ['add' => ['title' => $this->t('Place block'), 'url' => Url::fromRoute('mega_menu.block_add', ['mega_menu' => $mega_menu->id(), 'block_id' => $block_id], ['query' => ['link' => $link, 'region' => $region]]), 'attributes' => ['class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 700])]]]; $build['blocks']['#rows'][] = ['title' => ['data' => ['#type' => 'inline_template', '#template' => '<div class="block-filter-text-source">{{ label }}</div>', '#context' => ['label' => $block['admin_label']]]], 'category' => ['data' => $block['category']], 'operations' => ['data' => ['#type' => 'operations', '#links' => $links]]]; } return $build; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $form['#attached']['library'][] = 'mega_menu/admin'; $form['general'] = ['#title' => $this->t('General details'), '#type' => 'fieldset']; $form['general']['label'] = ['#type' => 'textfield', '#title' => $this->t('Label'), '#description' => $this->t('Enter label for this mega menu.'), '#required' => TRUE, '#default_value' => $this->entity->label()]; $form['general']['name'] = ['#type' => 'machine_name', '#title' => $this->t('Machine name'), '#machine_name' => ['source' => ['general', 'label'], 'exists' => [$this, 'megaMenuExists']], '#default_value' => $this->entity->id(), '#disabled' => $this->entity->id() ? TRUE : FALSE]; $form['general']['menu'] = ['#type' => 'select', '#title' => $this->t('Menu'), '#description' => $this->t('The menu that should be used as a base for the mega menu.'), '#empty_option' => $this->t('- Select a menu -'), '#options' => $this->getMenusListOptions(), '#required' => TRUE, '#default_value' => $this->entity->getTargetMenu(), '#disabled' => $this->entity->getTargetMenu() ? TRUE : FALSE]; $form['general']['render_content_outside'] = ['#type' => 'checkbox', '#title' => $this->t('Render content outside of list'), '#description' => $this->t('Check this box if you want to render the mega menu dropdown content outside of the list.'), '#default_value' => $this->entity->get('render_content_outside')]; return $form; }
/** * Form submission handler. * * @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. */ public function submitForm(array &$form, FormStateInterface $form_state) { $settings = (new FormState())->setValues($form_state->getValue('settings')); // Call the plugin submit handler. $this->block->submitConfigurationForm($form, $settings); // Update the original form values. $form_state->setValue('settings', $settings->getValues()); // Add available contexts if this is a context aware block. if ($this->block instanceof ContextAwarePluginInterface) { $this->block->setContextMapping($form_state->getValue(['settings', 'context_mapping'], [])); } $link = $form_state->get('link'); $region = $form_state->get('region'); $configuration = array_merge($this->block->getConfiguration(), ['link' => $link, 'region' => $region]); if ($this->megaMenu->hasBlock($link, $configuration['id'])) { $this->megaMenu->updateBlock($link, $configuration['id'], $configuration); } else { $this->megaMenu->addBlock($link, $configuration['id'], $configuration); } $this->megaMenu->save(); $form_state->setRedirectUrl(Url::fromRoute('entity.mega_menu.edit_form', ['mega_menu' => $this->megaMenu->id()])); }
/** * Build the mega menu link/content tree. * * @param MegaMenuInterface $mega_menu * * @return array */ private function buildMegaMenuTree(MegaMenuInterface $mega_menu) { $tree = $this->loadMenuTree($mega_menu->getTargetMenu()); $build = $this->menuLinkTree->build($tree); $build['#mega_menu'] = $mega_menu; $cacheability = CacheableMetadata::createFromRenderArray($build); $cacheability->addCacheableDependency($mega_menu); // Add content from the mega menus to the link tree. foreach ($build['#items'] as $item_key => $item) { $safe_item_key = str_replace('.', '_', $item_key); $layout = $mega_menu->getLinkLayout($safe_item_key); if ($layout === MegaMenuInterface::NO_LAYOUT) { continue; } $build['#items'][$item_key]['attributes']['data-mega-menu-content-target'] = $item_key; /** @var LayoutInterface $layout_plugin */ $layout_plugin = $this->layoutPluginManager->createInstance($layout); $plugin_definition = $layout_plugin->getPluginDefinition(); // Build an array of the region names in the right order. $empty = array_fill_keys(array_keys($plugin_definition['region_names']), []); $full = $mega_menu->getBlocksByLink($safe_item_key)->getAllByRegion(); // Merge it with the actual values to maintain the ordering. $block_assignments = array_intersect_key(array_merge($empty, $full), $empty); $build['#items'][$item_key]['content'] = ['#prefix' => '<div data-mega-menu-content="' . $item_key . '" class="mega-menu-content">', '#suffix' => '</div>', '#theme' => $plugin_definition['theme'], '#settings' => [], '#layout' => $plugin_definition]; if (isset($plugin_definition['library'])) { $build['#items'][$item_key]['content']['#attached']['library'][] = $plugin_definition['library']; } foreach ($block_assignments as $region => $blocks) { $build['#items'][$item_key]['content'][$region] = []; /** @var \Drupal\Core\Block\BlockPluginInterface[] $blocks */ foreach ($blocks as $block_id => $block) { if ($block instanceof ContextAwarePluginInterface) { $contexts = $this->contextRepository->getRuntimeContexts($block->getContextMapping()); $this->contextHandler->applyContextMapping($block, $contexts); } // Make sure the user is allowed to view the block. $access = $block->access($this->account, TRUE); $cacheability->addCacheableDependency($access); // If the user is not allowed then do not render the block. if (!$access->isAllowed()) { continue; } $configuration = $block->getConfiguration(); // Create the render array for the block as a whole. // @see template_preprocess_block(). $block_build = ['#theme' => 'block', '#attributes' => [], '#weight' => $configuration['weight'], '#configuration' => $configuration, '#plugin_id' => $block->getPluginId(), '#base_plugin_id' => $block->getBaseId(), '#derivative_plugin_id' => $block->getDerivativeId(), '#block_plugin' => $block, '#pre_render' => [[$this, 'preRenderBlock']], '#cache' => ['keys' => ['mega_menu', $mega_menu->id(), 'block', $block_id], 'tags' => Cache::mergeTags($mega_menu->getCacheTags(), $block->getCacheTags()), 'contexts' => $block->getCacheContexts(), 'max-age' => $block->getCacheMaxAge()]]; $build['#items'][$item_key]['content'][$region][$block_id] = $block_build; $cacheability->addCacheableDependency($block); } } } $cacheability->applyTo($build); return $build; }