1

I'm using Drupal 7, I have a content type, where I have two fields for project allocation:

  1. Status(Active/Inactive)
  2. End Date

If the "End date" is less than current date, then the "status field" should be Active(By Default, Status will be Active) and if it exceeds current date, then that particular "status field" value in Content type should automatically change to Inactive.

Now, how can I automatically change the field value without any Button trigger?

  • Do you want to do it when you save the node, or right when the end date field is updated? – user72672 Dec 9 '19 at 9:22
  • If the end date is 31/12/19 and current date is 30/12/2019 and then status should automatically change to Inactive on 01/01/2020. – Narutouzu19 Dec 9 '19 at 9:24
  • @naratouzu19 Do you want this to happen on a form during user interaction, or automatically in the background? When you say 'fields' this could refer to database fields or fields on a form so it's a little unclear as to what you need to happen. – gillarf Dec 9 '19 at 16:12
  • I want it to happen in background, becoz i will have at least 100 data so whenever the current date exceeds the end date, status should change as Inactive. – Narutouzu19 Dec 10 '19 at 5:18
2

I think you have to do this with some custom JS code.

You can do something like this:

$('.end-date-field').on('change', function () {
  var endDate = new Date($(this).val());
  if (endDate < Date.now()) {
    $('.status-field').val('inactive');
  }
});

This code is not 100% correct, but it can help you to get an idea of what you have to do.

|improve this answer|||||
  • Thanks, i will try it. – Narutouzu19 Dec 9 '19 at 9:46
  • @Narutouzu19 Did it work? – user72672 Dec 9 '19 at 12:22
  • I'm new to Drupal, all i know is to create custom modules. I'm working on creating Custom Js Code. I created a custom module with Hook_init() and in that i used drupal_add_js() but no use, not working :( – Narutouzu19 Dec 9 '19 at 12:48
  • Have you seen this guide, drupal.org/docs/7/api/javascript-api/…? – user72672 Dec 9 '19 at 12:50
  • I used drupal_add_js inline function, but the CT data is not modified based on the conditions we tried. – Narutouzu19 Dec 9 '19 at 12:52
1

I'm assuming this needs to happen behind the scenes rather than as a result of user interaction.

I suggest you write a custom rules action which runs on cron every 5 minutes (or whatever time period you are happy with). The rule should load all of the relevant nodes using a db_select query, loop through them, check the end date and update the status depending on the comparison result.

Check the video here by Johan Falk on how to create a rules action: https://drupalize.me/videos/creating-rules-action?p=1152

And info for db_select: https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7.x

If the site does not get a lot of traffic you might need to fire cron externally. You can use a service like uptime robot to do this (https://uptimerobot.com/), or you can get your server to do it. It will need to hit your cron endpoint.

|improve this answer|||||
  • Looping through all the nodes is a terrible way to do this. Instead you should use the Rules Scheduler to schedule a task that changes the status to inactive on the proper day. That will run once and only once, instead of checking the date on every node every 5 minutes. – anonymous Dec 9 '19 at 19:36
  • Well it depends on how many nodes you expect to have. I've given a simple answer for what might be a simple use case, and it's true I have made an assumption that there are not many nodes. – gillarf Dec 10 '19 at 12:16
0

Thanks both Jdrupal and gillarf above. I did something different here, but i used both of your logic.

First, i created a custom module and then i used hook_menu() function. In this hook_menu, i used a link and if i call that link then page callback function will execute.

In page callback function, i used dbquery to get the data from the DB and based on my condition, i change the data and then again i used db query to update the DB with new data.

Now, i fixed that link in drupal cron and set it to call the link by particuar time, so that it will execute everyday automatically.

That's it Thanks :)

|improve this answer|||||
  • Ok, well that's the same as my answer essentially, except that you aren't using rules actions, you are just hitting your function on cron using hook_menu – gillarf Jan 8 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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