diff --git a/src/Controller/FocalPointPreviewController.php b/src/Controller/FocalPointPreviewController.php index 9b35f6a..ffab891 100644 --- a/src/Controller/FocalPointPreviewController.php +++ b/src/Controller/FocalPointPreviewController.php @@ -10,6 +10,7 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\image\Entity\ImageStyle; use Drupal\file\Entity\File; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Symfony\Component\Validator\Exception\InvalidArgumentException; /** * Class FocalPointPreviewController. @@ -22,6 +23,8 @@ class FocalPointPreviewController extends ControllerBase { /** * {@inheritdoc} + * + * @throws \Symfony\Component\Validator\Exception\InvalidArgumentException */ public function content($fid, $focal_point_value) { $output = [ @@ -30,20 +33,25 @@ class FocalPointPreviewController extends ControllerBase { ], ]; - $image = $this->entityTypeManager()->getStorage('file')->load($fid); + $file = File::load($fid); + $image = \Drupal::service('image.factory')->get($file->getFileUri()); + if (!$image->isValid()) { + throw new InvalidArgumentException('The file with id = $fid is not an image.'); + } + $styles = $this->getFocalPointImageStyles(); // Since we are about to create a new preview of this image, we first must // flush the old one. This should not be a performance hit since there is // no good reason for anyone to preview an image unless they are changing // the focal point value. - image_path_flush($image->getFileUri()); + image_path_flush($image->getSource()); $derivative_images = []; $original_image = [ '#theme' => 'image', - '#uri' => $image->getFileUri(), + '#uri' => $image->getSource(), '#alt' => t('Focal Point Preview Image'), '#attributes' => [ 'id' => 'focal-point-preview-image', @@ -52,7 +60,7 @@ class FocalPointPreviewController extends ControllerBase { foreach ($styles as $style) { $style_label = $style->get('label'); - $url = $this->buildUrl($style, $image, $focal_point_value); + $url = $this->buildUrl($style, $file, $focal_point_value); $derivative_images[$style->getName()] = [ 'style' => $style_label,