10

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?

11

Check the Menu Item Extras module.

  • 1
    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 Oct 2 '17 at 14:04
  • +1 great for including today's date in the menu bar, for example – therobyouknow Nov 20 '18 at 17:41
11

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 HOOK_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];

  if ($entity_type->id() == 'menu_link_content') {
    $fields['FIELD_NAME'] = BaseFieldDefinition::create('FIELD_TYPE')
      ->setLabel(t('FIELD_LABEL'))
      ->setDescription(t('FIELD_DESCRIPTION '))
      ->setDisplayOptions('form',array('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.

  • 4
    You do not have to add fields via form alter. You can simply use the BaseFieldDefinition->setDisplayOptions() method: ...->setDisplayOptions('form', [ ... ]). – milkovsky Jan 5 '17 at 10:38
  • 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 Jan 31 '17 at 10:21
  • 4
    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 Jan 31 '17 at 14:52
  • What do you put in hook_form_menu_link_content_menu_link_content_form_alter() to add the field? – Matt Sep 19 '18 at 15:28
2

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.
  • 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 Jan 31 '17 at 10:19
1

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

-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.

  • 3
    Wrong. MenuLinkContent is a fieldable content entity. – milkovsky Jan 10 '17 at 14:03
  • How would you do to add new fields to MenuLinkContent? – MatthieuScarset Jan 20 '17 at 4:17
  • see the answers above. They both explain how to add new fields to MenuLinkContent. – milkovsky Jan 20 '17 at 12:33
  • 1
    Menu Item Extras do that drupal.org/project/menu_item_extras – Mykhailo Gurei Oct 24 '17 at 7:05
  • Ok, but MenuLinkContent is still NOT a fieldable content entity. – MatthieuScarset Nov 9 '18 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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