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

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?

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

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.

wjaspers: Thanks for your

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?

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

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

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,
    ),
  ),
)

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

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 :)

Rules

Group organizers

Group notifications

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

Hot content this week