You are here

public function ImagePopup::render in Simple Image Popup 2.x

Same name and namespace in other branches
  1. 8 src/Controller/ImagePopup.php \Drupal\image_popup\Controller\ImagePopup::render()

Render.

Return value

\Drupal\Core\Ajax\AjaxResponse The ajax response containing the modal.

1 string reference to 'ImagePopup::render'
image_popup.routing.yml in ./image_popup.routing.yml
image_popup.routing.yml

File

src/Controller/ImagePopup.php, line 26

Class

ImagePopup
Class ImagePopup.

Namespace

Drupal\image_popup\Controller

Code

public function render($fid, Request $request, $image_style = NULL) {
  $file = $this
    ->entityTypeManager()
    ->getStorage('file')
    ->load($fid);
  if (!empty($image_style)) {
    $image_style = ImageStyle::load($image_style);
  }
  $image_uri = $file
    ->getFileUri();
  if (!empty($image_style)) {
    $absolute_path = ImageStyle::load($image_style
      ->getName())
      ->buildUrl($image_uri);
  }
  else {

    // Get absolute path for original image.
    $absolute_path = Url::fromUri(file_create_url($image_uri))
      ->getUri();
  }
  $response = new AjaxResponse();
  $title = $request->query
    ->get('title');

  // Build a render array for the modal content.
  $content = [
    '#theme' => 'image',
    '#uri' => $absolute_path,
    '#alt' => $request->query
      ->get('alt'),
  ];

  // Get the modal options from the query.
  $options = Json::decode($request->query
    ->get('modal-options')) ?: [];
  $response
    ->addCommand(new OpenModalDialogCommand($title, $content, $options));
  return $response;
}