Add permission to show menu on node edit

Created on 30 October 2024, 22 days ago

Problem/Motivation

Out of the box Drupal will show menu UI on node edit if a role has the permission administer menu . If a user has view the administer theme the role will get the added benefit of seeing admin > structure > menu. This module now gives us granular control of what menus should be shown and how a user can interact with these menus.

One feature I feel is missing is the ability to see the menu UI on the node edit/create form. I feel like this is a pretty popular use case where you want a content creator to create content and assign it to a specific menu but you don't want them mucking about in the menu ui. Here is how I solved this locally but would love to see this as a future feature.

function foo_bar_form_node_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // Only check for simple menu perms if the user doesn't already have access.
  if (!$form['menu']['#access']) {
    // Get current user.
    $user = \Drupal::currentUser();
    $form['menu']['#access'] = !$user->hasPermission('add new links to main menu') ? AccessResult::forbidden() : AccessResult::allowed();
  }
}

Steps to reproduce

Proposed resolution

  1. Add a new permission that is Administer menu links on Node edit/create forms
  2. Update access checks

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States robpowell Boston

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024