override_node_options.module in Override Node Options 5
Same filename and directory in other branches
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
* Allow users to override the default publishing options for nodes they can
* edit without giving them the 'administer nodes' permission.
*/
/**
* Implements hook_perm().
*/
function override_node_options_perm() {
$node_types = array_keys(node_get_types('names'));
foreach ($node_types as $node_type) {
$node_type = check_plain($node_type);
$perms[] = 'override ' . $node_type . ' published option';
$perms[] = 'override ' . $node_type . ' promote to front page option';
$perms[] = 'override ' . $node_type . ' sticky option';
$perms[] = 'override ' . $node_type . ' revision option';
$perms[] = 'override ' . $node_type . ' authored on option';
$perms[] = 'override ' . $node_type . ' authored by option';
}
return $perms;
}
/**
* Implements hook_menu().
*/
function override_node_options_menu($may_cache) {
$items[] = array(
'path' => 'admin/settings/override_node_options',
'title' => t('Override node options'),
'description' => t('Allow non-admins to override the default publishing options for nodes they can edit'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'override_node_options_settings_form',
),
'access' => user_access('access administration pages'),
);
return $items;
}
/**
* Implements hook_form_alter().
*/
function override_node_options_form_alter($form_id, &$form) {
if (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id && !user_access('administer nodes')) {
// Get a copy of the current node object.
$node = $form['#node'];
// Core doesn't include the revision log element if the user does not have
// access to it, so we have to re-create it.
// @see http://drupal.org/node/668806
$form['log'] = array(
'#type' => 'textarea',
'#title' => t('Log message'),
'#rows' => 2,
'#weight' => 20,
'#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'),
'#access' => $node->revision || user_access('administer nodes') || user_access('override ' . $node->type . ' revision option'),
);
// Add access to the 'Authoring information' fieldset.
$form['author']['name']['#access'] = user_access('override ' . $node->type . ' authored by option');
$form['author']['date']['#access'] = user_access('override ' . $node->type . ' authored on option');
$form['author']['#access'] |= override_node_options_element_children_access($form['author']);
// Add access to the 'Publishing options' fieldset.
$form['options']['status']['#access'] = user_access('override ' . $node->type . ' published option');
$form['options']['promote']['#access'] = user_access('override ' . $node->type . ' promote to front page option');
$form['options']['sticky']['#access'] = user_access('override ' . $node->type . ' sticky option');
$form['options']['revision']['#access'] = user_access('override ' . $node->type . ' revision option');
$form['options']['#access'] |= override_node_options_element_children_access($form['options']);
// Add extra validation and processing if the user does not have the
// 'administer nodes' permission since it would be otherwise skipped by core.
if ($form['author']['#access']) {
$form['#validate']['override_node_options_validate_node'] = array();
}
if ($form['author']['name']['#access']) {
$form['#submit'] = array_merge(array(
'override_node_options_submit_node' => array(),
), $form['#submit']);
}
}
if (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id) {
$form['options']['#collapsible'] = variable_get('override_node_options_pocollapsible', 1);
$form['options']['#collapsed'] = variable_get('override_node_options_pocollapsed', 1);
$form['author']['#collapsible'] = variable_get('override_node_options_aicollapsible', 1);
$form['author']['#collapsed'] = variable_get('override_node_options_aicollapsed', 1);
if (module_exists('upload') && isset($form['attachments'])) {
$form['attachments']['#collapsible'] = variable_get('override_node_options_facollapsible', 1);
$form['attachments']['#collapsed'] = variable_get('override_node_options_facollapsed', 1);
}
if (module_exists('comment') && isset($form['comment_settings'])) {
$form['comment_settings']['#collapsible'] = variable_get('override_node_options_cscollapsible', 1);
$form['comment_settings']['#collapsed'] = variable_get('override_node_options_cscollapsed', 1);
}
if (module_exists('scheduler') && isset($form['scheduler_settings'])) {
$form['scheduler_settings']['#collapsible'] = variable_get('override_node_options_sscollapsible', 1);
$form['scheduler_settings']['#collapsed'] = variable_get('override_node_options_sscollapsed', 1);
}
}
}
/**
* Perform additional node form validation normally skipped by core.
*/
function override_node_options_validate_node($form_id, $form_values) {
// Validate the "authored by" field.
if (!empty($form_values['name']) && !($account = user_load(array(
'name' => $form_values['name'],
)))) {
// The use of empty() is mandatory in the context of usernames
// as the empty string denotes the anonymous user. In case we
// are dealing with an anonymous user we set the user ID to 0.
form_set_error('name', t('The username %name does not exist.', array(
'%name' => $form_values['name'],
)));
}
// Validate the "authored on" field. As of PHP 5.1.0, strtotime returns FALSE instead of -1 upon failure.
if (!empty($form_values['date']) && strtotime($form_values['date']) <= 0) {
form_set_error('date', t('You have to specify a valid date.'));
}
}
/**
* Perform additional node form submission processing normally skipped by core.
*/
function override_node_options_submit_node($form_id, $form_values) {
// Populate the "authored by" field.
if ($account = user_load(array(
'name' => $form_values['name'],
))) {
form_set_value(array(
'#parents' => array(
'uid',
),
), $account->uid);
}
else {
form_set_value(array(
'#parents' => array(
'uid',
),
), 0);
}
}
/**
* Check if there is a child element accessible in a parent element.
*
* @param $element
* A form element.
* @return
* TRUE if a child element of $element if accessible, or FALSE otherwise.
*/
function override_node_options_element_children_access($element) {
foreach (element_children($element) as $key) {
// Skip un-accessible children.
if (isset($element[$key]['#access']) && !$element[$key]['#access']) {
continue;
}
// Skip value and hidden elements, since they are not rendered.
if (isset($element[$key]['#type']) && in_array($element[$key]['#type'], array(
'value',
'hidden',
))) {
continue;
}
return TRUE;
}
return FALSE;
}
/**
* Administration settings form.
*
* @see system_settings_form()
*/
function override_node_options_settings_form() {
$form['attachments'] = array(
'#type' => 'fieldset',
'#title' => t('File attachments'),
'#description' => t('Options for the File attachments fieldset on the node form.'),
'#access' => module_exists('upload'),
);
$form['attachments']['override_node_options_facollapsible'] = array(
'#type' => 'checkbox',
'#title' => t('File attachments collapsible'),
'#default_value' => variable_get('override_node_options_facollapsible', 1),
);
$form['attachments']['override_node_options_facollapsed'] = array(
'#type' => 'checkbox',
'#title' => t('File attachments collapsed by default'),
'#default_value' => variable_get('override_node_options_facollapsed', 1),
);
$form['comment_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Comment settings'),
'#description' => t('Options for the Comment settings fieldset on the node form.'),
'#access' => module_exists('comment'),
);
$form['comment_settings']['override_node_options_cscollapsible'] = array(
'#type' => 'checkbox',
'#title' => t('Comment settings collapsible'),
'#default_value' => variable_get('override_node_options_cscollapsible', 1),
);
$form['comment_settings']['override_node_options_cscollapsed'] = array(
'#type' => 'checkbox',
'#title' => t('Comment settings collapsed by default'),
'#default_value' => variable_get('override_node_options_cscollapsed', 1),
);
$form['scheduler_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Scheduler settings'),
'#description' => t('Options for the Scheduler settings fieldset on the node form.'),
'#access' => module_exists('scheduler'),
);
$form['scheduler_settings']['override_node_options_sscollapsible'] = array(
'#type' => 'checkbox',
'#title' => t('Scheduler settings collapsible'),
'#default_value' => variable_get('override_node_options_sscollapsible', 1),
);
$form['scheduler_settings']['override_node_options_sscollapsed'] = array(
'#type' => 'checkbox',
'#title' => t('Scheduler settings collapsed by default'),
'#default_value' => variable_get('override_node_options_sscollapsed', 1),
);
$form['authoring_information'] = array(
'#type' => 'fieldset',
'#title' => t('Authoring information'),
'#description' => t('Options for the Authoring information fieldset on the node form.'),
);
$form['authoring_information']['override_node_options_aicollapsible'] = array(
'#type' => 'checkbox',
'#title' => t('Authoring information collapsible'),
'#default_value' => variable_get('override_node_options_aicollapsible', 1),
);
$form['authoring_information']['override_node_options_aicollapsed'] = array(
'#type' => 'checkbox',
'#title' => t('Authoring information collapsed by default'),
'#default_value' => variable_get('override_node_options_aicollapsed', 1),
);
$form['publishing_options'] = array(
'#type' => 'fieldset',
'#title' => t('Publishing options'),
'#description' => t('Options for the Publishing options fieldset on the node form.'),
);
$form['publishing_options']['override_node_options_pocollapsible'] = array(
'#type' => 'checkbox',
'#title' => t('Publishing options collapsible'),
'#default_value' => variable_get('override_node_options_pocollapsible', 1),
);
$form['publishing_options']['override_node_options_pocollapsed'] = array(
'#type' => 'checkbox',
'#title' => t('Publishing options collapsed by default'),
'#default_value' => variable_get('override_node_options_pocollapsed', 1),
);
return system_settings_form($form);
}
Functions
Name | Description |
---|---|
override_node_options_element_children_access | Check if there is a child element accessible in a parent element. |
override_node_options_form_alter | Implements hook_form_alter(). |
override_node_options_menu | Implements hook_menu(). |
override_node_options_perm | Implements hook_perm(). |
override_node_options_settings_form | Administration settings form. |
override_node_options_submit_node | Perform additional node form submission processing normally skipped by core. |
override_node_options_validate_node | Perform additional node form validation normally skipped by core. |