Problem/Motivation

Now that menu links are content entities, it would be nice if we could work with them in views, and perform the same kind of operations with them that are available for taxonomy terms and term references, so we can do things such as:

In Drupal 7, this might have involved using the Taxonomy Menu or Menu to Taxonomy modules (synchronizing a vocabulary to a menu, and any menu links to the relevant term references), but in Drupal 8 this should no longer be necessary.

Some work was also done in the Menu Node Views module (which is used on 7000+ sites, so there seems to be at least some demand for this).

Proposed resolution

Allow for menu link content entities to work with views, including filters and relationships linking them to nodes.

Remaining tasks

Decide whether we want this
Further clarify the kind of functionality related to menu links we want
Write patch
Review

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Issue fork drupal-2777749

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

stefan.r created an issue. See original summary.

stefan.r’s picture

For linking back menu links to nodes, #2315773: Create a menu link field type/widget/formatter would be perfect.

stefan.r’s picture

Issue summary: View changes

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

SylvainM’s picture

You can easily add the support by implementing hook_entity_type_alter():

/** @var \Drupal\Core\Entity\EntityTypeInterface $menu_link_content */
$menu_link_content = $entity_types['menu_link_content'];
if (!$menu_link_content->hasHandlerClass('views_data')) {
  $menu_link_content->setHandlerClass('views_data', 'Drupal\views\EntityViewsData');
}

But, there is a notice when editing view:
Notice: Undefined index: title in Drupal\views\Plugin\views\HandlerBase->adminLabel() (line 158 of core/modules/views/src/Plugin/views/HandlerBase.php).
It can be fixed by attached patch.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Anybody’s picture

Hi @stefan.r,

I think #2804335: Provide menu item integration for views is closely related and seems to have the same objective. Would you like to read through it, perhaps merge both issues and add your guess about using views_menu_children_filter module as testspace for further implementation and features before writing a patch for core?

We should also discuss the implications from #2315773: Create a menu link field type/widget/formatter if that will ever be done...

What do you think?

Anybody’s picture

Using the patch from #7 plus the hook:

/**
 * Implements hook_entity_type_alter to add views menu-link support (without entity relations):
 *
 * @return void
 */
function MYMODULE_entity_type_alter(array &$entity_types)
{
  /** @var \Drupal\Core\Entity\EntityTypeInterface $menu_link_content */
  $menu_link_content = $entity_types['menu_link_content'];
  if (!$menu_link_content->hasHandlerClass('views_data')) {
    $menu_link_content->setHandlerClass('views_data', 'Drupal\views\EntityViewsData');
  }
}

works to enable views to display menu item entities standalone, BUT the problem is, that entity relationships don't exist with it and that's the main criteria for typical use cases. So menu items don't reference entities and also entities can't reference menu items.
So this is just a very very early starting point or for very simple use cases.

I guess there are two ways to go now:
a) Decide if #2315773: Create a menu link field type/widget/formatter (https://www.drupal.org/project/menu_link in contrib) will happen and if it will, we'd just have to extend what comes from the reference for better usability (see my comment #177 in that issue)

or

b) If the current logic is kept, I'd suggest to extend views_menu_children_filter including #2996145: Support non-content entities to not only support nodes, which wouldn't be enough in the long term view. Based on that module a core patch could be written if the module works well.

How do we proceed and how may decide on #2315773: Create a menu link field type/widget/formatter? Can we ask important core maintainers about that?

In the meantime a solution like #7 can help as quickfix?

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

somersoft’s picture

With a strong hint from the code in core/modules/block_content/src/BlockContentViewsData.php, using hook_views_data_alter() gives the ability to create a views relationship to the information held in menu_link_content_data.

/**
 * Implements hook_views_data_alter().
 *
 * We need to use this hook instead of hook_views_data() as
 * entity API already provides data for our custom entity type.
 */
function hook_views_data_alter(&$data) {
  // @todo EntityViewsData should add these relationships by default.
  //   https://www.drupal.org/node/2410275
  $data['menu_link_content_field_revision']['id']['relationship']['id'] = 'standard';
  $data['menu_link_content_field_revision']['id']['relationship']['base'] = 'menu_link_content_data';
  $data['menu_link_content_field_revision']['id']['relationship']['base field'] = 'id';
  $data['menu_link_content_field_revision']['id']['relationship']['title'] = t('Menu Link Content ID');
  $data['menu_link_content_field_revision']['id']['relationship']['label'] = t('Get the actual menu link content from a menu link content revision by id.');

  $data['menu_link_content_field_revision']['revision_id']['relationship']['id'] = 'standard';
  $data['menu_link_content_field_revision']['revision_id']['relationship']['base'] = 'menu_link_content_data';
  $data['menu_link_content_field_revision']['revision_id']['relationship']['base field'] = 'revision_id';
  $data['menu_link_content_field_revision']['revision_id']['relationship']['title'] = t('Menu Link Content Revision ID');
  $data['menu_link_content_field_revision']['revision_id']['relationship']['label'] = t('Get the actual menu link content from a menu link content revision by revision id.');
}

Thank you to the person who wrote that code. alexpott ?

somersoft’s picture

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Anybody’s picture

Title: Add menu link support into views » Add menu_link_content support into views
Steven Jones’s picture

Here's a patch that builds on the work from #7 essentially doing away with needing the hook implementation.

Anybody’s picture

Thanks @Steven Jones, looks good, and I think it's okay to set this Needs Review, but please keep in mind that at least in a follow-up issue we need to add relations as written in #11.

I guess this will also needs tests, but didn't check yet, if there are tests for all other views enabled entity types in Core? But I think so...

Status: Needs review » Needs work
Anybody’s picture

Created a MR to proceed from #23. Please do NOT create further patches, but instead work on the MR.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

hswong3i made their first commit to this issue’s fork.

hswong3i’s picture

#26 missing the additional core/modules/menu_link_content/src/MenuLinkContentViewsData.php

Additional MR for 10.2.x and 11.x had been created.

hswong3i’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Sounds interesting!

Will need some test coverage.

MR should be against 11.x too.