Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / hook / cron.twig
1 /**
2  * Implements hook_cron().
3  */
4 function {{ machine_name }}_cron() {
5   // Short-running operation example, not using a queue:
6   // Delete all expired records since the last cron run.
7   $expires = \Drupal::state()->get('mymodule.last_check', 0);
8   \Drupal::database()->delete('mymodule_table')
9     ->condition('expires', $expires, '>=')
10     ->execute();
11   \Drupal::state()->set('mymodule.last_check', REQUEST_TIME);
12
13   // Long-running operation example, leveraging a queue:
14   // Queue news feeds for updates once their refresh interval has elapsed.
15   $queue = \Drupal::queue('aggregator_feeds');
16   $ids = \Drupal::entityManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
17   foreach (Feed::loadMultiple($ids) as $feed) {
18     if ($queue->createItem($feed)) {
19       // Add timestamp to avoid queueing item more than once.
20       $feed->setQueuedTime(REQUEST_TIME);
21       $feed->save();
22     }
23   }
24   $ids = \Drupal::entityQuery('aggregator_feed')
25     ->condition('queued', REQUEST_TIME - (3600 * 6), '<')
26     ->execute();
27   if ($ids) {
28     $feeds = Feed::loadMultiple($ids);
29     foreach ($feeds as $feed) {
30       $feed->setQueuedTime(0);
31       $feed->save();
32     }
33   }
34 }