In MenuForm, the "add link" link should only be provided if MenuLinkContentAccessControlHandler->createAccess() is true

Created on 4 October 2023, 11 months ago
Updated 7 March 2024, 6 months ago

Problem/Motivation

When we go to the form for an empty menu without links, there's a message in the link table that says There are no menu links yet. <a href="add-link-url">Add Link</a>.

If we are using core without any contrib/custom, That "Add Link" link is not really a concern. But! It is not unheard of to restrict the ability to create menu links, while it still being able to access the menu form. Fortunately, it should be fairly easy to add a condition to determine if it is appropriate to provide this link

Doing this without dependency injection might look something like

$menu_link_access_handler = \Drupal::entityTypeManager()->getAccessControlHandler('menu_link_content')

if ($menu_link_access_handler->createAccess()) {
    $form['links']['#empty'] = $this->t('There are no menu links yet. <a href=":url">Add link</a>.', [
      ':url' => Url::fromRoute('entity.menu.add_link_form', ['menu' => $this->entity->id()], [
        'query' => ['destination' => $this->entity->toUrl('edit-form')->toString()],
      ])->toString(),
    ]);
} 
else {
    $form['links']['links']['#empty'] = t('There are no menu links yet.');

}

But the actual fix should inject the access handler.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Needs work

Version

11.0 πŸ”₯

Component
Menu systemΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States bnjmnm Ann Arbor, MI

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024