diff --git a/core/modules/menu_link/src/Plugin/Field/FieldType/MenuLinkItem.php b/core/modules/menu_link/src/Plugin/Field/FieldType/MenuLinkItem.php index e635e70..70ef782 100644 --- a/core/modules/menu_link/src/Plugin/Field/FieldType/MenuLinkItem.php +++ b/core/modules/menu_link/src/Plugin/Field/FieldType/MenuLinkItem.php @@ -90,6 +90,10 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel $definitions['menu_name'] = DataDefinition::create('string') ->setLabel(t('Menu')); + $definitions['title'] = DataDefinition::create('string') + ->setLabel(t('Menu link title')); + $definitions['description'] = DataDefinition::create('string') + ->setLabel(t('Menu link description')); $definitions['parent'] = DataDefinition::create('string') ->setLabel(t('Menu link parent')); $definitions['weight'] = DataDefinition::create('integer') @@ -110,6 +114,21 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'length' => 255, 'not null' => FALSE, ]; + + $schema['columns']['title'] = [ + 'description' => 'The menu link text.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + ]; + + $schema['columns']['description'] = [ + 'description' => 'The description of the menu link.', + 'type' => 'blob', + 'size' => 'big', + 'not null' => FALSE, + ]; + $schema['columns']['parent'] = [ 'description' => 'The parent of the menu link', 'type' => 'varchar', @@ -191,7 +210,8 @@ protected function getMenuPluginDefinition() { $menu_definition = []; $entity = $this->getEntity(); $menu_definition['id'] = $this->getPluginId(); - $menu_definition['title'] = $entity->label(); + $menu_definition['title'] = $this->values['title'] ?: $entity->label(); + $menu_definition['description'] = isset($this->values['description']) ? $this->values['description'] : ''; $menu_definition['menu_name'] = $this->values['menu_name']; $menu_definition['parent'] = $this->values['parent']; $menu_definition['weight'] = isset($this->values['weight']) ? $this->values['weight'] : 0; diff --git a/core/modules/menu_link/src/Plugin/Field/FieldWidget/MenuLinkWidget.php b/core/modules/menu_link/src/Plugin/Field/FieldWidget/MenuLinkWidget.php index 59ac4f6..1bff7f1 100644 --- a/core/modules/menu_link/src/Plugin/Field/FieldWidget/MenuLinkWidget.php +++ b/core/modules/menu_link/src/Plugin/Field/FieldWidget/MenuLinkWidget.php @@ -11,6 +11,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Menu\MenuLinkManagerInterface; use Drupal\Core\Menu\MenuParentFormSelectorInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; @@ -44,13 +45,21 @@ class MenuLinkWidget extends WidgetBase implements ContainerFactoryPluginInterfa protected $account; /** + * The menu link manager. + * + * @var \Drupal\Core\Menu\MenuLinkManagerInterface + */ + protected $menuLinkManager; + + /** * {@inheritdoc} */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, MenuParentFormSelectorInterface $menu_parent_selector, AccountInterface $account) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, MenuParentFormSelectorInterface $menu_parent_selector, AccountInterface $account, MenuLinkManagerInterface $menu_link_manager) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings); $this->menuParentSelector = $menu_parent_selector; $this->account = $account; + $this->menuLinkManager = $menu_link_manager; } /** @@ -64,7 +73,8 @@ public static function create(ContainerInterface $container, array $configuratio $configuration['settings'], $configuration['third_party_settings'], $container->get('menu.parent_form_selector'), - $container->get('current_user') + $container->get('current_user'), + $container->get('plugin.manager.menu.link') ); } @@ -80,8 +90,18 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $menu = !empty($items[$delta]->menu) ? $items[$delta]->menu : reset($menu_names); $parent = !empty($items[$delta]->parent) ? $items[$delta]->parent : ''; $default_menu_parent = "$menu:$parent"; + $default_title = isset($items[$delta]->title) ? $items[$delta]->title : NULL; + $default_description = isset($items[$delta]->description) ? $items[$delta]->description : NULL; if (!$this->account->hasPermission('administer menu')) { + $element['title'] = [ + '#type' => 'value', + '#value' => $default_title, + ]; + $element['description'] = [ + '#type' => 'value', + '#value' => $default_description, + ]; $element['weight'] = [ '#type' => 'value', '#value' => $default_weight, @@ -96,15 +116,32 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $items->getParent()->getValue(); $element['#pre_render'][] = [$this, 'preRenderMenuDetails']; + + $element['title'] = [ + '#type' => 'textfield', + '#title' => $this > t('Menu link title'), + '#default_value' => $default_title, + ]; + + $element['description'] = [ + '#type' => 'textarea', + '#title' => $this > t('Description'), + '#default_value' => $default_description, + '#rows' => 1, + '#description' => $this > t('Shown when hovering over the menu link.'), + ]; + + $plugin_id = $items[$delta]->getPluginId(); + $has_plugin = $plugin_id && $this->menuLinkManager->hasDefinition($plugin_id); $element['enabled'] = [ '#type' => 'checkbox', '#title' => $this->t('Provide a menu link'), - '#default_value' => (int) (bool) $items[$delta]->getPluginId(), + '#default_value' => (int) (bool) $has_plugin, ]; $element['menu'] = [ '#type' => 'details', '#title' => $this->t('Menu settings'), - '#open' => (bool) $items[$delta]->getPluginId(), + '#open' => (bool) $has_plugin, '#tree' => FALSE, '#weight' => $entity->getEntityTypeId() == 'node' ? -2 : 0, '#group' => $entity->getEntityTypeId() == 'node' ? 'advanced' : NULL, @@ -143,6 +180,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen */ public function preRenderMenuDetails($element) { $element['menu']['enabled'] = $element['enabled']; + $element['menu']['title'] = $element['title']; + $element['menu']['description'] = $element['description']; $element['menu']['menu_parent'] = $element['menu_parent']; $element['menu']['weight'] = $element['weight']; @@ -155,7 +194,7 @@ public function preRenderMenuDetails($element) { ]; } - unset($element['enabled'], $element['menu_parent'], $element['weight']); + unset($element['enabled'], $element['title'], $element['description'], $element['menu_parent'], $element['weight']); return $element; } diff --git a/core/modules/menu_ui/src/Tests/MenuNodeTest.php b/core/modules/menu_ui/src/Tests/MenuNodeTest.php index 6434a17..58c3ad9 100644 --- a/core/modules/menu_ui/src/Tests/MenuNodeTest.php +++ b/core/modules/menu_ui/src/Tests/MenuNodeTest.php @@ -122,13 +122,14 @@ function testMenuNodeFormWidget() { $this->assertNoLink($node_title); // Edit the node, enable the menu link setting, but skip the link title. + // We still expect to have a link to the entity label of the node. $edit = array( 'menu[0][enabled]' => 1, ); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that there is no link for the node. $this->drupalGet('test-page'); - $this->assertNoLink($node_title); + $this->assertLink($node->label()); // Edit the node and create a menu link. $edit = array( @@ -154,6 +155,7 @@ function testMenuNodeFormWidget() { // Add a menu link to the Administration menu. $node->menu->menu_name = 'admin'; + $node->menu->title = $this->randomMachineName(16); $node->save(); // Assert that disabled Administration menu is not shown on the @@ -172,6 +174,7 @@ function testMenuNodeFormWidget() { // Create a second node. $child_node = $this->drupalCreateNode(array('type' => 'article')); // Assign a menu link to the second node, being a child of the first one. + $child_node->menu->title = $this->randomMachineName(16); $child_node->menu->parent = $node->menu->first()->getMenuPluginId(); $child_node->menu->menu_name = $node->menu->menu_name; $child_node->save();