/** * Gets all the available menus and menu items as a JavaScript array. * * @param \Symfony\Component\HttpFoundation\Request $request * The request of the page. * * @return \Symfony\Component\HttpFoundation\JsonResponse * The available menu and menu items. */ public function getParentOptions(Request $request) { $available_menus = array(); if ($menus = $request->request->get('menus')) { foreach ($menus as $menu) { $available_menus[$menu] = $menu; } } $options = $this->menuParentSelector->getParentSelectOptions('', $available_menus); return new JsonResponse($options); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId(); $id = $this->entity->isNew() ? '' : $this->entity->getPluginId(); $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id); $form['menu_parent']['#weight'] = 10; $form['menu_parent']['#title'] = $this->t('Parent link'); $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.'); $form['menu_parent']['#attributes']['class'][] = 'menu-title-select'; return $form; }
/** * Gets all the available menus and menu items as a JavaScript array. * * @param \Symfony\Component\HttpFoundation\Request $request * The request of the page. * * @return \Symfony\Component\HttpFoundation\JsonResponse * The available menu and menu items. */ public function getParentOptions(Request $request) { $available_menus = array(); if ($menus = $request->request->get('menus')) { foreach ($menus as $menu) { $available_menus[$menu] = $menu; } } // @todo Update this to use the optional $cacheability parameter, so that // a cacheable JSON response can be sent. $options = $this->menuParentSelector->getParentSelectOptions('', $available_menus); return new JsonResponse($options); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { parent::form($form, $form_state); $form['#title'] = $this->t('Edit menu link %title', ['%title' => $this->entity->getTitle()]); // Put the title field first. $form['title'] = ['#type' => 'textfield', '#title' => $this->t('Title'), '#default_value' => $this->entity->getTitle(), '#weight' => -10]; $form['id'] = ['#type' => 'machine_name', '#maxlength' => 128, '#machine_name' => ['source' => ['title'], 'exists' => '\\Drupal\\menu_link_config\\Controller\\MenuController::getMenuLink'], '#disabled' => !$this->entity->isNew(), '#weight' => -9]; $form['description'] = ['#type' => 'textfield', '#title' => $this->t('Description'), '#description' => $this->t('Shown when hovering over the menu link.'), '#default_value' => $this->entity->getDescription(), '#weight' => -5]; $link = ['#type' => 'link', '#title' => $this->entity->getTitle()] + $this->entity->getUrlObject()->toRenderArray(); $form['info'] = ['link' => $link, '#type' => 'item', '#title' => $this->t('Link')]; // We always show the internal path here. /** @var \Drupal\Core\Url $url */ $url = $this->entity->getUrlObject(); if ($url->isExternal()) { $default_value = $url->toString(); } elseif ($url->getRouteName() == '<front>') { // The default route for new entities is <front>, but we just want an // empty form field. $default_value = ''; } else { // @todo Url::getInternalPath() calls UrlGenerator::getPathFromRoute() // which need a replacement since it is deprecated. // https://www.drupal.org/node/2307061 try { $default_value = $url->getInternalPath(); } catch (\Exception $e) { $default_value = 'broken path'; } // @todo Add a helper method to Url to render just the query string and // fragment. https://www.drupal.org/node/2305013 $options = $url->getOptions(); if (isset($options['query'])) { $default_value .= $options['query'] ? '?' . UrlHelper::buildQuery($options['query']) : ''; } if (isset($options['fragment']) && $options['fragment'] !== '') { $default_value .= '#' . $options['fragment']; } } $form['url'] = ['#title' => $this->t('Link path'), '#type' => 'textfield', '#description' => $this->t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', ['%front' => '<front>', '%add-node' => '/node/add', '%drupal' => 'http://drupal.org']), '#default_value' => $default_value, '#required' => TRUE, '#weight' => -2]; $form['enabled'] = ['#type' => 'checkbox', '#title' => $this->t('Enable menu link'), '#description' => $this->t('Menu links that are not enabled will not be listed in any menu.'), '#default_value' => $this->entity->status()]; $form['expanded'] = ['#type' => 'checkbox', '#title' => t('Show as expanded'), '#description' => $this->t('If selected and this menu link has children, the menu will always appear expanded.'), '#default_value' => $this->entity->isExpanded()]; $menu_parent = $this->entity->getMenuName() . ':' . $this->entity->getParent(); $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($menu_parent, $this->entity->getPluginId()); $form['menu_parent']['#title'] = $this->t('Parent link'); $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.'); $form['menu_parent']['#attributes']['class'][] = 'menu-title-select'; $delta = max(abs($this->entity->getWeight()), 50); $form['weight'] = ['#type' => 'number', '#min' => -$delta, '#max' => $delta, '#default_value' => $this->entity->getWeight(), '#title' => $this->t('Weight'), '#description' => $this->t('Link weight among links in the same menu at the same depth. In the menu, the links with high weight will sink and links with a low weight will be positioned nearer the top.')]; return $form; }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['#title'] = $this->t('Edit menu link %title', array('%title' => $this->menuLink->getTitle())); $provider = $this->menuLink->getProvider(); $form['info'] = array('#type' => 'item', '#title' => $this->t('This link is provided by the @name module. The title and path cannot be edited.', array('@name' => $this->moduleHandler->getName($provider)))); $link = array('#type' => 'link', '#title' => $this->menuLink->getTitle(), '#url' => $this->menuLink->getUrlObject()); $form['path'] = array('link' => $link, '#type' => 'item', '#title' => $this->t('Link')); $form['enabled'] = array('#type' => 'checkbox', '#title' => $this->t('Enable menu link'), '#description' => $this->t('Menu links that are not enabled will not be listed in any menu.'), '#default_value' => $this->menuLink->isEnabled()); $form['expanded'] = array('#type' => 'checkbox', '#title' => t('Show as expanded'), '#description' => $this->t('If selected and this menu link has children, the menu will always appear expanded.'), '#default_value' => $this->menuLink->isExpanded()); $menu_parent = $this->menuLink->getMenuName() . ':' . $this->menuLink->getParent(); $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($menu_parent, $this->menuLink->getPluginId()); $form['menu_parent']['#title'] = $this->t('Parent link'); $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.'); $form['menu_parent']['#attributes']['class'][] = 'menu-title-select'; $delta = max(abs($this->menuLink->getWeight()), 50); $form['weight'] = array('#type' => 'number', '#min' => -$delta, '#max' => $delta, '#default_value' => $this->menuLink->getWeight(), '#title' => $this->t('Weight'), '#description' => $this->t('Link weight among links in the same menu at the same depth. In the menu, the links with high weight will sink and links with a low weight will be positioned nearer the top.')); return $form; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); // We always show the internal path here. /** @var \Drupal\Core\Url $url */ $url = $this->getEntity()->getUrlObject(); if ($url->isExternal()) { $default_value = $url->toString(); } elseif ($url->getRouteName() == '<front>') { // The default route for new entities is <front>, but we just want an // empty form field. $default_value = $this->getEntity()->isNew() ? '' : '<front>'; } else { // @todo Url::getInternalPath() calls UrlGenerator::getPathFromRoute() // which need a replacement since it is deprecated. // https://www.drupal.org/node/2307061 $default_value = $url->getInternalPath(); // @todo Add a helper method to Url to render just the query string and // fragment. https://www.drupal.org/node/2305013 $options = $url->getOptions(); if (isset($options['query'])) { $default_value .= $options['query'] ? '?' . UrlHelper::buildQuery($options['query']) : ''; } if (isset($options['fragment']) && $options['fragment'] !== '') { $default_value .= '#' . $options['fragment']; } } $form['url'] = array('#title' => $this->t('Link path'), '#type' => 'textfield', '#description' => $this->t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')), '#default_value' => $default_value, '#required' => TRUE, '#weight' => -2); $language_configuration = $this->moduleHandler->invoke('language', 'get_default_configuration', array('menu_link_content', 'menu_link_content')); if ($this->entity->isNew()) { $default_language = isset($language_configuration['langcode']) ? $language_configuration['langcode'] : $this->languageManager->getDefaultLanguage()->getId(); } else { $default_language = $this->entity->getUntranslated()->language()->getId(); } $form['langcode'] = array('#title' => t('Language'), '#type' => 'language_select', '#default_value' => $default_language, '#languages' => Language::STATE_ALL, '#access' => !empty($language_configuration['language_show'])); $form['enabled'] = array('#type' => 'checkbox', '#title' => $this->t('Enable menu link'), '#description' => $this->t('Menu links that are not enabled will not be listed in any menu.'), '#default_value' => !$this->entity->isHidden(), '#weight' => 0); $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId(); $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $this->entity->getPluginId()); $form['menu_parent']['#weight'] = 10; $form['menu_parent']['#title'] = $this->t('Parent link'); $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.'); $form['menu_parent']['#attributes']['class'][] = 'menu-title-select'; return $form; }