Unpublish Expired Nodes using Rules?

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

Similiar to a previous post, but different :)

I have a series of nodes that have a CCK repeating dates field [DATE].

When the last of each node's repeating dates has passed - ie: the item has expired - I'd like to unpublish the node and send an email to the author.

Presumably I'd like to do this nightly.

I assume that this should be possible to do using views and rules, but I'm not sure where to start....

The problem is that with views, I dont see any direct way of selecting the nodes that have expired (all nodes with all enddates < now)

So I was thinking I might have to approach this as multiple steps.

1) Using views and rules, mark all nodes using a flag (assume its expired)
2) Using views and rules, scan nodes again, and if a node has any end date in the future, clear the flag (its not really expired)
3) Then for each node that is still flagged (it really has expired), unpublish the node and email the author.

Isw it possible to pass the results of a view to rules?
Is it possible to use views and rules in the same scheduled process?
Is it possible to have multiple views/rules steps like this in one scheduled process?

Thanks for any help.

Comments

No need for Views

itangalo's picture

If you can get the last date, there should be no need of using Views. Instead I would recommend Rules Scheduler (included in the Rules project).

What you want to do is basically the following:

  • A rule set that unpublished a selected node (provided by an argument to the rule set).
  • A rule that triggers on node creation (or save), and then schedules the above rule set on the last date.

There are two things making this process a bit more complicated.

  1. If the date field(s) changes, you would probably want to reschedule the publishing. This can be done by letting the rule set first delete any previous scheduled tasks for the node, and then add a new one.
  2. There are (if I understand correctly) multiple instances of the date field, and you want the last date. My guess is that you'll have to run some PHP to get the end date value. Or, possibly, you could have the dates listed in reverse chronological order (since Tokens collect only the first value of multiple-value fields.)

Good luck!
//Johan Falk, NodeOne, Sweden

Itangalo> "My guess is that

dereks8443's picture

Itangalo> "My guess is that you'll have to run some PHP to get the end date value."

Do you know where I could find some sample PHP code that deals with multiple-value dates (such as repeating dates)? (I'm also interested in node references that have multiple values.)

Thanks!

Devel + PHP filter

itangalo's picture

I don't know off the cuff where to find snippets for that, but I bet there are plenty to be found in the Handbooks.

However, if you activate the PHP filter (in core) and install Devel, you can get a good overview of the data structre of nodes (and some other objects) -- each node gets a 'devel' tab that shows its data structure.

The general structure for multiple-value fields is the same as for single-value fields, which is $node->field_name_here[0]['value'], but the [0] is increased for each value entered in the field. The ['value'] part shifts somewhat between different types of fields -- this is where Devel is really handy. (Dates, for example, has a whole lot of other data available to get stuff like time zone, while formatted text fields has data for input format and cleaned text.)

If you want to process all the data from a multiple-value field, you'll have to write a short loop -- something like this.

foreach ($node->field_name_here as $field_instance) {
if ($field_instance['value'] == $your_condition) {
// Do some stuff
}
}

I've seen some discussions in the Rules issue queue about integrating foreach loops into the Rules actions scheme, but I think it will take quite some time before that is usable. Until then, we have to code ourselves. :-)

Good luck!
//Johan Falk, NodeOne, Sweden

Rules

Group organizers

Group categories

Categories

Group notifications

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