-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrole.module
More file actions
113 lines (100 loc) · 3.62 KB
/
role.module
File metadata and controls
113 lines (100 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* @file
* Role control.
*/
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\role\Form\ControlRoleForm;
/**
* Implements hook_entity_type_alter().
*/
function role_entity_type_alter(array &$entity_types) {
if (isset($entity_types['user_role'])) {
/** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $user_role */
$user_role = &$entity_types['user_role'];
$user_role->setFormClass('default', ControlRoleForm::class);
}
}
/**
* Implements hook_entity_form_display_alter().
*/
function role_entity_form_display_alter(EntityFormDisplayInterface &$form_display, array $context) {
if ($context['entity_type'] === 'user' && $context['bundle'] === 'user') {
/** @var Drupal\role\Service\RoleControlManagerInterface $control_manager */
$control_manager = Drupal::service('role.control_manager');
/** @var \Drupal\user\Entity\User $user */
$user = Drupal::routeMatch()->getParameter('user');
if (!$user) {
return NULL;
}
if ($context['form_mode'] === 'default') {
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
$storage = Drupal::service('entity_type.manager')
->getStorage('entity_form_display');
if ($form_mode = $control_manager->getUserAccountFormMode($user)) {
$form_display = $storage->load($context['entity_type'] . '.' . $context['bundle'] . '.' . $form_mode);
}
}
}
}
/**
* Implements hook_entity_view_mode_alter().
*/
function role_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
if ($entity->bundle() === 'user') {
/** @var Drupal\role\Service\RoleControlManagerInterface $control_manager */
$control_manager = Drupal::service('role.control_manager');
/** @var \Drupal\user\Entity\User $user */
$user = Drupal::routeMatch()->getParameter('user');
if (!$user) {
return NULL;
}
($control_manager->getUserAccountViewMode($user) ? $view_mode = $control_manager->getUserAccountViewMode($user) : '');
}
}
/**
* Implements hook_form_alter().
*/
function role_form_alter(&$form, FormStateInterface $form_state, $form_id) {
switch ($form_id) {
case 'user_register_form':
case 'user_form':
$element = &NestedArray::getValue($form, ['account', 'roles']);
// Make sure this module also works when role_delegation is enabled.
if (isset($form['role_change'])) {
$element = &NestedArray::getValue($form, ['role_change', 'widget']);
}
_single_role_edit_element($element, $form);
break;
case 'role_delegation_role_assign_form':
_single_role_edit_element($form['account']['role_change'], $form);
break;
}
}
/**
* Helper function that alters the role form element.
*/
function _single_role_edit_element(&$element, &$form) {
/** @var \Drupal\Core\Config\ImmutableConfig $single_role_settings */
$single_role_settings = Drupal::config('single.role.settings');
if (!$single_role_settings->get('state')) {
return NULL;
}
$element['#type'] = $single_role_settings->get('field_type');
$element['#multiple'] = FALSE;
$element['#description'] = $single_role_settings->get('field_description');
$element['#title'] = t('Role');
$element[AccountInterface::AUTHENTICATED_ROLE]['#disabled'] = FALSE;
// Set default role.
foreach ($element['#default_value'] as $key => $value) {
if ($key !== 0) {
// Set first assigned role as default.
$element['#default_value'] = $value;
break;
}
}
}