View source
<?php
namespace Drupal\photos\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\file\Entity\File;
use Drupal\Core\Url;
class PhotosImage extends FieldPluginBase {
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_photo'] = [
'default' => '',
];
$options['image_style'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['link_photo'] = [
'#title' => t("Link image"),
'#description' => t("Link the image to the album page or image page."),
'#type' => 'radios',
'#options' => [
'' => $this
->t('None'),
'album' => $this
->t('Album page'),
'image' => $this
->t('Image page'),
],
'#default_value' => $this->options['link_photo'],
];
$style_options = image_style_options();
$form['image_style'] = [
'#title' => $this
->t('Image style'),
'#type' => 'select',
'#default_value' => $this->options['image_style'],
'#options' => $style_options,
];
parent::buildOptionsForm($form, $form_state);
}
public function render(ResultRow $values) {
$render_image = [];
$image_style = $this->options['image_style'];
$picture_fid = $this
->getValue($values);
if (!$picture_fid) {
$node = $values->_entity;
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,
],
],
];
}
if ($this->options['link_photo'] == 'image') {
$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') {
$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;
}
}