11

I need to build a main menu that is a subset of the regular menu. The design team thinks it would be better for user to have only the most important links in the main menu and not have everything. Some links are top level menu and some are sub menus. For this I was hoping to be able to add a field to the menu link item form as a drop down. When creating a link the user could use this dropdown to "promoteToMainMenuAsBigLink" or "promoteToMainMenuAsSmallLink" or leave none to not show the item on the main menu.

I'm looking for a similar solution as this Drupal 7 solution but for Drupal 8.

I was hoping to be able to add a column in the table menu_link_content_data beside 'expanded'. I was hoping not to have to record this information in an other table like shown in the d7 solution.

I was able to add a field to the link form in a custom module I build, just for test purpose I only added a text field to start with:

custom_menu.module:

function custom_menu_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if($form_id == 'menu_link_content_menu_link_content_form') {
    $form['promoteToMainMenu'] = array(
      '#type' => 'textfield',
      '#title' => t('Promote to main menu'),
      '#description' => t('Promote link menu to main menu.'),
      '#default_value' => '',
      '#weight' => -9
    );
  }
}

The field is indeed shown in the link form but it is never recorded.

Can someone help me with this?

1

7 Answers 7

16

Check the Menu Item Extras module.

4
  • 2
    This is definitely the way to go, as of v2.0 of this module turns menu links into fully fieldable entites. Better solution than Colossal menu because it enhances the core menu system instead of replacing it.
    – Hudri
    Commented Oct 2, 2017 at 14:04
  • 1
    +1 great for including today's date in the menu bar, for example Commented Nov 20, 2018 at 17:41
  • 1
    There is the article about all features lembergsolutions.com/blog/… Commented Aug 19, 2019 at 18:14
  • I don't think this is a good solution. You can only add additional fields to a menu, but you can't use existing fields that belong to the menu item entity?
    – AlxVallejo
    Commented May 8, 2020 at 19:37
12

You can use hook_entity_base_field_info to add an additional field to the menu_link_content entity with the following code:

/**
 * Implements hook_entity_base_field_info().
 */
function MYMODULE_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  $fields = [];

  if ($entity_type->id() == 'menu_link_content') {
    $fields['FIELD_NAME'] = \Drupal\Core\Field\BaseFieldDefinition::create('FIELD_TYPE')
      ->setLabel(t('FIELD_LABEL'))
      ->setDescription(t('FIELD_DESCRIPTION '))
      ->setDisplayOptions('form', ['type' => 'FIELD_TYPE', 'weight' => 0]);
  }

  return $fields;
}

See https://www.drupal.org/docs/8/api/entity-api/defining-and-using-content-entity-field-definitions for more details on BaseFieldDefinition.

After which, you'll need hook_form_menu_link_content_menu_link_content_form_alter() to add a field to the form.

5
  • 5
    You do not have to add fields via form alter. You can simply use the BaseFieldDefinition->setDisplayOptions() method: ...->setDisplayOptions('form', [ ... ]).
    – milkovsky
    Commented Jan 5, 2017 at 10:38
  • 1
    How I can create image field? I cannot find example with this field type and I need it. I can see image upload field when using this but when I try to save menu item I get: Drupal\Core\Entity\EntityStorageException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_menu_image__target_id' Also when I refresh page for add menu item form I see in logs: Notice: Undefined index: field_menu_image__height Notice: Undefined index: field_menu_image__width and so on... Any example for image field type? Thanks
    – blastoise
    Commented Jan 31, 2017 at 10:21
  • 5
    I've solved this problem. /update.php is not working so column is never crated. I've used drush to update db: drush updatedb --entity-updates
    – blastoise
    Commented Jan 31, 2017 at 14:52
  • 1
    What do you put in hook_form_menu_link_content_menu_link_content_form_alter() to add the field?
    – Amy
    Commented Sep 19, 2018 at 15:28
  • i used this but filed comes on menus but not store values i am using boolean filed can anyone help me
    – Naveen
    Commented Sep 19, 2022 at 11:30
3

Unfortunately there is indeed no fields UI for menu links by default in Drupal 8 as @MatthieuScarset said. But menu_link_content is a content entity in Drupal 8, so it is possible to add fields to it.

Option #1 Of course you can do it programmatically as @Decipher suggested.

Option #2 But there is another workaround to enable fields UI for menu links temporary:

  1. Open Drupal\menu_link_content\Entity\MenuLinkContent
  2. Add field_ui_base_route = "entity.menu.add_link_form" to the annotation
  3. Rebuild cache
  4. Visit /admin/structure/menu/manage/main/add/fields
  5. Manage all your fields, displays
  6. Export your site configuration (drush cex -y).
  7. Remove field_ui_base_route = "entity.menu.add_link_form" from the annotation at Drupal\menu_link_content\Entity\MenuLinkContent
  8. Rebuild cache.
1
  • 1
    I tried this approach. At first it looks good, but when I tried to create new field I get error: There was a problem creating field Menu image: Attempt to create a field 'field_menu_image' without a bundle. I'm trying to achieve this programmatically now, but it would be better if this could work also.
    – blastoise
    Commented Jan 31, 2017 at 10:19
3

You could use Colossal Menu to add the field instead of the core Menu module.

0

"Menu item extras" is one way to achieve this, as @Mykhailo Gurei suggested.

But in my case I had to add extra fields and to use Mega menu module. I was using "We mega menu" and with it "Menu item extras" is not working. Simply menu is not shown on front-end (some templates override issue).

So other option that works with "We mega menu" is "Menu item content fields":

https://www.drupal.org/project/menu_item_fields

Has the same functionality and doesn't have conflict with We mega menu module.

0

A newer solution could be menu item content fields

According to the project description its features are:

  • Add fields to menu items and sort their display.
  • Show fielded menu items with a block chosing the view mode.
  • Configure additional options on the formatter like the rel and target attributes.
  • Add more attributes with the Link attributes module.
  • Optionally have a field on the menu link entity that overrides the display mode.
    • Is up to the site builder to create it.
    • This field needs to store the string value of the display mode, for example: 'mega'.
-3

Unfortunately, Drupal 8 core developers have not made Menu links fieldable. So it is not possible to add fields to menu links for now.

The site builder "work around" is to create a custom vocabulary which can have custom fields.

6
  • 5
    Wrong. MenuLinkContent is a fieldable content entity.
    – milkovsky
    Commented Jan 10, 2017 at 14:03
  • 1
    How would you do to add new fields to MenuLinkContent?
    – Matthieu
    Commented Jan 20, 2017 at 4:17
  • 1
    see the answers above. They both explain how to add new fields to MenuLinkContent.
    – milkovsky
    Commented Jan 20, 2017 at 12:33
  • 2
    Menu Item Extras do that drupal.org/project/menu_item_extras Commented Oct 24, 2017 at 7:05
  • 1
    So to be clear @milkovsky True. MenuLinkContent is NOT a fieldable content entity.
    – Matthieu
    Commented Mar 19, 2019 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.