How can I make "on publish" be a trigger?

Events happening in the community are now at Drupal community events on www.drupal.org.
littlealy's picture

Is there a way I could do this with custom php code or something? I have hunted and hunted and all I can find is people who need help PUBLISHING, not using "on publish" as a trigger.

Basically, I need to have a rule that activates when a node is published. Not whenever a node is published, but specifically at the time it goes from being "unpublished" to "published." I thought the built in "Content is published" condition would do it, but nope, that just makes the action activate every time a published content is edited.

Basically I need:

ON content gets published
DO (my action here.)

Thanks for any help anyone can offer. Please try and make it simple/thorough, I'm quite new to Drupal still.

Comments

Maybe I found what I need?

littlealy's picture

After more searching, it seems the best way is to write some custom code to actually create the event.... and it seems this person tried to do just that: http://groups.drupal.org/node/42150

Is that the easiest/best way to go about this?

You don't need custom

wjaspers's picture

You don't need custom code.
Create a rule with the event "On Node Save(d)".
Under the rules conditions, you can specify that the node is to be "published",
and when it is ...
do .... whatever tasks you wish.

The Rules events aren't very straightforward, but once you get used to them, it'll make more sense.
Hope this helps.

If it helps, you can safely explore the Rules module by creating "test" cases wherein they aren't enabled.

Thank you very much! This was

miraje's picture

Thank you very much!
This was helpful!

wjaspers: Thanks for your

littlealy's picture

wjaspers:

Thanks for your response! Unfortunately (see my original post) I already tried this. Maybe something is wrong with my versions of rules, but when I set up event: node is updated, condition: node is published, this meant my action is carried out whenever a published post is edited, which is bad bad bad cuz the users of my site edit their posts a lot. It appears to be a condition that checks only to see if the post is published at all, not if it is being published at this save point....

any luck?

adraskoy's picture

Any luck with this? I need something similar and am trying to determine if Rules is my best bet, or writing a custom module. What I need is a way to sync the "published" status of a lot of related content connected by two levels of node references. When the top level is published or unpublished, I need the referenced nodes and their referenced nodes to also change status. A "node is published" rule would presumably work if it was not tied to the editing of the node form.

This solution works for me

dvoznyak's picture

The event should be "After updating existing content".
Under condition "Content is published" there is a fieldset called "Arguments configuration" with two choice options: "updated content" and "unchanged content". The condition we are looking for is when "updated content" is published and "unchanged content" is not.
So
1. Add a condition "Content is published", select "updated content" under "Arguments configuration".
2. Add a condition "Content is published", select "unchanged content" and check "Negate"
The result would be:
IF Updated content is published
AND NOT Unchanged content is published
DO
...

Going to be saved

liberatr's picture

Using dvoznyak's conditions, I got this to work, but using the event, "Content is going to be saved".

ON event Content is going to be saved
IF
NOT condition Unchanged content is published indent
AND
condition Saved content is published indent
DO
action (choose whatever action you would like to happen)

Worked for me

krisahil's picture

Using Ryan Price's suggestion worked perfectly for me. If you're interested, here's the exported rule you can import:

array (
  'rules' =>
  array (
    'rules_node_going_from_unpublished_to_published' =>
    array (
      '#type' => 'rule',
      '#set' => 'event_node_presave',
      '#label' => 'Node Going from Unpublished to Published',
      '#active' => 1,
      '#weight' => '0',
      '#categories' =>
      array (
      ),
      '#status' => 'custom',
      '#conditions' =>
      array (
        0 =>
        array (
          '#type' => 'AND',
          0 =>
          array (
            '#negate' => 1,
            '#type' => 'condition',
            '#settings' =>
            array (
              '#argument map' =>
              array (
                'node' => 'node_unchanged',
              ),
            ),
            '#name' => 'rules_condition_content_is_published',
            '#info' =>
            array (
              'label' => 'Node not published',
              'label callback' => false,
              'arguments' =>
              array (
                'node' =>
                array (
                  'type' => 'node',
                  'label' => 'Content',
                ),
              ),
              'module' => 'Node',
            ),
            '#weight' => 0,
          ),
          '#weight' => 0,
          1 =>
          array (
            '#weight' => 0,
            '#info' =>
            array (
              'label' => 'Node is now published',
              'label callback' => false,
              'arguments' =>
              array (
                'node' =>
                array (
                  'type' => 'node',
                  'label' => 'Content',
                ),
              ),
              'module' => 'Node',
            ),
            '#name' => 'rules_condition_content_is_published',
            '#settings' =>
            array (
              '#argument map' =>
              array (
                'node' => 'node',
              ),
            ),
            '#type' => 'condition',
          ),
        ),
      ),
      '#actions' =>
      array (
        0 =>
        array (
          '#weight' => 0,
          '#info' =>
          array (
            'label' => 'Show message that node is now published',
            'label callback' => false,
            'module' => 'System',
            'eval input' =>
            array (
              0 => 'message',
            ),
          ),
          '#name' => 'rules_action_drupal_message',
          '#settings' =>
          array (
            'message' => 'Hooray! Node went from unpublished to published.',
            'error' => 0,
          ),
          '#type' => 'action',
        ),
      ),
      '#version' => 6003,
    ),
  ),
)

Hi, Please how can I import

drupping's picture

Hi,

Please how can I import this rule? I'm using Drupal 7.

Thanks so much!

Hi again I've tried to import

drupping's picture

Hi again

I've tried to import it but these errors was displayed:

  • Import failed.
  • Unable to parse the pasted export.

Do you know why and how can I solve it?

Thanks!

This Rules export is outdated....

dydave's picture

Hi @drupal727,

I found 2 videos that might be of help if you would like to import Rules:

If you look carefully at the dates of the comment to which you replied (July 28, 2011 at 1:11pm) you will probably find out it was posted a long time ago.... Therefore, I'm afraid this Rules export is already outdated if you are using some of Rules' latest versions (which I would assume you are, since you are getting these error messages).

What I would suggest is that you try to re-build this Rule (on a current version), based on the information you can find in the export, for example:
'#set' => 'event_node_presave', would most likely correspond to the event before node is updated or saved.

'#conditions' =>
      array (

lists the conditions
'#type' => 'condition',
...
'node' => 'node_unchanged',
'#name' => 'rules_condition_content_is_published',

condition to test whether the unchanged node is published.

and so forth...

If you are able to update this Rule with a current version, we would greatly appreciate if you could please share the export (with the corresponding version of Rules you've been using) with us in this post.

Lastly, I would like to mention that you might be able to find more help and much quicker by trying to connect with the Drupal community on IRC, for example in the channels #drupal-support or #drupal.

I hope these few ideas will help you moving forward with this feature.
Cheers!

CCK field and some logic

aharown07's picture

Maybe I've misunderstood the OP question, but it sounds like they want the rule to only execute once. If I understand the "publish" process correctly, it's nothing more than a save with the publish status enabled. If that's the case, the rule is never going to be able to tell if it is going from unpub to pub status. It has no way of knowing what the state was before the save.

So we have to give it a way. It seems a bit heavy, but one way to do it is add a CCK field (or you could use a Flag from the Flags module). Then have the rule do this:

  • On save, condition=published AND condition=cckfield < 1
    Action...

    • Set the CCK field to 1
    • Execute your stuff

Result is that next time the node is edited while still in Published status, the rule will check the CCK field, see the "1" and not execute.

Edit... actually, the previous post is probably a better way. I think I understand it now, but didn't before: the arguments there are doing some kind of check to make sure the save is one where there is no change to the content. So it won't run the rule on edits.

Drupal 7

blogook's picture

Hi there,

I am trying to accomplish the same but then for drupal 7

I am using module scheduler to publish content .. and i cannot figure out how i can have rules detect that when the scheduled content is published to take certain actions (email members that want to be notified when new content is published)

My work around is that when content is changed members are notified, but then everytime i change the content .. all subscribers are notified agiain, and again .. and again

thanks in advance :)

You can use a rule like

jca's picture

You can use a rule like that:

{ "rules_on_node_publish" : {
    "LABEL" : "On node publish",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "devel" ],
    "ON" : [ "node_presave" ],
    "IF" : [
      { "data_is" : { "data" : [ "node:status" ], "value" : "1" } },
      { "AND" : [] },
      { "data_is" : { "data" : [ "node-unchanged:status" ], "value" : "0" } }
    ],
    "DO" : [ { "devel_debug" : { "value" : [ "node:nid" ] } } ]
  }
}

Impor in Drupal 7

thanks!

pjmcghee's picture

Good morning, JCA,

I really appreciate the response. My question is, "is there a way to do this without haveing devel installed?

Thanks, PJ

Hi pjmcghee!

jca's picture

Of course, I only put the action as an example...

Try importing this rule:

{ "rules_on_node_publish" : {
    "LABEL" : "On node publish",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_presave" ],
    "IF" : [
      { "data_is" : { "data" : [ "node:status" ], "value" : "1" } },
      { "AND" : [] },
      { "data_is" : { "data" : [ "node-unchanged:status" ], "value" : "0" } }
    ],
    "DO" : []
  }
}

I have created exactly this

estoyausente's picture

I have created exactly this last rule and run perfect!

Thanks for the help, guys.

Estoy por las redes @estoyausente

Same thing from interface

vladan.me's picture

As @jca suggested, I believe that this can be done through user interface that rules provide, without any coding, like this:
1) Event -
Node - After updating existing content
2) Conditions -
a) pick Data Comparison, node-unchanged:status equals 0
b) pick Data Comparison, node:status equals 1
3) Actions -
whatever you want to add as action

Doesn't work as required.

avalot's picture

This is still only a partial solution. Every time the user does an un-publish and re-publish, the action will trigger again.

I'm assigning points to users for submitting new stories, I can't allow them to game the system by un/re-publishing their stories. So here's the workaround I used:

Added the module Hidden Field Widgets, and used it on a boolean field on my "story" content type. The boolean field is called "ever published". It's not visible to users, ever.

The rule checks that this "field-ever-published" is empty and content is "published". If so it changes "field-ever-published" to true, and rewards the user. There's nothing else in the system that can change "field-ever-published" back to false, ever.

If you need a rule that fires only the first time something is published, this might help. Good luck!

here is http://stackoverflow.

qqboy's picture

here is

http://stackoverflow.com/questions/12763223/drupal-7-how-to-trigger-hook...

<?php
function MYMODULE_node_update($node){
  if (isset(
$node->original->status) && $node->original->status == 0 && $node->status == 1){
    
MYMODULE_mymagic_func($node);
  }
?>

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week