You are here

public function FocalPointPreviewController::content in Focal Point 8

Throws

\Symfony\Component\Validator\Exception\InvalidArgumentException

1 string reference to 'FocalPointPreviewController::content'
focal_point.routing.yml in ./focal_point.routing.yml
focal_point.routing.yml

File

src/Controller/FocalPointPreviewController.php, line 77

Class

FocalPointPreviewController
Class FocalPointPreviewController.

Namespace

Drupal\focal_point\Controller

Code

public function content($fid, $focal_point_value) {
  $output = [];
  $file = $this->fileStorage
    ->load($fid);
  $image = $this->imageFactory
    ->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
    ->getSource());
  $derivative_images = [];
  $derivative_image_note = '';
  $original_image = [
    '#theme' => 'image',
    '#uri' => $image
      ->getSource(),
    '#alt' => $this
      ->t('Focal Point Preview Image'),
    '#attributes' => [
      'id' => 'focal-point-preview-image',
    ],
  ];
  if (!empty($styles)) {
    foreach ($styles as $style) {
      $style_label = $style
        ->get('label');
      $url = $this
        ->buildUrl($style, $file, $focal_point_value);
      $derivative_images[$style
        ->getName()] = [
        'style' => $style_label,
        'url' => $url,
        'image' => [
          '#theme' => 'image',
          '#uri' => $url,
          '#alt' => $this
            ->t('Focal Point Preview: %label', [
            '%label' => $style_label,
          ]),
          '#attributes' => [
            'class' => [
              'focal-point-derivative-preview-image',
            ],
          ],
        ],
      ];
    }
    $derivative_image_note = $this
      ->t('Click an image to see a larger preview. You may need to scroll horizontally for more image styles.');
  }
  else {

    // There are no styles that use a focal point effect to preview.
    $image_styles_url = Url::fromRoute('entity.image_style.collection')
      ->toString();
    $this
      ->messenger()
      ->addWarning($this
      ->t('You must have at least one <a href=":url">image style</a> defined that uses a focal point effect in order to preview.', [
      ':url' => $image_styles_url,
    ]));
  }
  $output['focal_point_preview_page'] = [
    '#theme' => "focal_point_preview_page",
    "#original_image" => $original_image,
    '#derivative_images' => $derivative_images,
    '#focal_point' => $focal_point_value,
    '#preview_image_note' => $this
      ->t('This preview image above may have been scaled to fit on the page.'),
    '#derivative_image_note' => $derivative_image_note,
  ];
  $options = [
    'dialogClass' => 'popup-dialog-class',
    'width' => '80%',
  ];
  $response = new AjaxResponse();
  $response
    ->addCommand(new OpenModalDialogCommand($this
    ->t('Images preview'), $output, $options));
  return $response;
}