You are here

public function PhotosImageCover::render in Album Photos 8.5

Same name and namespace in other branches
  1. 6.0.x src/Plugin/views/field/PhotosImageCover.php \Drupal\photos\Plugin\views\field\PhotosImageCover::render()

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/PhotosImageCover.php, line 136

Class

PhotosImageCover
Field handler to display album cover in views.

Namespace

Drupal\photos\Plugin\views\field

Code

public function render(ResultRow $values) {
  $renderImage = [];
  $viewMode = $this->options['view_mode'];
  $picture_id = $this
    ->getValue($values);
  $photosImage = FALSE;
  if ($picture_id) {

    /* @var \Drupal\photos\Entity\PhotosImage $photosImage */
    $photosImage = $this->entityTypeManager
      ->getStorage('photos_image')
      ->load($picture_id);
  }
  else {
    if ($values->_entity instanceof PhotosImage) {
      $photosImage = $values->_entity;
      $node = $this->entityTypeManager
        ->getStorage('node')
        ->load($values->_entity
        ->getAlbumId());
    }
    else {
      $node = $values->_entity;
    }
    if ($node
      ->bundle() == 'photos') {
      $nid = $node
        ->id();
      if (!$picture_id) {

        // Get first image for cover photo.
        if ($nid) {
          $picture_id = $this->connection
            ->query("SELECT id FROM {photos_image_field_data} WHERE album_id = :nid ORDER BY id ASC", [
            ':nid' => $nid,
          ])
            ->fetchField();
        }
      }
      $photosImage = $this->entityTypeManager
        ->getStorage('photos_image')
        ->load($picture_id);
    }
  }
  if ($photosImage && $viewMode) {
    $viewBuilder = $this->entityTypeManager
      ->getViewBuilder('photos_image');
    $renderImage = $viewBuilder
      ->view($photosImage, $viewMode);

    // Add the link if option is selected.
    if ($this->options['link_photo'] == 'image') {

      // Link to image page.
      $image = \Drupal::service('renderer')
        ->render($renderImage);
      $renderImage = [
        '#type' => 'link',
        '#title' => $image,
        '#url' => Url::fromRoute('entity.photos_image.canonical', [
          'node' => $photosImage
            ->getAlbumId(),
          'photos_image' => $photosImage
            ->id(),
        ]),
        '#options' => [
          'attributes' => [
            'html' => TRUE,
          ],
        ],
        '#cache' => [
          'tags' => [
            'photos:image:' . $picture_id,
          ],
        ],
      ];
    }
    elseif ($this->options['link_photo'] == 'album') {

      // Get album id and link to album page.
      $node = $values->_entity;
      $nid = $node
        ->id();
      $image = \Drupal::service('renderer')
        ->render($renderImage);
      $renderImage = [
        '#type' => 'link',
        '#title' => $image,
        '#url' => $photosImage
          ->getAlbumUrl(),
        '#options' => [
          'attributes' => [
            'html' => TRUE,
          ],
        ],
        '#cache' => [
          'tags' => [
            'photos:album:' . $nid,
            'photos:image:' . $picture_id,
          ],
        ],
      ];
    }
  }
  return $renderImage;
}