/** * {@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; }
/** * 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; }
/** * 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()])); }