You are here

function photos_photos_image_delete in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 photos.module \photos_photos_image_delete()

Implements hook_ENTITY_TYPE_delete().

File

./photos.module, line 633
Implementation of photos.module.

Code

function photos_photos_image_delete(PhotosImageInterface $photos_image) {

  /** @var \Drupal\photos\Entity\PhotosImage $photos_image */

  // If this entity is the album cover, clear it.
  $db = \Drupal::database();
  $db
    ->update('photos_album')
    ->fields([
    'cover_id' => 0,
  ])
    ->condition('album_id', $photos_image
    ->getAlbumId())
    ->condition('cover_id', $photos_image
    ->id())
    ->execute();

  // Update image statistics.
  if (\Drupal::config('photos.settings')
    ->get('photos_user_count_cron')) {
    $album_id = $photos_image
      ->getAlbumId();
    $uid = $photos_image
      ->getOwnerId();
    if ($album_id) {

      // Update album count.
      PhotosAlbum::setCount('node_album', $album_id);

      // Clear album page and node cache.
      Cache::invalidateTags([
        'photos:album:' . $album_id,
        'node:' . $album_id,
      ]);
    }
    if ($uid) {

      // Update user count.
      PhotosAlbum::setCount('user_image', $uid);
    }
  }
}