publishcontent.module in Publish Content 5.2
Same filename and directory in other branches
Add button to publish or unpublish a node, with access control based on the node type
File
publishcontent.moduleView source
<?php
/**
* @file
* Add button to publish or unpublish a node,
* with access control based on the node type
*/
/**
* Implementation of hook_menu().
*
* @see publishcontent_install()
*/
function publishcontent_menu($may_cache) {
$items = array();
if (!$may_cache && arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
if ($node->nid) {
$items[] = array(
'path' => 'node/' . arg(1) . '/publish/' . drupal_get_token(),
'title' => t('Publish'),
'callback' => 'publishcontent_toggle_status',
'callback arguments' => array(
$node,
),
'access' => !$node->status && _publishcontent_publish_access($node),
'weight' => 5,
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'node/' . arg(1) . '/unpublish/' . drupal_get_token(),
'title' => t('Unpublish'),
'callback' => 'publishcontent_toggle_status',
'callback arguments' => array(
$node,
),
'access' => $node->status && _publishcontent_unpublish_access($node),
'weight' => 5,
'type' => MENU_LOCAL_TASK,
);
// force the access to the node
if (!user_access('administer nodes')) {
$items[] = array(
'path' => 'node/' . arg(1),
'title' => t('View'),
'callback' => 'node_page_view',
'callback arguments' => array(
$node,
),
'access' => _publishcontent_view_access($node),
'type' => MENU_CALLBACK,
);
}
}
}
return $items;
}
function _publishcontent_view_access($node) {
return user_access(' publish *all* content') || user_access('unpublish *all* content') || user_access(' publish ' . check_plain($node->type) . ' content') || user_access('unpublish ' . check_plain($node->type) . ' content') || node_access('view', $node);
}
function _publishcontent_publish_access($node) {
global $user;
return !$node->status && (user_access(' publish *all* content') || user_access(' publish own ' . check_plain($node->type) . ' content', $user) && $user->uid == $node->uid || user_access(' publish ' . check_plain($node->type) . ' content'));
}
function _publishcontent_unpublish_access($node) {
global $user;
return $node->status && (user_access('unpublish *all* content') || user_access('unpublish own ' . check_plain($node->type) . ' content', $user) && $user->uid == $node->uid || user_access('unpublish ' . check_plain($node->type) . ' content'));
}
/**
* Implementation of hook_perm().
*
* @note: the 2 extra spaces is a hack: Drupal 5-6 user permissions page set
* 'publish *all* content' auto-magically when 'unpublish *all* content'
* is set.
*/
function publishcontent_perm() {
$perms = array(
' publish *all* content',
'unpublish *all* content',
);
foreach (node_get_types() as $type) {
if (isset($type->type)) {
$perms[] = ' publish ' . check_plain($type->type) . ' content';
$perms[] = ' publish own ' . check_plain($type->type) . ' content';
$perms[] = 'unpublish ' . check_plain($type->type) . ' content';
$perms[] = 'unpublish own ' . check_plain($type->type) . ' content';
}
}
return $perms;
}
function _publishcontent_get_message($nid, $title, $status) {
return t($status ? '"@title" [@nid] has been published' : '"@title" [@nid] has been unpublished', array(
'@title' => $title,
'@nid' => $nid,
));
}
/**
* @param $node a node object
*/
function publishcontent_toggle_status($node) {
$node->status = !$node->status;
node_save($node);
drupal_set_message(_publishcontent_get_message($node->nid, $node->title, $node->status));
drupal_goto('node/' . $node->nid);
}
/**
* Implementation of hook_simpletest().
*/
function publishcontent_simpletest() {
$dir = drupal_get_path('module', 'publishcontent') . '/tests';
$tests = file_scan_directory($dir, '\\.test$');
return array_keys($tests);
}
/**
* Implementation of hook_views_tables_alter().
* implements a a link to un/publish for views
*/
function publishcontent_views_tables_alter(&$tables) {
$tables['node']['fields']['publish'] = array(
'name' => t('Node: Publish link'),
'handler' => array(
'publishcontent_views_handler_node_publish_destination' => t('Return To View'),
'publishcontent_views_handler_node_publish' => t('Return to Node'),
),
'notafield' => TRUE,
'addlfields' => array(
'type',
'uid',
'status',
),
'help' => t('Display a link to publish the node.'),
);
}
/**
* display a link to publish a node
*/
function publishcontent_views_handler_node_publish($fieldinfo, $fielddata, $value, $data, $destination = NULL) {
// try to build a fake node object
$data->type = $data->node_type;
$data->uid = $data->node_uid;
$data->status = $data->node_status;
if ($data->status && _publishcontent_unpublish_access($data) || !$data->status && _publishcontent_publish_access($data)) {
return l(t($data->status ? 'Unpublish' : 'Publish'), "node/{$data->nid}/" . ($data->status ? 'unpublish/' : 'publish/') . drupal_get_token(), NULL, $destination);
}
}
/**
* display a link to edit a node with a destination return
*/
function publishcontent_views_handler_node_publish_destination($fieldinfo, $fielddata, $value, $data) {
return publishcontent_views_handler_node_publish($fieldinfo, $fielddata, $value, $data, drupal_get_destination());
}
/**
* Implementation of hook_form_alter()
*
* allow to use the 'Publishing options' on the edit/add page
*/
function publishcontent_form_alter($form_id, &$form) {
if (!user_access('administer nodes') && $form['type']['#value'] . '_node_form' == $form_id && (_publishcontent_unpublish_access($form['#node']) || _publishcontent_publish_access($form['#node']))) {
$form['options']['#access'] = TRUE;
unset($form['options']['promote']);
unset($form['options']['sticky']);
unset($form['options']['revision']);
}
}
Functions
Name![]() |
Description |
---|---|
publishcontent_form_alter | Implementation of hook_form_alter() |
publishcontent_menu | Implementation of hook_menu(). |
publishcontent_perm | Implementation of hook_perm(). |
publishcontent_simpletest | Implementation of hook_simpletest(). |
publishcontent_toggle_status | |
publishcontent_views_handler_node_publish | display a link to publish a node |
publishcontent_views_handler_node_publish_destination | display a link to edit a node with a destination return |
publishcontent_views_tables_alter | Implementation of hook_views_tables_alter(). implements a a link to un/publish for views |
_publishcontent_get_message | |
_publishcontent_publish_access | |
_publishcontent_unpublish_access | |
_publishcontent_view_access |