Actions and triggers (who knew?)

Posted on February 27, 2009 by agentrickard

Well, the folks who put Actions into D6 core did. But until yesterday, I had never bothered with actions. Then a client asked for the following:

For certain user roles, any new or edited posts of certain types need to be unpublished and queued for editorial review.

My first thought was: "Well, Drupal doesn't do that." But it does. Thanks to Actions. Here's a little code snippet for you, and it works.

/**
* Implements hook_perm().
*/
function custom_perm() {
$types = node_get_types();
foreach ($types as $type => $data) {
$array[] = "edit $type content without approval";
}
return $array;
}

/**
* Implementation of hook_action_info().
*/
function custom_action_info() {
return array(
'custom_unpublish_action' => array(
'type' => 'node',
'description' => t('Unpublish posts by untrusted users'),
'configurable' => FALSE,
'behavior' => array('changes_node_property'),
'hooks' => array(
'nodeapi' => array('presave'),
),
),
);
}

/**
* Implementation of a Drupal action.
* Sets the status of a node to 0, meaning unpublished,
* if created or edited by an untrusted user.
*/
function custom_unpublish_action(&$node, $context = array()) {
// If the user is allowed to edit, let them.
if (user_access("edit $node->type content without approval")
|| user_access('administer nodes')) {
return;
}
// Otherwise, unpublish.
$node->status = 0;
drupal_set_message(t('Your changes have been saved. This content
has been sent for administrator approval. Until it is approved,
this page has been removed from the web site.'));
watchdog('action', 'Set @type %title to unpublished.',
array('@type' => node_get_types('name', $node),
'%title' => $node->title));
}

And that's it. I can't believe it took me this long to exploit this feature.

And I just know that I'll learn all sorts of new tricks at DrupalCON next week, so see you there!

Comments

Ken, Can't you do this

February 27, 2009 by ultimike (not verified), 1 year 1 week ago
Comment: 222

Ken,

Can't you do this without any code at all using the Rules (formerly Workflow-NG in Drupal 5) module?

-mike

I too have just jumped on

February 27, 2009 by sirkitree (not verified), 1 year 1 week ago
Comment: 223

I too have just jumped on that bandwagon. Some really fun stuff with these. I'm actually re-writing one of my modules in order to take advantage of these core features. So much more efficient and less code to maintain overall. Fun Stuff!

@Ultimike I don't know.

February 27, 2009 by Ken Rickard (not verified), 1 year 1 week ago
Comment: 224

@Ultimike

I don't know. Not as far as I can see from glancing through the code. Though http://drupal.org/node/298480 suggests that it can. I think you would still need the custom_perm() section, however.

@Ken, Rules module allows

February 27, 2009 by Amitai (not verified), 1 year 1 week ago
Comment: 225

@Ken,
Rules module allows you to do it without coding.
1) You would create an event that is triggered after a content is saved.
2) Add a condition to check if the user has a certain role.
3) Add an action to unpublish content.
4) Add an action to add the content to the queue of nodequeue module.
5) Optional: You can export your rule and place it in a file - just like you would do with Views.

Go Rules! :)

@Amitai Problems: 2)

February 27, 2009 by Ken Rickard (not verified), 1 year 1 week ago
Comment: 226

@Amitai

Problems:

2) Role is not enough. Needs to check for a specific permission per node type per role.
4) NodeQueue has no place in the workflow. Why would I put it there?

And you've added 2 modules to overhead.

Though I like step 5.

I'm using Rules to do things

February 28, 2009 by nonsie (not verified), 1 year 1 week ago
Comment: 227

I'm using Rules to do things that are most often requested without requiring any extra modules:
1. notifying content authors of certain roles when comments get posted on their content
2. updating path aliases when terms created using NAT module are updated (we're using terms to create aliases in the first place).

2) Specific checks can be

February 28, 2009 by Amitai (not verified), 1 year 1 week ago
Comment: 228

2) Specific checks can be done with the PHP excecute action.
4) Sorry - I saw the word 'queue' so I mistakenly assumed nodequeue.

@Amitai Thanks. And then

February 28, 2009 by Ken Rickard (not verified), 1 year 1 week ago
Comment: 229

@Amitai

Thanks. And then you can export that PHP rule to code? So that's cool.

Yes, then you can export it

February 28, 2009 by Amitai (not verified), 1 year 1 week ago
Comment: 230

Yes, then you can export it or even bulk export all your rule configurations.

The great benefit of using rules is that it's already integrated with CCK, organic groups, token, and other important modules. worth checking :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <blockquote> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Speaking Engagements

Contact me for availability.

Contact

  • agentrickard [at] gmail [dot] com

Donations

My Amazon.com Wish List