public function MediaExpireService::unpublishExpiredMedia in Media Expire 8.2
Same name and namespace in other branches
- 8 src/MediaExpireService.php \Drupal\media_expire\MediaExpireService::unpublishExpiredMedia()
Unpublishes already expired media elements.
File
- src/
MediaExpireService.php, line 46
Class
- MediaExpireService
- Contains the media unpublish logic.
Namespace
Drupal\media_expireCode
public function unpublishExpiredMedia() {
/** @var \Drupal\media\MediaTypeInterface[] $bundles */
$bundles = $this->entityTypeManager
->getStorage('media_type')
->loadMultiple();
foreach ($bundles as $bundle) {
if ($bundle
->getThirdPartySetting('media_expire', 'enable_expiring')) {
$expireField = $bundle
->getThirdPartySetting('media_expire', 'expire_field');
$query = $this->entityTypeManager
->getStorage('media')
->getQuery('AND');
$query
->condition('status', 1);
$query
->condition('bundle', $bundle
->id());
$query
->condition($expireField, date("Y-m-d\\TH:i:s"), '<');
$entityIds = $query
->execute();
/** @var \Drupal\media\Entity\Media[] $medias */
$medias = $this->entityTypeManager
->getStorage('media')
->loadMultiple($entityIds);
foreach ($medias as $media) {
$media
->setUnpublished();
$media->{$expireField}
->removeItem(0);
$media
->save();
}
}
}
}