function brightcove_file_delete in Brightcove Video Connect 8
Same name and namespace in other branches
- 8.2 brightcove.module \brightcove_file_delete()
- 3.x brightcove.module \brightcove_file_delete()
Implements hook_ENTITY_TYPE_delete().
File
- ./
brightcove.module, line 334 - Brightcove module.
Code
function brightcove_file_delete(EntityInterface $entity) {
/** @var \Drupal\file\FileInterface $entity */
// Condition to check whether the poster or the thumbnail has reference to
// the deleted file.
$condition = new Condition('OR');
$condition
->condition('poster__target_id', $entity
->id())
->condition('thumbnail__target_id', $entity
->id());
// Check file usage on BrightcoveVideo entity.
$database = \Drupal::database();
$results = $database
->select('brightcove_video', 'video')
->fields('video', [
'bcvid',
'poster__target_id',
'thumbnail__target_id',
])
->condition($condition)
->execute()
->fetchAll();
// If we got used files on the BrightcoveVideo entity that were deleted,
// update the entity to remove the reference to the deleted file(s).
foreach ($results as $result) {
$video = BrightcoveVideo::load($result->bcvid);
if (!empty($video)) {
$needs_save = FALSE;
// Unset poster image reference.
if ($result->poster__target_id == $entity
->id()) {
$video
->setPoster(NULL);
$needs_save = TRUE;
}
// Unset thumbnail image reference.
if ($result->thumbnail__target_id == $entity
->id()) {
$video
->setThumbnail(NULL);
$needs_save = TRUE;
}
// Save BrightcoveVideo if needed.
if ($needs_save) {
$video
->save();
}
}
}
}