<?php
/**
* Implements hook_entity_presave().
*
* This hook autamatically sets an unpublish date for events so that they are no
* longer indexed by search engines after event has occurred.
*/
function unpublisher_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity instanceof NodeInterface) {
if ($entity->getType() == 'event') {
$start_date = date('U', strtotime($entity->field_start_date->value));
$end_date = date('U', strtotime($entity->field_end_date->value));
if ($start_date > $end_date) {
$entity->unpublish_on->value = strtotime('+1 day', $start_date);
}
else {
$entity->unpublish_on->value = strtotime('+1 day', $end_date);
}
}
}
}