Skip to content

Instantly share code, notes, and snippets.

@fieke
Last active August 29, 2015 14:08
Show Gist options
  • Save fieke/8d537e19b35a55fceb0c to your computer and use it in GitHub Desktop.
Save fieke/8d537e19b35a55fceb0c to your computer and use it in GitHub Desktop.
Bulk update change author to siteowner
name = "Bulk update author"
description = "Bulk update the author of your nodes to siteowner."
package = "the AIM"
core = 7.x
<?php
/**
* Implementation of hook_help().
*/
function bulk_update_author_help($path, $arg) {
switch ($path) {
case 'admin/help#bulk_update_author':
$output = '<h3>' . t('Add extra bulk operation for content') . '</h3>';
$output .= '<p>Unpublished node created by admin are not accessable for siteowners.<br />Therefore we must change the author of the nodes to siteowner. This module adds the extra bulk update action...</p>';
$output .= '<ul>';
$output .= '<li>Activate the <a href="/admin/modules">module</a></li>';
$output .= '<li><a href="/admin/config/development/performance">Clear cache</a></li>';
$output .= '<li>Bulk update your <a href="/admin/content">nodes</a></li>';
$output .= '<li>Don\'t forget to desactivate the <a href="/admin/modules">module</a> when this magnificent piece of code did it\'s work</li>';
$output .= '</ul>';
$output .= '<p>Documentation of the hook: <a href="https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_operations/7">hook_node_operations()</a>';
return $output;
}
}
/**
* Implementation of hook_node_operations().
* Add siteowner users to list to easily change author to.
*/
function bulk_update_author_node_operations() {
// Get the correct role data
$role = user_role_load_by_name('siteowners');
// Get siteowner users
$query = db_select('users_roles', 'ur');
$query->innerJoin('users', 'u', 'ur.uid = u.uid AND ur.rid = :rid', array(':rid' => $role->rid));
$results = $query
->fields('ur', array('uid'))
->fields('u', array('name'))
->execute()
->fetchAllAssoc('uid');
$operations = array();
foreach($results as $uid => $result) {
$operations['author_update_so_' . $result->uid] = array(
'label' => t('Change author to !name', array('!name' => $result->name)),
'callback' => 'node_mass_update',
'callback arguments' => array('updates' => array('uid' => $result->uid)),
);
}
return $operations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment