View source
<?php
namespace Drupal\photos\Plugin\views\field;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\photos\Entity\PhotosImage;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PhotosImageCover extends FieldPluginBase {
protected $connection;
protected $entityTypeManager;
protected $entityDisplayRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->connection = $connection;
$this->entityTypeManager = $entity_type_manager;
if (!$entity_display_repository) {
$entity_display_repository = \Drupal::service('entity_display.repository');
}
$this->entityDisplayRepository = $entity_display_repository;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('database'), $container
->get('entity_type.manager'), $container
->get('entity_display.repository'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_photo'] = [
'default' => '',
];
$options['view_mode'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['link_photo'] = [
'#title' => $this
->t("Link image"),
'#description' => $this
->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'],
];
$viewModeOptions = $this->entityDisplayRepository
->getViewModeOptionsByBundle('photos_image', 'photos_image');
$default = '';
if (isset($viewModeOptions['cover'])) {
$default = 'cover';
}
$form['view_mode'] = [
'#title' => $this
->t('View mode'),
'#type' => 'select',
'#default_value' => $this->options['view_mode'] ?: $default,
'#options' => $viewModeOptions,
];
parent::buildOptionsForm($form, $form_state);
}
public function render(ResultRow $values) {
$renderImage = [];
$viewMode = $this->options['view_mode'];
$picture_id = $this
->getValue($values);
$photosImage = FALSE;
if ($picture_id) {
$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) {
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);
if ($this->options['link_photo'] == 'image') {
$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') {
$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;
}
}