You are here

function photos_node_delete in Album Photos 6.0.x

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

Implements hook_ENTITY_TYPE_delete().

File

./photos.module, line 600
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')) {
      $results = $db
        ->query('SELECT id FROM {photos_image_field_data} WHERE album_id = :nid', [
        ':nid' => $node
          ->id(),
      ]);
      $imageCount = 0;
      foreach ($results as $result) {
        $photos_image = \Drupal::entityTypeManager()
          ->getStorage('photos_image')
          ->load($result->id);
        $photos_image
          ->delete();
        $imageCount++;
      }
      if ($imageCount > 0) {

        // @todo move to PhotosStatistics?
        // @todo look at patch for core statistics and mimic until that is committed.
        PhotosAlbum::setCount('user_image', $node
          ->getOwnerId());
        $message = \Drupal::translation()
          ->formatPlural($imageCount, '1 image deleted.', '@count images deleted.');
        \Drupal::messenger()
          ->addMessage($message);
      }
    }

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