I need to automatically flag a node after it has been published. All nodes of a certain content type will be flagged.

In order to solve that problem, I have installed the Rules module hoping that I would be able to automatically flag the nodes that are created. Rules module does not come prepacked with flag actions. Is it possible to flag an item that has just been created?

link|improve this question

57% accept rate
feedback

3 Answers

up vote 1 down vote accepted

To flag a node when it is published, you need to:

  • In admin/config/workflow/rules you create a new rule selecting "After saving new content" as event;
  • you create a "Content is of type" condition selecting the content type you want to be flagged;
  • click on the "Add and" link that appears on the bottom of the conditions table;
  • on the "AND" row, click on "Add condition;"
  • create a new condition choosing "Content is published" as condition type;
  • add a new action selecting "Execute custom PHP code" as type;
  • use the following code as custom PHP code.

    $flag = flag_get_flag('published'); // Replace "published" with the name of the flag you want to add. $flag->flag('flag', $node->nid);

On Drupal 6, Rules exposed the custom actions set on admin/config/system/actions, but the version of Rules for Drupal 7 doesn't seem to have that possibility. Flags for Drupal 6, as far as I remember, exposed some actions to Rules, but I doesn't seem it does it on Drupal 7.
That is the reason I used a custom PHP action to flag the node.

link|improve this answer
Thank you! It works – Marcos Buarque Apr 10 '11 at 18:30
This can be done without custom PHP using Rules 2 – Citricguy Jan 31 at 4:40
feedback

FYI, after a lot of search I discovered that Rules2 support in Flag for Drupal7 is already in dev. I tried it and it works well!

There is also a patch as stated here: http://nodeone.se/blogg/learn-flag-with-nodeone-part-7

Not easy to google out! :P Bye!

link|improve this answer
feedback

Yes, this can be done with Rules 2 through the Rules UI without creating custom PHP.

I need to automatically flag a node after it has been published.

If you are also 'creating' the node in the same action you will need to modify your Rules actions to be able to flag the node without using custom PHP code.

Create a new rule and add your 'create new entity' action. See image below:

Add Create new entity action

Next you will need to save the entity (force immediate save) and then flag the entity. See image below:

enter image description here

Without first saving the entity I was receiving all kinds of SQL errors and such. The above settings work for me and does not require custom PHP.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.