You are here

ImagePopup.php in Simple Image Popup 2.x

Same filename in this branch
  1. 2.x src/Controller/ImagePopup.php
  2. 2.x src/Plugin/CKEditorPlugin/ImagePopup.php
Same filename and directory in other branches
  1. 8 src/Controller/ImagePopup.php

File

src/Controller/ImagePopup.php
View source
<?php

namespace Drupal\image_popup\Controller;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\image\Entity\ImageStyle;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class ImagePopup.
 *
 * @package Drupal\image_popup\Controller
 */
class ImagePopup extends ControllerBase {

  /**
   * Render.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The ajax response containing the modal.
   */
  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;
  }

}

Classes

Namesort descending Description
ImagePopup Class ImagePopup.