I have the article node with two extra fields:
- Taxonomy reference field
field_verification_status
with two possible values:- Valid (tid: 272)
- Expired (tid: 271)
- Date field (
field_article_expiry_date
)
When a user will create a new article, will set:
- Taxonomy reference field
field_verification_status
= Valid (tid: 272) - Date field (
field_article_expiry_date
) = Any time in the future.
Now using Ultimate Cron Module, I want on every cron run, to:
- Check if the
field_article_expiry_date
value is less than current time (now
) - if above return true then change the
field_verification_status
value to Expired (tid: 271
)
To accomplish the above, I have created a new module custom_cron_expired_articles
and in the .module
file I added the below codes:
<?php
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
/**
* Implements hook_cron().
*/
// On "Cron Run" and "Every Half Hour":
// Loop through all "Valid" articles with expiry date set to the past,
// Change the status of each article to "Expired".
function custom_cron_expired_articles_cron() {
// Get the "cron time" (Return Value is in UNIX Timestamp format)
//$cron_time = \Drupal::time()->getCurrentTime();
//date_default_timezone_set('GMT');
// Load all available expired nodes of type 'offer'.
$storage = \Drupal::entityTypeManager()
->getStorage('node');
$query = \Drupal::entityQuery('node')
->accessCheck(FALSE)
->condition('type', 'article')
->condition('field_verification_status', 272) // Verification Status = Valid
->condition('field_article_expiry_date', strtotime($?????), '<=') // Something is missing here
//->range(0,50);
$nids = $query->execute();
// Loop through all expired nodes.
foreach ($nids as $nid) {
// Load the Node by its ID.
$node = \Drupal\node\Entity\Node::load($nid);
// Update the node.
$node->field_verification_status->target_id = 271; // Verification Status = Expired
// Save the updated node.
$node->save();
// resetCache() in such a loop over all entities of one type avoids running out of memory.
\Drupal::entityManager()->getStorage('node')->resetCache(array($nid));
}
}
How to compare if the field_article_expiry_date
is less than the current_time
? or by other words, how to load only the nodes with field_article_expiry_date
less than the current_time
?
Can somebody tell me what is the missing part ?