menu_item_role_access.module in Menu Item Role Access 8
Same filename and directory in other branches
Menu Item Role Access module.
File
menu_item_role_access.moduleView source
<?php
/**
* @file
* Menu Item Role Access module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
/**
* Implements hook_entity_base_field_info().
*/
function menu_item_role_access_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type
->id() == 'menu_link_content') {
// Get config.
$config = \Drupal::config('menu_item_role_access.config');
$fields['menu_item_roles'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Menu Item Roles'))
->setDescription(t('Set the roles allowed for this menu item.'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'user_role')
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 3,
'settings' => [
'display_label' => TRUE,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['menu_item_override_children'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Override children'))
->setDescription(t('With this option checked the children will adhere to the above selected roles. Children may then again override their own children by setting this option. Please note that you may need to turn the option "Overwrite internal link target access check" on for this to work properly.'))
->setCardinality(1)
->setDisplayOptions('view', [
'label' => 'hidden',
])
->setDisplayOptions('form', [
'type' => 'checkbox',
'weight' => 4,
'settings' => [
'display_label' => TRUE,
],
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
}
return $fields;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function menu_item_role_access_form_menu_link_content_menu_link_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['menu_item_override_children'])) {
// If parent inheritance is enabled show the field.
$config = \Drupal::config('menu_item_role_access.config');
if (!$config
->get('inherit_parent_access')) {
$form['menu_item_override_children']['#access'] = FALSE;
}
}
}
/**
* Implements hook_entity_field_access().
*
* We want to make sure that fields aren't being edited by those who shouldn't.
*/
function menu_item_role_access_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
if ($field_definition
->getName() == 'menu_item_roles') {
if ($operation == 'edit' and $account
->hasPermission('edit menu_item_role_access')) {
return AccessResult::allowed();
}
// Anything else on this field is forbidden.
return AccessResult::forbidden();
}
return AccessResult::neutral();
}
Functions
Name | Description |
---|---|
menu_item_role_access_entity_base_field_info | Implements hook_entity_base_field_info(). |
menu_item_role_access_entity_field_access | Implements hook_entity_field_access(). |
menu_item_role_access_form_menu_link_content_menu_link_content_form_alter | Implements hook_form_FORM_ID_alter(). |