Rule to unpublish on Date field, but ignore if the Date field is empty

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

I have a site in which there are job listings that have an expiration date. I have created a rule that pulls that expiration date from the date field and unpublished the node on that date. I can get that to work fine.

The difficult part that was added into the situation is that some of the jobs are "continuous" In other words, they do not have a deadline date and the site users need to be able to leave the date field empty on the job listing.

When cron runs, the nodes with empty date fields unpublish. I was trying to use the "Created content field has value" and make it see it as NULL, but I'm not having any success.

Idea is:
If the date field is empty, do not unpublish it. Leave it alone. ;-)

Any advice would be greatly appreciated.

By the way... this module is amazing!!!!

Dan

Comments

Two ideas

itangalo's picture

-1-
You should check out Rules Scheduler, included in Rules! If you use that, you can schedule a rule set 'Unpublish node and do some other stuff' on any time you choose -- for example the time set in the CCK field.
Create a rule set, and then a triggered rule activated on node save. If date is set, schedule the rule set.

I agree that Rules is awesome, and Rules Scheduler takes it to an even higher level.

-2-
Just the other day I struggled with date fields and checking for empty values. What I ended up doing was activating the PHP filter and using a custom PHP condition: return is_null($node->field_publish_date[0]['value']);.
I don't know why, but it just seems that empty fields and dates don't work very well.

Good luck!
//Johan Falk, NodeOne, Sweden

Trying this out

daniel-san's picture

Yes, I am using Rules scheduler to make all this work and it's great. I am having it unpublish on the deadline date by using the token replacement in the DO section

I am trying out your suggestion and it does work in not setting a date, but the node is still showing up in the scheduled tasks with an empty date. When cron runs, it automatically unpublishes. So, I think I need to think through the flow of how this should run. Here is what I have:

ON event After saving new content
IF
condition Created content is job test <- This is looking for specific content type
AND
NOT
Created content's field 'field_job_test_date' has value <- This is where I've included your bit of php and included the NOT value
Here is the bit of code: return is_null($node->field_publish_date[0]['value']);
DO
action Schedule "Unpublish content {rule set}" <- This is the unpublish action on the date field. I've included the data field token here so that it "knows" when to set the unpublish date.

My thinking is that the Rules Trigger looks at the content type and that the node does not have an empty value in the date field, then schedule an unpublish on the value in the date field.

Hopefully I am writing this clearly enough to understand. Thanks for the help.

Strange

itangalo's picture

Thats strange -- your rule flow seems perfectly fine to me...

Have you installed Devel and had a look at the date field? It could perhaps tell you if there's anything strange in the array in which the date is stored.

Oh, now I see it!
You shouldn't run condition "Field has value", but rather a custom PHP test. The way you have put it up now, Rules checks whether the date field matches your expression, which becomes pretty weird (and definately a non-true statement).

I hope this helps.
//Johan Falk, NodeOne, Sweden

Ok, I'm gonna give that a

daniel-san's picture

Ok, I'm gonna give that a try. Thanks for checking it.

Woo hoo!!!!

daniel-san's picture

It seems to be working!!! Awesome.

I've tested creating nodes with dates and the schedule an unpublish action and I have created a couple of nodes without dates and they do not show up on the schedule list. Seems like a success to me!!

Thank you so much for your help Johan. I've been really trying to figure this out on my own for some time and I had exhausted my knowledge in this area.

Having the ability to get feedback, ideas and help from the Drupal community is one of my favorite things about Drupal. I always try and pass on help as well because there are so many that have come to my assistance.

Have a great one,
Dan Serrato

Should I be using the

daniel-san's picture

Should I be using the checkbox to Negate the php test?

I assume that I should, since I am wanting Rules to not schedule an unpublish on nodes that have a null value in the date field.

I am still having difficulty with this and I'm not getting nodes to schedule correctly.

Two tips

itangalo's picture

Here are two more tips:

1) When writing PHP conditions I usually put any negations in the test itself -- !is_null(whatever) instead of is_null(whatever). I find the 'NOT' notation in the Rules UI a bit confusing, and removing a negation makes everything a bit easier to overview.

2) Are you having problems scheduling things at all, or is it the time that is weird? I've had some problems getting Rules Scheduler to use the proper timezone, which ended up in me using a PHP statement and variable_get('date_default_timezone', '0') or something similar. (I really should get around to post an issue about this...) If done properly, you should also check for user configured timezones.

//Johan Falk, NodeOne, Sweden

Yea, at this point it's not

daniel-san's picture

Yea, at this point it's not event scheduling at all. I've turned on the debugger and it seems as if everything is running correctly. It goes through all the steps successfully

      o 0 ms "After saving new content" has been invoked.
      o 0.482 ms Executing the rule "Schedule Unpublish Job Content on creation" on rule set "After saving new content"
      o 0.836 ms Condition "Created content is Job" evaluated to TRUE.
      o 1.073 ms Condition "Execute custom PHP code" evaluated to TRUE.
      o 4.198 ms Action execution: "Schedule "Unpublish content {rule set}""
      o 8.28 ms Evaluation of "After saving new content" has been finished.
* Job job 3 has been created.

I just changed the Execute custom php code to return !is_null($node->field_job_closingdate[0]['value']);

I do have a feeling that it has something to do with the date field and not using the correct token or something.

Where to find php custom rule

jasom's picture

Where to find php custom rule code
http://drupal.org/node/367483#comment-3298030

It is working, but...

daniel-san's picture

Ok, here is why I was not seeing any nodes being added to the Rules Scheduler.

I was testing nodes with a date later than May 14.

There are already more than 30 nodes with dates up to and including May 14.

The view that created the Rules Schedule table only shows 30 results and the pager was not enabled.

I enabled the pager and I can then page through the list and find the new and updated nodes added to the scheduler.

It seems as thought the pager for the Rules Scheduler view should be enabled by default. I had no idea that it was being created by a view. My colleague asked if it was and we both had a lightbulb moment!!!

What a huge relief. whew.

Well spotted

itangalo's picture

Well spotted!

I'm glad that it was solved. That pager think should really be a bug report. :-)

//Johan Falk, NodeOne, Sweden

Scheduled items not being unpublished

daniel-san's picture

Johan,

So, it seems as though I did get the system to create the dates to unpublish and put them in the schedule list, but they are not unpublishing.

I have a rule set called "Unpublish Content {rule set}" with no conditions and a DO action of "Unpublish Content {argument}" in the Rule Elements. It is active.

I have a Triggered Rule that is the following:
Name: Schedule Unpublish Job Content on Creation to work after saving new content
It has the conditional items:

ON event After saving new content
IF
conditionCreated content is Job or Job via Feed (two content types
AND
conditionExecute custom PHP code (this is the code from previous discussions that set the correct schedule date and ignored nodes with blank dates)

DO
"Unpublish content {rule set}" (the rule set from above)

Now, I have a huge list of nodes in the Schedule with a big portion of that list has nodes that have passed the date they should have been unpublished.

I'm stumped as to what I'm missing

Cron!

itangalo's picture

Scheduled tasks will only be executed on cron runs. So you'll have to run cron manually, if you haven't any automated processes for that.

Check out administer > reports > status, and you'll find a link for running cron. (Administration menu one of my favourite modules), has a quick-link for this. As does Devel.)

I hope this helps!
//Johan Falk, NodeOne, Sweden

Update and solution

daniel-san's picture

Thanks for all the feedback Johan. It has been truly appreciated.

I ended up figuring out the problem, and it did have to do with cron.

I was using Poorman's Cron module and cron jobs were being executed but not doing anything about the Rules scheduled to be executed.

I checked into Poorman's Cron and saw that version 1 had some issues with caching. I upgraded to Poorman's Cron version 2 and everything started working.

I am not sure that it was exactly the caching issue, but the problem was definitely resolved by moving to the newer version of the Poorman's Cron module.

Dan

Rules

Group organizers

Group categories

Categories

Group notifications

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