public function GroupPermissionsForm::buildForm in Group 8
Same name and namespace in other branches
- 2.0.x src/Form/GroupPermissionsForm.php \Drupal\group\Form\GroupPermissionsForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
2 calls to GroupPermissionsForm::buildForm()
- GroupPermissionsRoleSpecificForm::buildForm in src/
Form/ GroupPermissionsRoleSpecificForm.php - Form constructor.
- GroupPermissionsTypeSpecificForm::buildForm in src/
Form/ GroupPermissionsTypeSpecificForm.php - Form constructor.
2 methods override GroupPermissionsForm::buildForm()
- GroupPermissionsRoleSpecificForm::buildForm in src/
Form/ GroupPermissionsRoleSpecificForm.php - Form constructor.
- GroupPermissionsTypeSpecificForm::buildForm in src/
Form/ GroupPermissionsTypeSpecificForm.php - Form constructor.
File
- src/
Form/ GroupPermissionsForm.php, line 122
Class
- GroupPermissionsForm
- Provides the group permissions administration form.
Namespace
Drupal\group\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$role_info = [];
// Sort the group roles using the static sort() method.
// See \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
$group_roles = $this
->getGroupRoles();
uasort($group_roles, '\\Drupal\\group\\Entity\\GroupRole::sort');
// Retrieve information for every role to user further down. We do this to
// prevent the same methods from being fired (rows * permissions) times.
foreach ($group_roles as $role_name => $group_role) {
$role_info[$role_name] = [
'label' => $group_role
->label(),
'permissions' => $group_role
->getPermissions(),
'is_anonymous' => $group_role
->isAnonymous(),
'is_outsider' => $group_role
->isOutsider(),
'is_member' => $group_role
->isMember(),
];
}
// Render the general information.
if ($info = $this
->getInfo()) {
$form['info'] = $info;
}
// Render the link for hiding descriptions.
$form['system_compact_link'] = [
'#id' => FALSE,
'#type' => 'system_compact_link',
];
// Render the roles and permissions table.
$form['permissions'] = [
'#type' => 'table',
'#header' => [
$this
->t('Permission'),
],
'#id' => 'permissions',
'#attributes' => [
'class' => [
'permissions',
'js-permissions',
],
],
'#sticky' => TRUE,
];
// Create a column with header for every group role.
foreach ($role_info as $info) {
$form['permissions']['#header'][] = [
'data' => $info['label'],
'class' => [
'checkbox',
],
];
}
// Render the permission as sections of rows.
$hide_descriptions = system_admin_compact_mode();
foreach ($this
->getPermissions() as $provider => $sections) {
// Print a full width row containing the provider name for each provider.
$form['permissions'][$provider] = [
[
'#wrapper_attributes' => [
'colspan' => count($group_roles) + 1,
'class' => [
'module',
],
'id' => 'module-' . $provider,
],
'#markup' => $this->moduleHandler
->getName($provider),
],
];
foreach ($sections as $section => $permissions) {
// Create a clean section ID.
$section_id = $provider . '-' . preg_replace('/[^a-z0-9_]+/', '_', strtolower($section));
// Start each section with a full width row containing the section name.
$form['permissions'][$section_id] = [
[
'#wrapper_attributes' => [
'colspan' => count($group_roles) + 1,
'class' => [
'section',
],
'id' => 'section-' . $section_id,
],
'#markup' => $section,
],
];
// Then list all of the permissions for that provider and section.
foreach ($permissions as $perm => $perm_item) {
// Create a row for the permission, starting with the description cell.
$form['permissions'][$perm]['description'] = [
'#type' => 'inline_template',
'#template' => '<span class="title">{{ title }}</span>{% if description or warning %}<div class="description">{% if warning %}<em class="permission-warning">{{ warning }}</em><br />{% endif %}{{ description }}</div>{% endif %}',
'#context' => [
'title' => $perm_item['title'],
],
'#wrapper_attributes' => [
'class' => [
'permission',
],
],
];
// Show the permission description and warning if toggled on.
if (!$hide_descriptions) {
$form['permissions'][$perm]['description']['#context']['description'] = $perm_item['description'];
$form['permissions'][$perm]['description']['#context']['warning'] = $perm_item['warning'];
}
// Finally build a checkbox cell for every group role.
foreach ($role_info as $role_name => $info) {
// Determine whether the permission is available for this role.
$na = $info['is_anonymous'] && !in_array('anonymous', $perm_item['allowed for']);
$na = $na || $info['is_outsider'] && !in_array('outsider', $perm_item['allowed for']);
$na = $na || $info['is_member'] && !in_array('member', $perm_item['allowed for']);
// Show a red '-' if the permission is unavailable.
if ($na) {
$form['permissions'][$perm][$role_name] = [
'#title' => $info['label'] . ': ' . $perm_item['title'],
'#title_display' => 'invisible',
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
'style' => 'color: #ff0000;',
],
'#markup' => '-',
];
}
else {
$form['permissions'][$perm][$role_name] = [
'#title' => $info['label'] . ': ' . $perm_item['title'],
'#title_display' => 'invisible',
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
],
'#type' => 'checkbox',
'#default_value' => in_array($perm, $info['permissions']) ? 1 : 0,
'#attributes' => [
'class' => [
'rid-' . $role_name,
'js-rid-' . $role_name,
],
],
'#parents' => [
$role_name,
$perm,
],
];
}
}
}
}
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save permissions'),
'#button_type' => 'primary',
];
// @todo Do something like the global permissions page JS for 'member'.
// @todo See user/drupal.user.permissions for JS example.
$form['#attached']['library'][] = 'group/permissions';
return $form;
}