You are here

public function PhotosLegacyImageViewController::view in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 src/Controller/PhotosLegacyImageViewController.php \Drupal\photos\Controller\PhotosLegacyImageViewController::view()

Provides a page to render a single entity.

Parameters

\Drupal\Core\Entity\EntityInterface $_entity: The Entity to be rendered. Note this variable is named $_entity rather than $entity to prevent collisions with other named placeholders in the route.

string $view_mode: (optional) The view mode that should be used to display the entity. Defaults to 'full'.

Return value

array A render array as expected by \Drupal\Core\Render\RendererInterface::render().

Overrides PhotosImageViewController::view

File

src/Controller/PhotosLegacyImageViewController.php, line 18

Class

PhotosLegacyImageViewController
Image view controller.

Namespace

Drupal\photos\Controller

Code

public function view(EntityInterface $photos_image, $view_mode = 'full', $langcode = NULL) {

  /** @var \Drupal\photos\Entity\PhotosImage $photosImage */
  $photosImage = $photos_image;
  if (!$photosImage) {
    throw new NotFoundHttpException();
  }
  $build = parent::view($photosImage, $view_mode, $langcode);

  // Legacy view modes rely heavily on {file_managed}.fid.
  $file_ids = $photosImage
    ->getFids();
  $fid = reset($file_ids);

  // @todo we can also check if legacy mode is enabled per node or some other
  // parameters if needed.
  if (!$fid || !is_numeric($fid)) {

    // Legacy view controller can only handle images with {file_managed}.fid.
    return $build;
  }
  $file = NULL;
  if ($fid) {
    $file = $this->entityTypeManager
      ->getStorage('file')
      ->load($fid);
  }

  // Get config settings.
  // @todo inject config factory?
  $config = \Drupal::config('photos.settings');

  // Get image file and data.
  $imageData = \Drupal::service('image.factory')
    ->get($file
    ->getFileUri());

  // Check if valid image.
  if (!$imageData
    ->isValid()) {
    throw new NotFoundHttpException();
  }
  $image = $this
    ->load($fid);
  if (!$image) {
    throw new NotFoundHttpException();
  }
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->load($image->album_id);
  if ($photosImage
    ->access('update')) {
    $image->ajax['edit_url'] = Url::fromUri('base:photos/image/' . $image->fid . '/update')
      ->toString();

    // Set album cover.
    $image->links['cover'] = Link::createFromRoute($this
      ->t('Set to Cover'), 'photos.album.update.cover', [
      'node' => $image->album_id,
      'photos_image' => $image->id,
    ], [
      'query' => $this
        ->getDestinationArray(),
    ]);

    // Remove parent set to cover link.
    unset($build['content']['links']['cover']);
  }
  $image->class = [
    'title_class' => '',
    'des_class' => '',
  ];
  $image->id = [
    'des_edit' => '',
    'title_edit' => '',
  ];
  $edit = $photosImage
    ->access('update');
  if ($edit) {

    // Image edit link.
    $url = Url::fromUri('base:photos/image/' . $image->fid . '/edit', [
      'query' => [
        'destination' => 'photos/image/' . $image->fid,
      ],
      'attributes' => [
        'class' => [
          'colorbox-load',
          'photos-edit-edit',
        ],
      ],
    ]);
    $image->ajax['edit_link'] = Link::fromTextAndUrl($this
      ->t('Edit'), $url);
  }
  if ($photosImage
    ->access('delete')) {

    // Image delete link.
    // @todo cancel should go back to image. Confirm to album.
    $url = Url::fromUri('base:photos/image/' . $image->fid . '/delete', [
      'query' => [
        'destination' => 'node/' . $image->album_id,
      ],
      'attributes' => [
        'class' => [
          'colorbox-load',
          'photos-edit-delete',
        ],
      ],
    ]);
    $image->ajax['del_link'] = Link::fromTextAndUrl($this
      ->t('Delete'), $url);
  }
  $renderCommentCount = [];
  if ($config
    ->get('photos_comment') && \Drupal::moduleHandler()
    ->moduleExists('comment')) {

    // Comment integration.
    $entities = [
      $photosImage
        ->id() => $photosImage,
    ];
    $stats = \Drupal::service('comment.statistics')
      ->read($entities, 'photos_image');
    if ($stats) {
      $comCount = 0;
      foreach ($stats as $commentStats) {
        $comCount = $comCount + $commentStats->comment_count;
      }
      $renderCommentCount = [
        '#theme' => 'photos_comment_count',
        '#comcount' => $comCount,
      ];
    }
  }
  $image->links['comment'] = $renderCommentCount;

  // Album images.
  $pager_type = 'album_id';
  $pager_id = $image->album_id;
  $data = isset($image->data) ? unserialize($image->data) : [];
  $style_name = isset($data['view_imagesize']) ? $data['view_imagesize'] : $config
    ->get('photos_display_view_imagesize');
  $image->links['pager'] = $photosImage
    ->getPager($pager_id, $pager_type);
  $legacyImage = [
    'file' => $file,
    'uri' => $image->uri,
    'title' => $image->title,
    'width' => $image->width,
    'height' => $image->height,
  ];
  $image->view = [
    '#theme' => 'photos_image_html',
    '#style_name' => $style_name,
    '#image' => $legacyImage,
    '#cache' => [
      'tags' => [
        'photos:image:' . $fid,
      ],
    ],
  ];

  // Get comments.
  // @todo get comments?
  // Check count image views variable.
  $photos_image_count = $config
    ->get('photos_image_count');
  $image->disable_photos_image_count = $photos_image_count;
  if (!$photos_image_count) {
    $count = 1;
    $this->connection
      ->update('photos_count')
      ->fields([
      'value' => $count,
    ])
      ->expression('value', 'value + :count', [
      ':count' => $count,
    ])
      ->condition('type', 'image_views')
      ->condition('id', $photosImage
      ->id())
      ->execute();
  }
  $image->title = $photosImage
    ->getTitle();
  $image->des = $photosImage
    ->getDescription();
  $GLOBALS['photos'][$image->fid . '_album_id'] = $image->album_id;
  $image_view = [
    '#theme' => 'photos_image_view',
    '#image' => $image,
    '#display_type' => 'view',
    '#cache' => [
      'tags' => [
        'photos:image:' . $fid,
      ],
    ],
  ];
  $build['#view_mode'] = 'legacy';
  $build['photos_legacy_image_view'] = $image_view;
  return $build;
}