Last active
August 29, 2015 14:17
-
-
Save henrytran9x/40f6f0050c1f9f8c1f8a to your computer and use it in GitHub Desktop.
Add custom Contextual Links to Drupal node teaser pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_permission(). | |
*/ | |
function YOUR_MODULE_permission() { | |
return array( | |
'YOUR MODULE article unpublish articles' => array( | |
'title' => t('YOUR MODULE Article Unpublish'), | |
), | |
); | |
} | |
/** | |
* Implements hook_menu(). | |
*/ | |
function YOUR_MODULE_menu() { | |
$items = array(); | |
$items['node/%node/article/unpublish'] = array( | |
'title' => 'Unpublish', | |
'access callback' => '_YOUR_MODULE_article_unpublish_access_check', | |
'access arguments' => array(1), | |
'page callback' => '_YOUR_MODULE_article_unpublish', | |
'page arguments' => array(1), | |
'type' => MENU_LOCAL_TASK, | |
'context' => MENU_CONTEXT_INLINE, | |
); | |
return $items; | |
} | |
/** | |
* Custom access check for node/%node/article/unpublish menu link. | |
* | |
* @param object $node Node object from menu argument. | |
* @return boolean TRUE if has access, FALSE otherwise. | |
*/ | |
function _YOUR_MODULE_article_unpublish_access_check($node) { | |
if ($node->type == 'article' && user_access('YOUR MODULE unpublish articles')) { | |
return TRUE; | |
} | |
return FALSE; | |
} | |
/** | |
* Callback action to unpublish article. | |
* | |
* @param object $node Node object. | |
*/ | |
function _YOUR_MODULE_article_unpublish($node) { | |
node_object_prepare($node); | |
$node->status = 0; | |
node_save($node); | |
watchdog('YOUR_MODULE', __FUNCTION__ . ' -- Unpublished article "%title" (NID: %nid).', array('%title' => $node->title, '%nid' => $node->nid), WATCHDOG_NOTICE); | |
$destination = drupal_get_destination(); | |
$destination = isset($destination['destination']) ? $destination['destination'] : '<front>'; | |
drupal_goto($destination); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment