I was working on a project recently where we came across a very unique situation.
The Project required menu to be shown based on roles.These roles were tied with groups created earlier by previous developer team. Each department wanted to have complete access of the Drupal menu to configure (add/ edit/delete) along with drag & drop option within the department. This was to be accomplished without giving them admin access of project.
Menu creation needed to have a workflow where once menu has been added, it should be draft stage and must be reviewed & published by the head of the department to make it live. There was a need for adding additional fields (text & image) along with each menu item. This was to highlight the content of certain pages in menu drop-down itself.Addition & updation of menu was expected to happen in Drupal dialog (popup).
Challenges
- By default, there is no option in Drupal to create menu as per role. There is a contributed module available Menu per Role, but this can be configured only for roles whereas our need was to make it work with groups too.
- Since this is configuration entity (schema file) and not a content entity, no option was available to add extra fields in menu items
- By default, Drupal modal dialog opens the custom form in a pop-up and on submission of the form, you have to mention close dialog command to close the popup and submit the form. But requirement was to create new form (while adding up new menu item) without closing the modal.
Because of the above three scenarios, we created a custom module to enable the following functionalities.
- Department-wise Menu Creation Configuration
- Option to publish menu in Draft state
How we enabled role & group specific menu configuration
- Enable Roles & Groups
We created a custom page to list all the menu items:
Create a routing file
Fetch roles of current user
Fetch groups based on the role of that current user and give access to user based on specific role & groups
How we added extra fields in menu item\
Created custom entity
Created a custom table to link custom entity ID with menu ID:
Create a schema file for creating custom table in drupal 8
On creation of promo, add drupal insert query to add in custom table
3) How to create a new form without closing the modal
I added following custom ajax command to achieve this
Add UpdateMenuCommand.php file under module_name/src/Ajax folder
Then in js file, add like this:
Drupal.AjaxCommands.prototype.updateMenu key name mentioned in UpdateMenuCommand.php file.
Use the same in js file.
By following this procedure, we were able to create role based menu which worked with groups related permission. This can also be used when you need an extra field in the menu.
Feel Free to Contact Us for Your Enterprises