public function PhotosImageFile::delete in Album Photos 6.0.x
Delete image.
File
- src/
PhotosImageFile.php, line 182
Class
- PhotosImageFile
- Create images object.
Namespace
Drupal\photosCode
public function delete($filepath = NULL, $count = FALSE) {
$fid = $this->fid;
if (!$filepath) {
if ($count) {
$file = File::load($fid);
$db = \Drupal::database();
$file->album_id = $db
->select('photos_image', 'p')
->fields('p', [
'album_id',
])
->condition('fid', $fid)
->execute()
->fetchField();
$filepath = $file
->getFileUri();
}
else {
$db = \Drupal::database();
$filepath = $db
->query('SELECT uri FROM {file_managed} WHERE fid = :fid', [
':fid' => $fid,
])
->fetchField();
}
}
if ($filepath) {
// If photos_access is enabled.
if (\Drupal::config('photos.settings')
->get('photos_access_photos')) {
$file_scheme = \Drupal::service('stream_wrapper_manager')
->getScheme($filepath);
if ($file_scheme == 'private') {
// Delete private image styles.
$pathinfo = pathinfo($filepath);
$ext = strtolower($pathinfo['extension']);
$basename = 'image_' . $fid . '.' . $ext;
// Find all derivatives for this image.
$file_uris = \Drupal::service('file_system')
->scanDirectory('private://photos/tmp_images', '~\\b' . $basename . '\\b~');
foreach ($file_uris as $uri => $data) {
// Delete.
\Drupal::service('file_system')
->delete($uri);
}
}
}
$db = \Drupal::database();
$db
->delete('photos_image')
->condition('fid', $fid)
->execute();
$db
->delete('photos_comment')
->condition('fid', $fid)
->execute();
if ($count) {
// Update image count.
PhotosAlbum::setCount('node_album', $file->album_id);
PhotosAlbum::setCount('user_image', $file
->getOwnerId());
}
if (empty($file)) {
$file = File::load($fid);
}
if (empty($file->album_id)) {
$db = \Drupal::database();
$file->album_id = $db
->select('photos_image', 'p')
->fields('p', [
'album_id',
])
->condition('fid', $file
->id())
->execute()
->fetchField();
}
// Delete file usage and delete files.
$file_usage = \Drupal::service('file.usage');
$file_usage
->delete($file, 'photos', 'node', $file->album_id);
$file
->delete();
// Clear image cache.
Cache::invalidateTags([
'photos:image:' . $fid,
]);
return TRUE;
}
else {
return FALSE;
}
}