/** * Overrides EntityForm::form(). */ public function form(array $form, array &$form_state) { $menu_link = $this->entity; // Since menu_link_load() no longer returns a translated and access checked // item, do it here instead. _menu_link_translate($menu_link); $form['link_title'] = array('#type' => 'textfield', '#title' => t('Menu link title'), '#default_value' => $menu_link->link_title, '#description' => t('The text to be used for this link in the menu.'), '#required' => TRUE); foreach (array('link_path', 'mlid', 'module', 'has_children', 'options') as $key) { $form[$key] = array('#type' => 'value', '#value' => $menu_link->{$key}); } // Any item created or edited via this interface is considered "customized". $form['customized'] = array('#type' => 'value', '#value' => 1); // We are not using url() when constructing this path because it would add // $base_path. $path = $menu_link->link_path; if (isset($menu_link->options['query'])) { $path .= '?' . $this->urlGenerator->httpBuildQuery($menu_link->options['query']); } if (isset($menu_link->options['fragment'])) { $path .= '#' . $menu_link->options['fragment']; } if ($menu_link->module == 'menu_ui') { $form['link_path'] = array('#type' => 'textfield', '#title' => t('Path'), '#maxlength' => 255, '#default_value' => $path, '#description' => 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')), '#required' => TRUE); } else { $form['_path'] = array('#type' => 'item', '#title' => t('Path'), '#description' => l($menu_link->link_title, $menu_link->href, $menu_link->options)); } $form['description'] = array('#type' => 'textarea', '#title' => t('Description'), '#default_value' => isset($menu_link->options['attributes']['title']) ? $menu_link->options['attributes']['title'] : '', '#rows' => 1, '#description' => t('Shown when hovering over the menu link.')); $form['enabled'] = array('#type' => 'checkbox', '#title' => t('Enabled'), '#default_value' => !$menu_link->hidden, '#description' => t('Menu links that are not enabled will not be listed in any menu.')); $form['expanded'] = array('#type' => 'checkbox', '#title' => t('Show as expanded'), '#default_value' => $menu_link->expanded, '#description' => t('If selected and this menu link has children, the menu will always appear expanded.')); // Generate a list of possible parents (not including this link or descendants). $options = menu_ui_parent_options(menu_ui_get_menus(), $menu_link); $default = $menu_link->menu_name . ':' . $menu_link->plid; if (!isset($options[$default])) { $default = 'tools:0'; } $form['parent'] = array('#type' => 'select', '#title' => t('Parent link'), '#default_value' => $default, '#options' => $options, '#description' => t('The maximum depth for a link and all its children is fixed at !maxdepth. Some menu links may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)), '#attributes' => array('class' => array('menu-title-select'))); // Get number of items in menu so the weight selector is sized appropriately. $delta = $this->menuLinkStorage->countMenuLinks($menu_link->menu_name); $form['weight'] = array('#type' => 'weight', '#title' => t('Weight'), '#delta' => max($delta, 50), '#default_value' => $menu_link->weight, '#description' => t('Optional. In the menu, the heavier links will sink and the lighter links will be positioned nearer the top.')); // Language module allows to configure the menu link language independently // of the menu language. It also allows to optionally show the language // selector on the menu link form so that the language of each menu link can // be configured individually. if ($this->moduleHandler->moduleExists('language')) { $language_configuration = language_get_default_configuration('menu_link', $menu_link->bundle()); $default_langcode = $menu_link->isNew() ? $language_configuration['langcode'] : $menu_link->langcode; $language_show = $language_configuration['language_show']; } else { $default_langcode = $menu_link->isNew() ? entity_load('menu', $menu_link->menu_name)->language()->getId() : $menu_link->langcode; $language_show = FALSE; } $form['langcode'] = array('#type' => 'language_select', '#title' => t('Language'), '#languages' => LanguageInterface::STATE_ALL, '#default_value' => $default_langcode, '#access' => $language_show); return parent::form($form, $form_state, $menu_link); }
/** * Tests path-based URL generation. */ public function testPathBasedURLGeneration() { $base_path = '/subdir'; $base_url = 'http://www.example.com' . $base_path; $this->generator->setBasePath($base_path . '/'); $this->generator->setBaseUrl($base_url . '/'); foreach (array('', 'index.php/') as $script_path) { $this->generator->setScriptPath($script_path); foreach (array(FALSE, TRUE) as $absolute) { // Get the expected start of the path string. $base = ($absolute ? $base_url . '/' : $base_path . '/') . $script_path; $url = $base . 'node/123'; $result = $this->generator->generateFromPath('node/123', array('absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base . 'node/123#foo'; $result = $this->generator->generateFromPath('node/123', array('fragment' => 'foo', 'absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base . 'node/123?foo'; $result = $this->generator->generateFromPath('node/123', array('query' => array('foo' => NULL), 'absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base . 'node/123?foo=bar&bar=baz'; $result = $this->generator->generateFromPath('node/123', array('query' => array('foo' => 'bar', 'bar' => 'baz'), 'absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base . 'node/123?foo#bar'; $result = $this->generator->generateFromPath('node/123', array('query' => array('foo' => NULL), 'fragment' => 'bar', 'absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base; $result = $this->generator->generateFromPath('<front>', array('absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); } } }
/** * Asserts \Drupal\Core\Routing\UrlGenerator::generateFromRoute()'s output. * * @param $route_name * The route name to test. * @param array $route_parameters * The route parameters to test. * @param array $options * The options to test. * @param $expected_url * The expected generated URL string. * @param \Drupal\Core\Cache\CacheableMetadata $expected_cacheability * The expected generated cacheability metadata. */ protected function assertGenerateFromRoute($route_name, array $route_parameters, array $options, $expected_url, CacheableMetadata $expected_cacheability) { // First, test with $collect_cacheability_metadata set to the default value. $url = $this->generator->generateFromRoute($route_name, $route_parameters, $options); $this->assertSame($expected_url, $url); // Second, test with it set to TRUE. $generated_url = $this->generator->generateFromRoute($route_name, $route_parameters, $options, TRUE); $this->assertSame($expected_url, $generated_url->getGeneratedUrl()); $this->assertEquals($expected_cacheability, CacheableMetadata::createFromObject($generated_url)); }
/** * Tests path-based URL generation. */ public function testPathBasedURLGeneration() { $base_path = '/subdir'; $base_url = 'http://www.example.com' . $base_path; foreach (array('', 'index.php/') as $script_path) { foreach (array(FALSE, TRUE) as $absolute) { // Setup a fake request which looks like a Drupal installed under the // subdir "subdir" on the domain www.example.com. // To reproduce the values install Drupal like that and use a debugger. $server = ['SCRIPT_NAME' => '/subdir/index.php', 'SCRIPT_FILENAME' => $this->root . '/index.php', 'SERVER_NAME' => 'http://www.example.com']; $request = Request::create('/subdir/' . $script_path, 'GET', [], [], [], $server); $request->headers->set('host', ['www.example.com']); $this->requestStack->push($request); // Get the expected start of the path string. $base = ($absolute ? $base_url . '/' : $base_path . '/') . $script_path; $url = $base . 'node/123'; $result = $this->generator->generateFromPath('node/123', array('absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base . 'node/123#foo'; $result = $this->generator->generateFromPath('node/123', array('fragment' => 'foo', 'absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base . 'node/123?foo'; $result = $this->generator->generateFromPath('node/123', array('query' => array('foo' => NULL), 'absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base . 'node/123?foo=bar&bar=baz'; $result = $this->generator->generateFromPath('node/123', array('query' => array('foo' => 'bar', 'bar' => 'baz'), 'absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base . 'node/123?foo#bar'; $result = $this->generator->generateFromPath('node/123', array('query' => array('foo' => NULL), 'fragment' => 'bar', 'absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); $url = $base; $result = $this->generator->generateFromPath('<front>', array('absolute' => $absolute)); $this->assertEquals($url, $result, "{$url} == {$result}"); } } }