function menu_per_role_form_menu_link_content_form_alter in Menu Per Role 8
Implements hook_form_FORM_ID_alter().
Alter menu_link_content fields to hide extra fields on content.
File
- ./
menu_per_role.module, line 77
Code
function menu_per_role_form_menu_link_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$config = \Drupal::config('menu_per_role.settings');
// Get config properties.
$hide_show_mode = !is_null($config
->get('hide_show')) ? $config
->get('hide_show') : MenuPerRoleAdminSettings::MODE_DISPLAY_BOTH;
$hide_on_content_mode = !is_null($config
->get('hide_on_content')) ? $config
->get('hide_on_content') : MenuPerRoleAdminSettings::MODE_DISPLAY_ON_CONTENT_ALWAYS;
// Check if content mode setting applies, and if fields should be hidden.
$is_content = FALSE;
if ($hide_on_content_mode != MenuPerRoleAdminSettings::MODE_DISPLAY_ON_CONTENT_ALWAYS) {
/** @var \Drupal\menu_link_content\Form\MenuLinkContentForm $form_obj */
$form_obj = $form_state
->getFormObject();
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link */
$menu_link = $form_obj
->getEntity();
if (!$menu_link
->isNew()) {
$link_url = $menu_link
->getUrlObject();
if ($link_url
->isRouted()) {
$route_params = $link_url
->getRouteParameters();
if (array_key_exists('node', $route_params)) {
// Nodes routes will contain a 'node' with 'nid' value.
if ($hide_on_content_mode == MenuPerRoleAdminSettings::MODE_DISPLAY_ON_CONTENT_NO_NODE_ACCESS) {
// For the existence of any hook_node_grants() implementations.
$is_content = !empty(\Drupal::moduleHandler()
->getImplementations('node_grants'));
}
// If false, then check if the setting is display never.
$is_content = $is_content || $hide_on_content_mode == MenuPerRoleAdminSettings::MODE_DISPLAY_ON_CONTENT_NEVER;
}
}
}
}
// Check for the display of each field.
$display_show_roles = !$is_content && $hide_show_mode != MenuPerRoleAdminSettings::MODE_DISPLAY_ONLY_HIDE;
$display_hide_roles = !$is_content && $hide_show_mode != MenuPerRoleAdminSettings::MODE_DISPLAY_ONLY_SHOW;
// Hide fields if they need to be.
if (!$display_show_roles) {
$form['menu_per_role__show_role']['#access'] = FALSE;
}
if (!$display_hide_roles) {
$form['menu_per_role__hide_role']['#access'] = FALSE;
}
}