Your article content type has a dar_start_and_end_date date range field and you want to unpublish all article contents if end value of the date range field is passed.
If your content type uses a date field, you just need to remove .end_value in the coding.
Note: Drupal\Core\Datetime\DrupalDateTime, Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface.
$contentStorage = \Drupal::entityTypeManager()->getStorage('node');
$now = new DrupalDateTime('now');
$now->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
$contentIds = $contentStorage->getQuery()
->condition('type', 'article')
->condition('status', NodeInterface::PUBLISHED)
->condition('dar_start_and_end_date.end_value',
$now->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT), '<')
->execute();
$contents = $contentStorage->loadMultiple($contentIds);
foreach ($contents as $content) {
/** @var \Drupal\node\NodeInterface $content*/
$content->setUnpublished();
$content->save();
}
Bình luận