Hello,
Recently, I found that Always visible is very useful when you want to appear a page without permissions to the main menu, but unfortunately it’s only available for D7.
I’m very new to Drupal module development, so as a first job I tried to migrate this module to D8. Although the source code is really small, most probably I miss some things and doesn’t seem to work.
First of all, I changed always_visible.info to always_visible.info.yml and the respective properties:
name: Always Visible
description: Allows administrators to override menu item visibility.
package: Custom
type: module
core: 8.x
In always_visible.module there are two parts for two different functionalities using hooks.
-
hook_form_alter(): Gives the opportunity to users to select which menu item will be appeared even if a user doesn’t have permission
Picture with the respective functionality in D7 site:
-
hook_translated_menu_link_alter(): The one that manipulates menu items according to user selection
I tried to figure out the changes for hook_form_alter() between the respective D7 and D8 and I noticed these two differences:
- I need to add \Drupal\Core\Form\FormStateInterface in front of &$form_state
- $form_id of Edit menu link page changed from ‘menu_edit_item’ to ’ menu-link-content-menu-link-content-form’
So, the always_visible.module looks like this:
<?php
//largely based on http://drupal.org/node/300607#comment-2805992
/**
* Implements hook_form_alter().
*/
function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
if ($form_id == 'menu-link-content-menu-link-content-form') {
$form['always_visible'] = array(
'#title' => t('Always show this menu item.'),
'#description' => t('Disables the check if the user has access to this path.'),
'#type' => 'checkbox',
'#weight' => '0',
'#default_value' => isset($form['options']['#value']['always_visible'])?$form['options']['#value']['always_visible']:FALSE,
);
$form['actions']['submit']['#submit'][] = 'always_visible_menu_edit_form_submit';
}
}
function always_visible_menu_edit_form_submit($form, \Drupal\Core\Form\FormStateInterface &$form_state) {
$menu =& $form_state['values'];
if ($menu['always_visible']) {
$menu['options']['always_visible'] = 1;
$menu['options']['alter'] = 1;
}
else {
$menu['options']['always_visible'] = 0;
$menu['options']['alter'] = 0;
}
}
The first functionality doesn’t seem to work because I can’t see the “check” button that appears in D7 site
As far as hook_translated_menu_link_alter() is concerned, I saw that it’s not exist in D8. Nevertheless, It seems like there are some alternatives, but I didn’t manage to implement it.
Any help will be more than useful,
Ioannis