You are here

function photos_node_delete in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 photos.module \photos_node_delete()
  2. 7.3 photos.module \photos_node_delete()
  3. 6.0.x photos.module \photos_node_delete()

Implements hook_ENTITY_TYPE_delete().

File

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

Code

function photos_node_delete(NodeInterface $node) {
  if ($node
    ->getType() == 'photos') {
    $db = \Drupal::database();
    if ($node->album['count'] || !\Drupal::config('photos.settings')
      ->get('photos_user_count_cron')) {
      $result = $db
        ->query('SELECT f.fid, f.uri FROM {file_managed} f INNER JOIN {photos_image} p ON f.fid = p.fid WHERE p.pid = :nid', [
        ':nid' => $node
          ->id(),
      ]);
      foreach ($result as $file) {
        $image = new PhotosImage($file->fid);
        $msg[] = $image
          ->delete($file->uri);
      }
      if (isset($msg[0])) {
        PhotosAlbum::setCount('user_image', $node
          ->getOwnerId());
        $image_count = count($msg);
        $message = \Drupal::translation()
          ->formatPlural($image_count, '1 image deleted.', '@count images deleted.');
        \Drupal::messenger()
          ->addMessage($message);
      }
    }

    // Cleanup photos_album table.
    $db
      ->delete('photos_album')
      ->condition('pid', $node
      ->id())
      ->execute();
    PhotosAlbum::setCount('user_album', $node
      ->getOwnerId());
  }
}