Issue #2416955 by dawehner, YesCT, amateescu, jibran, yched, anavarre: Convert MenuLinkContent to use a link widget

8.0.x
webchick 2015-01-31 07:01:58 -08:00
parent f977553177
commit ce199a18ff
6 changed files with 26 additions and 36 deletions

View File

@ -76,7 +76,7 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterf
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getUrlObject() { public function getUrlObject() {
return \Drupal::pathValidator()->getUrlIfValidWithoutAccessCheck($this->link->uri) ?: Url::fromRoute('<front>'); return $this->link->first()->getUrl();
} }
/** /**
@ -291,7 +291,10 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterf
'link_type' => LinkItemInterface::LINK_GENERIC, 'link_type' => LinkItemInterface::LINK_GENERIC,
'title' => DRUPAL_DISABLED, 'title' => DRUPAL_DISABLED,
)) ))
; ->setDisplayOptions('form', array(
'type' => 'link_default',
'weight' => -2,
));
$fields['external'] = BaseFieldDefinition::create('boolean') $fields['external'] = BaseFieldDefinition::create('boolean')
->setLabel(t('External')) ->setLabel(t('External'))
@ -337,7 +340,7 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterf
)) ))
->setDisplayOptions('form', array( ->setDisplayOptions('form', array(
'settings' => array('display_label' => TRUE), 'settings' => array('display_label' => TRUE),
'weight' => 0, 'weight' => -1,
)); ));
$fields['langcode'] = BaseFieldDefinition::create('language') $fields['langcode'] = BaseFieldDefinition::create('language')

View File

@ -193,7 +193,7 @@ class MenuLinkContentForm extends ContentEntityForm implements MenuLinkFormInter
$new_definition['route_parameters'] = []; $new_definition['route_parameters'] = [];
$new_definition['options'] = []; $new_definition['options'] = [];
$extracted = $this->pathValidator->getUrlIfValid($form_state->getValue('url')); $extracted = $this->pathValidator->getUrlIfValid($form_state->getValue(['link', 0, 'uri']));
if ($extracted) { if ($extracted) {
if ($extracted->isExternal()) { if ($extracted->isExternal()) {
@ -220,17 +220,6 @@ class MenuLinkContentForm extends ContentEntityForm implements MenuLinkFormInter
public function form(array $form, FormStateInterface $form_state) { public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state); $form = parent::form($form, $form_state);
$default_value = $this->getEntity()->link->uri;
$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,
);
$default = $this->entity->getMenuName() . ':' . $this->entity->getParentId(); $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId();
$form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $this->entity->getPluginId()); $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $this->entity->getPluginId());
$form['menu_parent']['#weight'] = 10; $form['menu_parent']['#weight'] = 10;
@ -274,8 +263,6 @@ class MenuLinkContentForm extends ContentEntityForm implements MenuLinkFormInter
$entity->enabled->value = (bool) $new_definition['enabled']; $entity->enabled->value = (bool) $new_definition['enabled'];
$entity->expanded->value = $new_definition['expanded']; $entity->expanded->value = $new_definition['expanded'];
$entity->link->uri = $form_state->getValue('url');
return $entity; return $entity;
} }
@ -312,10 +299,10 @@ class MenuLinkContentForm extends ContentEntityForm implements MenuLinkFormInter
* The current state of the form. * The current state of the form.
*/ */
protected function doValidate(array $form, FormStateInterface $form_state) { protected function doValidate(array $form, FormStateInterface $form_state) {
$extracted = $this->pathValidator->getUrlIfValid($form_state->getValue('url')); $extracted = $this->pathValidator->getUrlIfValid($form_state->getValue(['link', 0, 'uri']));
if (!$extracted) { if (!$extracted) {
$form_state->setErrorByName('url', $this->t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $form_state->getValue('url')))); $form_state->setErrorByName('link][0][uri', $this->t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $form_state->getValue(['link', 0, 'uri']))));
} }
} }

View File

@ -57,7 +57,7 @@ class MenuLinkContentUITest extends ContentTranslationUITest {
*/ */
protected function createEntity($values, $langcode, $bundle_name = NULL) { protected function createEntity($values, $langcode, $bundle_name = NULL) {
$values['menu_name'] = 'tools'; $values['menu_name'] = 'tools';
$values['link']['uri'] = 'admin/structure/menu'; $values['link']['uri'] = 'user-path:admin/structure/menu';
$values['title'] = 'Test title'; $values['title'] = 'Test title';
return parent::createEntity($values, $langcode, $bundle_name); return parent::createEntity($values, $langcode, $bundle_name);
@ -70,7 +70,7 @@ class MenuLinkContentUITest extends ContentTranslationUITest {
$this->drupalGet('admin/structure/menu/manage/tools'); $this->drupalGet('admin/structure/menu/manage/tools');
$this->assertNoLink(t('Translate')); $this->assertNoLink(t('Translate'));
$menu_link_content = MenuLinkContent::create(['menu_name' => 'tools', 'link' => ['uri' => 'admin/structure/menu']]); $menu_link_content = MenuLinkContent::create(['menu_name' => 'tools', 'link' => ['uri' => 'user-path:admin/structure/menu']]);
$menu_link_content->save(); $menu_link_content->save();
$this->drupalGet('admin/structure/menu/manage/tools'); $this->drupalGet('admin/structure/menu/manage/tools');
$this->assertLink(t('Translate')); $this->assertLink(t('Translate'));

View File

@ -76,7 +76,7 @@ class MenuLanguageTest extends MenuWebTestBase {
$link_title = $this->randomString(); $link_title = $this->randomString();
$edit = array( $edit = array(
'title[0][value]' => $link_title, 'title[0][value]' => $link_title,
'url' => $link_path, 'link[0][uri]' => $link_path,
); );
$this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save')); $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
// Check the link was added with the correct menu link default language. // Check the link was added with the correct menu link default language.
@ -98,7 +98,7 @@ class MenuLanguageTest extends MenuWebTestBase {
$link_title = $this->randomString(); $link_title = $this->randomString();
$edit = array( $edit = array(
'title[0][value]' => $link_title, 'title[0][value]' => $link_title,
'url' => $link_path, 'link[0][uri]' => $link_path,
); );
$this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save')); $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
// Check the link was added with the correct new menu link default language. // Check the link was added with the correct new menu link default language.

View File

@ -106,7 +106,7 @@ class MenuTest extends MenuWebTestBase {
foreach ($this->items as $item) { foreach ($this->items as $item) {
// Paths were set as 'node/$nid'. // Paths were set as 'node/$nid'.
$node = Node::load(str_replace('node/', '', $item->link->uri)); $node = Node::load(str_replace('user-path:node/', '', $item->link->uri));
$this->verifyMenuLink($item, $node); $this->verifyMenuLink($item, $node);
} }
@ -266,7 +266,7 @@ class MenuTest extends MenuWebTestBase {
$this->clickLink(t('Add link')); $this->clickLink(t('Add link'));
$link_title = $this->randomString(); $link_title = $this->randomString();
$this->drupalPostForm(NULL, array('url' => '<front>', 'title[0][value]' => $link_title), t('Save')); $this->drupalPostForm(NULL, array('link[0][uri]' => '<front>', 'title[0][value]' => $link_title), t('Save'));
$this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name])); $this->assertUrl(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));
// Test the 'Edit' operation. // Test the 'Edit' operation.
$this->clickLink(t('Edit')); $this->clickLink(t('Edit'));
@ -475,7 +475,7 @@ class MenuTest extends MenuWebTestBase {
$this->assertResponse(200); $this->assertResponse(200);
$this->assertFieldByName('title[0][value]', ''); $this->assertFieldByName('title[0][value]', '');
$this->assertFieldByName('url', ''); $this->assertFieldByName('link[0][uri]', '');
$this->assertNoFieldChecked('edit-expanded-value'); $this->assertNoFieldChecked('edit-expanded-value');
$this->assertFieldChecked('edit-enabled-value'); $this->assertFieldChecked('edit-enabled-value');
@ -495,13 +495,13 @@ class MenuTest extends MenuWebTestBase {
$item = $this->addMenuLink('', $path); $item = $this->addMenuLink('', $path);
$this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit'); $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
$this->assertFieldByName('url', $path, 'Path is found with both query and fragment.'); $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
// Now change the path to something without query and fragment. // Now change the path to something without query and fragment.
$path = 'test-page'; $path = 'test-page';
$this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', array('url' => $path), t('Save')); $this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', array('link[0][uri]' => $path), t('Save'));
$this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit'); $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
$this->assertFieldByName('url', $path, 'Path no longer has query or fragment.'); $this->assertFieldByName('link[0][uri]', $path, 'Path no longer has query or fragment.');
// Use <front>#fragment and ensure that saving it does not loose its // Use <front>#fragment and ensure that saving it does not loose its
// content. // content.
@ -509,12 +509,12 @@ class MenuTest extends MenuWebTestBase {
$item = $this->addMenuLink('', $path); $item = $this->addMenuLink('', $path);
$this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit'); $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
$this->assertFieldByName('url', $path, 'Path is found with both query and fragment.'); $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
$this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', array(), t('Save')); $this->drupalPostForm('admin/structure/menu/item/' . $item->id() . '/edit', array(), t('Save'));
$this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit'); $this->drupalGet('admin/structure/menu/item/' . $item->id() . '/edit');
$this->assertFieldByName('url', $path, 'Path is found with both query and fragment.'); $this->assertFieldByName('link[0][uri]', $path, 'Path is found with both query and fragment.');
} }
/** /**
@ -610,7 +610,7 @@ class MenuTest extends MenuWebTestBase {
$title = '!link_' . $this->randomMachineName(16); $title = '!link_' . $this->randomMachineName(16);
$edit = array( $edit = array(
'url' => $path, 'link[0][uri]' => $path,
'title[0][value]' => $title, 'title[0][value]' => $title,
'description[0][value]' => '', 'description[0][value]' => '',
'enabled[value]' => 1, 'enabled[value]' => 1,
@ -639,7 +639,7 @@ class MenuTest extends MenuWebTestBase {
function addInvalidMenuLink() { function addInvalidMenuLink() {
foreach (array('-&-', 'admin/people/permissions', '#') as $link_path) { foreach (array('-&-', 'admin/people/permissions', '#') as $link_path) {
$edit = array( $edit = array(
'url' => $link_path, 'link[0][uri]' => $link_path,
'title[0][value]' => 'title', 'title[0][value]' => 'title',
); );
$this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save')); $this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));

View File

@ -187,7 +187,7 @@ class BreadcrumbTest extends MenuTestBase {
$menu = 'tools'; $menu = 'tools';
$edit = array( $edit = array(
'title[0][value]' => 'Root', 'title[0][value]' => 'Root',
'url' => 'node', 'link[0][uri]' => 'node',
); );
$this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save')); $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
$menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => 'Root')); $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => 'Root'));
@ -240,7 +240,7 @@ class BreadcrumbTest extends MenuTestBase {
$term = $data['term']; $term = $data['term'];
$edit = array( $edit = array(
'title[0][value]' => "$name link", 'title[0][value]' => "$name link",
'url' => "taxonomy/term/{$term->id()}", 'link[0][uri]' => "taxonomy/term/{$term->id()}",
'menu_parent' => "$menu:{$parent_mlid}", 'menu_parent' => "$menu:{$parent_mlid}",
'enabled[value]' => 1, 'enabled[value]' => 1,
); );
@ -248,7 +248,7 @@ class BreadcrumbTest extends MenuTestBase {
$menu_links = entity_load_multiple_by_properties('menu_link_content', array( $menu_links = entity_load_multiple_by_properties('menu_link_content', array(
'title' => $edit['title[0][value]'], 'title' => $edit['title[0][value]'],
// @todo Use link.uri once https://www.drupal.org/node/2391217 is in. // @todo Use link.uri once https://www.drupal.org/node/2391217 is in.
'link__uri' => 'taxonomy/term/' . $term->id(), 'link__uri' => 'user-path:taxonomy/term/' . $term->id(),
)); ));
$tags[$name]['link'] = reset($menu_links); $tags[$name]['link'] = reset($menu_links);
$parent_mlid = $tags[$name]['link']->getPluginId(); $parent_mlid = $tags[$name]['link']->getPluginId();