public function PhotosImage::delete in Album Photos 8.4
Same name and namespace in other branches
- 8.5 src/PhotosImage.php \Drupal\photos\PhotosImage::delete()
Delete image.
File
- src/
PhotosImage.php, line 169
Class
- PhotosImage
- 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->pid = $db
->select('photos_image', 'p')
->fields('p', [
'pid',
])
->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 (\Drupal::config('photos.settings')
->get('photos_comment')) {
$db = \Drupal::database();
$result = $db
->select('photos_comment', 'v')
->fields('v', [
'cid',
])
->condition('v.fid', $fid)
->execute();
$cids = $result
->fetchAssoc();
if ($cids) {
$comments = \Drupal::entityTypeManager()
->getStorage('comment')
->loadMultiple($cids);
foreach ($comments as $comment) {
// Delete comment.
$comment
->delete();
}
}
}
// If photos_access is enabled.
if (\Drupal::config('photos.settings')
->get('photos_access_photos')) {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$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 = $file_system
->scanDirectory('private://photos/tmp_images', '~\\b' . $basename . '\\b~');
foreach ($file_uris as $uri => $data) {
// Delete.
$file_system
->delete($uri);
}
}
}
$db = \Drupal::database();
$db
->delete('photos_image')
->condition('fid', $fid)
->execute();
$db
->delete('photos_node')
->condition('fid', $fid)
->execute();
$db
->delete('photos_comment')
->condition('fid', $fid)
->execute();
if ($count) {
// Update image count.
PhotosAlbum::setCount('node_node', $file->pid);
PhotosAlbum::setCount('node_album', $file->pid);
PhotosAlbum::setCount('user_image', $file
->getOwnerId());
// Update comment statistics for album node.
// @todo Argument 1 passed to Drupal\comment\CommentStatistics::update() must be an instance of Drupal\comment\CommentInterface.
// _comment_update_node_statistics($file->pid);
// $node = \Drupal\node\Entity\Node::load($file->pid);
// \Drupal::service('comment.statistics')->update($node);
// @todo delete comments.
}
if (empty($file)) {
$file = File::load($fid);
}
if (empty($file->pid)) {
$db = \Drupal::database();
$file->pid = $db
->select('photos_image', 'p')
->fields('p', [
'pid',
])
->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->pid);
$file
->delete();
// Clear image cache.
Cache::invalidateTags([
'photos:image:' . $fid,
]);
return TRUE;
}
else {
return FALSE;
}
}