perms_fieldsets.module in Util 6.3
Same filename and directory in other branches
Set default collapsed/expanded state for Access Permissions
File
perms_fieldsets.moduleView source
<?php
/**
* @file
* Set default collapsed/expanded state for Access Permissions
*/
function perms_fieldsets_menu() {
$menu['admin/settings/util/apfm'] = array(
'title' => 'Access Permissions Fieldsets',
'description' => 'Manage Access Permissions Fieldsets',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'perms_fieldsets_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
);
return $menu;
}
// The theme registry.
function perms_fieldsets_theme() {
return array(
'perms_fieldsets_theme' => array(
'arguments' => array(
'form' => null,
),
),
);
}
function perms_fieldsets_settings() {
drupal_add_css(drupal_get_path('module', 'perms_fieldsets') . '/perms_fieldsets.css');
$form = array();
$form['link'] = array(
'#value' => l(t('Go to permissions admin page'), 'admin/user/permissions'),
);
$form['perms_fieldsets_collapsed'] = array(
'#type' => 'radios',
'#title' => t('Collapse all permissions fieldsets by default'),
'#description' => t('If you collapse all by default, new modules will be collapsed automatically unless you select them below.'),
'#default_value' => variable_get('perms_fieldsets_collapsed', 0),
'#options' => array(
t('Expand all by default'),
t('Collapse all by default'),
),
);
// Get all available modules that have a hook_perm.
$modules = drupal_map_assoc(array_intersect(module_implements('perm'), module_list()));
ksort($modules);
$form['modules'] = array(
'#type' => 'fieldset',
'#title' => t('Available modules'),
'#description' => t('Check the box to reverse the default collapsed state above.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['modules']['perms_fieldsets_cfg'] = array(
'#type' => 'checkboxes',
'#default_value' => variable_get('perms_fieldsets_cfg', array()),
'#options' => $modules,
);
$form['todo'] = array(
'#type' => 'fieldset',
'#title' => 'TODO',
);
$form['todo']['item1'] = array(
'#type' => 'markup',
'#value' => '<ul><li>Add per-role permission counter (administrator[3/10])</li></ul>',
);
return system_settings_form($form);
}
function perms_fieldsets_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'user_admin_perm':
// Neat idea to access the important settings directly from the admin page,
// saves page loads from navigating to site configuration etc etc.
$form['#theme'] = 'perms_fieldsets_theme';
break;
}
}
function theme_perms_fieldsets_theme($form) {
$fieldset_output = '<p>' . l(t('Set collapsed state'), 'admin/settings/util/apfm') . '</p>';
$header[] = array(
'data' => t('Permission'),
'width' => '100%',
);
foreach (element_children($form['role_names']) as $rid) {
if (is_array($form['role_names'][$rid])) {
$header[] = array(
'data' => drupal_render($form['role_names'][$rid]),
'class' => 'checkbox',
'nowrap' => 'nowrap',
);
}
}
// This is my custom fieldset-enabled form.
$fieldset_form = array();
$collapse_all = variable_get('perms_fieldsets_collapsed', 0);
$selected = array_filter(variable_get('perms_fieldsets_cfg', array()));
$roles = user_roles();
// Fieldset weight counter.
$fieldset_weight = 0;
// Module watcher.
// Because role information is not grouped, we need to emulate grouping.
// Watching this variable will help us determine which module's permissions are being processed now
$fieldset_module_watcher = '';
$fieldset_module_watcher_old = '';
foreach (element_children($form['permission']) as $key) {
// Don't take form control structures
if (is_array($form['permission'][$key])) {
$row = array();
// Module name.
if (is_numeric($key)) {
$fieldset_module_watcher = $form['permission'][$key]['#value'];
// Final decision on collapsing this field set.
if ($collapse_all) {
$collapsed = isset($selected[$fieldset_module_watcher]) ? FALSE : TRUE;
}
else {
$collapsed = isset($selected[$fieldset_module_watcher]) ? TRUE : FALSE;
}
$fieldset_form[$fieldset_module_watcher] = array(
'#type' => 'fieldset',
'#title' => '<a name="module-' . $fieldset_module_watcher . '">' . t('@module module', array(
'@module' => $fieldset_module_watcher,
)) . '</a>',
'#collapsible' => TRUE,
'#collapsed' => $collapsed,
'#weight' => ++$fieldset_weight,
);
$row[] = array(
'data' => t('@module module', array(
'@module' => drupal_render($form['permission'][$key]),
)),
'class' => 'module',
'id' => 'module-' . $form['permission'][$key]['#value'],
'colspan' => count($form['role_names']) + 1,
);
}
else {
$row[] = array(
'data' => drupal_render($form['permission'][$key]),
'class' => 'permission',
);
foreach (element_children($form['checkboxes']) as $rid) {
if (is_array($form['checkboxes'][$rid])) {
$row[] = array(
'data' => drupal_render($form['checkboxes'][$rid][$key]),
'class' => 'checkbox',
'title' => $roles[$rid] . ' : ' . t($key),
);
}
}
$fieldset_rows[] = $row;
}
if ($fieldset_module_watcher != $fieldset_module_watcher_old) {
// This should mean our module changed.
// But first check if this is not the first iteration where _old will be empty
// and always != the current _watcher.
if (!empty($fieldset_module_watcher_old)) {
// If we got here then it means that old module watcher was changed and
// this is not first iteration. so we must build the table.
$fieldset_form[$fieldset_module_watcher_old][$fieldset_module_watcher_old . 'perms'] = array(
'#type' => 'item',
'#value' => theme('table', $header, $fieldset_rows, array(
'id' => 'permissions',
)),
);
unset($fieldset_rows);
$fieldset_module_watcher_old = $fieldset_module_watcher;
}
else {
// Because this is first iteration we simply set the _watcher_old.
$fieldset_module_watcher_old = $fieldset_module_watcher;
}
}
$rows[] = $row;
}
}
// This step is necessary because the loop above exits before last module's permissions get
// added to our fieldsets.
// I dont like the whole approach on principle and would like a cleaner and more streamlined solution.
// @TODO: come up with a better algorithm to do this whole theme
$fieldset_form[$fieldset_module_watcher_old][$fieldset_module_watcher_old . 'perms'] = array(
'#type' => 'item',
'#value' => theme('table', $header, $fieldset_rows, array(
'id' => 'permissions',
)),
);
$fieldset_output .= drupal_render($fieldset_form);
$fieldset_output .= drupal_render($form);
return $fieldset_output;
}
Functions
Name![]() |
Description |
---|---|
perms_fieldsets_form_alter | |
perms_fieldsets_menu | @file Set default collapsed/expanded state for Access Permissions |
perms_fieldsets_settings | |
perms_fieldsets_theme | |
theme_perms_fieldsets_theme |