override_node_options.module in Override Node Options 7
Same filename and directory in other branches
Main module file for override_node_options.
Allow users to override the default publishing options for nodes they can edit without giving them the 'administer nodes' permission.
File
override_node_options.moduleView source
<?php
/**
* @file
* Main module file for override_node_options.
*
* Allow users to override the default publishing options for nodes they can
* edit without giving them the 'administer nodes' permission.
*/
/**
* Implements hook_permission().
*/
function override_node_options_permission() {
// Global permissions which apply to all node types.
$permissions = array(
'administer override node options' => array(
'title' => t('Administer override node options.'),
),
);
$show_perms = variable_get('override_node_options_permissions', array(
'general',
'specific',
));
if (in_array('general', $show_perms, TRUE)) {
$permissions += array(
'override all published option' => array(
'title' => t('Override published option for all node types.'),
),
'override all promote to front page option' => array(
'title' => t('Override promote to front page option for all node types.'),
),
'override all sticky option' => array(
'title' => t('Override sticky option for all node types.'),
),
'override all revision option' => array(
'title' => t('Override revision option for all node types.'),
),
'enter all revision log entry' => array(
'title' => t('Enter revision log entry for all node types.'),
),
'override all authored on option' => array(
'title' => t('Override authored on option for all node types.'),
),
'override all authored by option' => array(
'title' => t('Override authored by option for all node types.'),
),
);
if (module_exists('comment')) {
$permissions += array(
'override all comment setting option' => array(
'title' => t('Override comment setting option for all node types.'),
),
);
}
}
if (in_array('specific', $show_perms, TRUE)) {
// Generate override node permissions for all applicable node types.
foreach (node_permissions_get_configured_types() as $type) {
$permissions += override_node_options_list_permissions($type);
}
}
return $permissions;
}
/**
* Helper function to generate override node permission list for a given type.
*
* @param string $type
* The machine-readable name of the node type.
*
* @return array
* An array of permission names and description.
*/
function override_node_options_list_permissions($type, $name = NULL) {
if (!$name) {
$name = node_type_get_name($type);
}
$type = check_plain($type);
$permissions = array(
"override {$type} published option" => array(
'title' => t('Override %type_name published option.', array(
'%type_name' => $name,
)),
),
"override {$type} promote to front page option" => array(
'title' => t('Override %type_name promote to front page option.', array(
'%type_name' => $name,
)),
),
"override {$type} sticky option" => array(
'title' => t('Override %type_name sticky option.', array(
'%type_name' => $name,
)),
),
"override {$type} revision option" => array(
'title' => t('Override %type_name revision option.', array(
'%type_name' => $name,
)),
),
"enter {$type} revision log entry" => array(
'title' => t('Enter %type_name revision log entry.', array(
'%type_name' => $name,
)),
),
"override {$type} authored on option" => array(
'title' => t('Override %type_name authored on option.', array(
'%type_name' => $name,
)),
),
"override {$type} authored by option" => array(
'title' => t('Override %type_name authored by option.', array(
'%type_name' => $name,
)),
),
);
if (module_exists('comment')) {
$permissions += array(
"override {$type} comment setting option" => array(
'title' => t('Override %type_name comment setting option.', array(
'%type_name' => $name,
)),
),
);
}
return $permissions;
}
/**
* Implements hook_menu().
*/
function override_node_options_menu() {
$items['admin/config/content/override-node-options'] = array(
'title' => 'Override Node Options Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'override_node_options_settings_form',
),
'access arguments' => array(
'administer override node options',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Settings form.
*/
function override_node_options_settings_form($form, &$form_values) {
$form = array();
$form['override_node_options_permissions'] = array(
'#type' => 'checkboxes',
'#title' => t('Provide the following permissions:'),
'#options' => array(
'general' => t('General permissions, accross all node types'),
'specific' => t('Specific permissions, for each individual node type'),
),
'#default_value' => variable_get('override_node_options_permissions', array(
'general',
'specific',
)),
);
$form['#submit'][] = 'override_node_options_settings_form_submit';
return system_settings_form($form);
}
/**
* Submit handler for settings form.
*/
function override_node_options_settings_form_submit(&$form, &$form_state) {
// Get old perms to compare.
$old_perms = variable_get('override_node_options_permissions', array(
'general',
'specific',
));
$new_perms = $form_state['values']['override_node_options_permissions'];
// Clean up saved permissions.
$role_names = user_roles();
$revoke = array();
$alert = FALSE;
if (!in_array('specific', $new_perms, TRUE) && in_array('specific', $old_perms, TRUE)) {
$alert = TRUE;
$permissions = array();
foreach (node_permissions_get_configured_types() as $type) {
$permissions += override_node_options_list_permissions($type);
}
foreach ($permissions as $permission => $description) {
$revoke[$permission] = FALSE;
}
// Be sure to clear the cache.
cache_clear_all();
}
if (!in_array('general', $new_perms, TRUE) && in_array('general', $old_perms, TRUE)) {
$alert = TRUE;
$revoke = array(
'override all published option' => FALSE,
'override all promote to front page option' => FALSE,
'override all sticky option' => FALSE,
'override all revision option' => FALSE,
'enter all revision log entry' => FALSE,
'override all authored on option' => FALSE,
'override all authored by option' => FALSE,
);
if (module_exists('comment')) {
$revoke['override all comment setting option'] = FALSE;
}
}
// Any specific grants not used anymore need to be deleted.
if (!empty($revoke)) {
foreach ($role_names as $rid => $name) {
user_role_change_permissions($rid, $revoke);
}
}
// Set a helpful message.
$message = 'Configuration saved.';
$arguments = array();
$status = 'status';
if ($alert) {
if (user_access('administer permissions')) {
$arguments = array(
'!permissions' => l(t('the permissions page'), 'admin/config/people/permissions', array(
'fragment' => 'module-override_node_options',
)),
);
$message .= t('Please visit !permissions and double check access.');
$status = 'warning';
}
else {
$message .= t('Please visit the permissions page and double check access.');
}
}
drupal_set_message(t($message, $arguments), $status);
}
/**
* Implements hook_form_alter().
*/
function override_node_options_form_alter(&$form, &$form_state, $form_id) {
if (!empty($form['#node_edit_form']) && !user_access('administer nodes')) {
// Get a copy of the current node object.
$node = $form['#node'];
// Add access to the 'Revision information: log message' field.
$form['revision_information']['log']['#access'] = user_access('enter ' . $node->type . ' revision log entry') || user_access('enter all revision log entry');
// Add access to the 'Revision information' fieldset.
$form['revision_information']['revision']['#access'] = user_access('override ' . $node->type . ' revision option') || user_access('override all revision option');
$form['revision_information']['#access'] = element_get_visible_children($form['revision_information']);
// Add access to the 'Authoring information' fieldset.
$form['author']['name']['#access'] = user_access('override ' . $node->type . ' authored by option') || user_access('override all authored by option');
$form['author']['date']['#access'] = user_access('override ' . $node->type . ' authored on option') || user_access('override all authored on option');
if (array_key_exists('#access', $form['author'])) {
$form['author']['#access'] |= element_get_visible_children($form['author']);
}
else {
$form['author']['#access'] = element_get_visible_children($form['author']);
}
// Add access to the 'Publishing options' fieldset.
$form['options']['status']['#access'] = user_access('override ' . $node->type . ' published option') || user_access('override all published option');
$form['options']['promote']['#access'] = user_access('override ' . $node->type . ' promote to front page option') || user_access('override all promote to front page option');
$form['options']['sticky']['#access'] = user_access('override ' . $node->type . ' sticky option') || user_access('override all sticky option');
// If access is granted for promote or sticky but not for status, show
// status but disable it.
// This keeps core's JS working, and correctly populates the vertical tab.
if ($form['options']['status']['#access'] == FALSE && ($form['options']['promote']['#access'] || $form['options']['sticky']['#access'])) {
$form['options']['status']['#access'] = TRUE;
$form['options']['status']['#disabled'] = TRUE;
}
if (array_key_exists('#access', $form['options'])) {
$form['options']['#access'] |= element_get_visible_children($form['options']);
}
else {
$form['options']['#access'] = element_get_visible_children($form['options']);
}
// Add access to the 'Comment settings' fieldset.
if (module_exists('comment') && isset($form['comment_settings'])) {
$form['comment_settings']['#access'] |= user_access('override ' . $node->type . ' comment setting option') || user_access('override all comment setting option');
}
if (!empty($form['#node_edit_form']) && !user_access('administer nodes')) {
$form['author']['#attached']['js'][1]['data']['anonymous'] = $form['author']['name']['#default_value'];
}
}
}
Functions
Name | Description |
---|---|
override_node_options_form_alter | Implements hook_form_alter(). |
override_node_options_list_permissions | Helper function to generate override node permission list for a given type. |
override_node_options_menu | Implements hook_menu(). |
override_node_options_permission | Implements hook_permission(). |
override_node_options_settings_form | Settings form. |
override_node_options_settings_form_submit | Submit handler for settings form. |