You are here

public function PhotosImage::render in Album Photos 8.4

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/PhotosImage.php, line 66

Class

PhotosImage
Field handler to view album photos.

Namespace

Drupal\photos\Plugin\views\field

Code

public function render(ResultRow $values) {
  $render_image = [];
  $image_style = $this->options['image_style'];
  $picture_fid = $this
    ->getValue($values);
  if (!$picture_fid) {
    $node = $values->_entity;

    // Get first image for cover photo.
    if ($node && $node
      ->getType() == 'photos') {
      $nid = $node
        ->id();
      $db = \Drupal::database();
      $picture_fid = $db
        ->query("SELECT fid FROM {photos_image} WHERE pid = :nid ORDER BY fid ASC", [
        ':nid' => $nid,
      ])
        ->fetchField();
    }
  }
  if ($image_style && $picture_fid) {
    $file = File::load($picture_fid);
    $render_image = [
      '#theme' => 'image_style',
      '#style_name' => $this->options['image_style'],
      '#uri' => $file
        ->getFileUri(),
      '#cache' => [
        'tags' => [
          'photos:image:' . $picture_fid,
        ],
      ],
    ];
  }

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

    // Link to image page.
    $image = \Drupal::service('renderer')
      ->render($render_image);
    $link_href = 'base:photos/image/' . $picture_fid;
    $render_image = [
      '#type' => 'link',
      '#title' => $image,
      '#url' => Url::fromUri($link_href),
      '#options' => [
        'attributes' => [
          'html' => TRUE,
        ],
      ],
      '#cache' => [
        'tags' => [
          'photos:image:' . $picture_fid,
        ],
      ],
    ];
  }
  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($render_image);
    $link_href = 'base:photos/album/' . $nid;
    $render_image = [
      '#type' => 'link',
      '#title' => $image,
      '#url' => Url::fromUri($link_href),
      '#options' => [
        'attributes' => [
          'html' => TRUE,
        ],
      ],
      '#cache' => [
        'tags' => [
          'photos:album:' . $nid,
          'photos:image:' . $picture_fid,
        ],
      ],
    ];
  }
  return $render_image;
}