View source
<?php
namespace Drupal\photos\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\image\ImageStyleStorageInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Render\RendererInterface;
use Drupal\photos\PhotosAlbum;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
class PhotosAlbumFormatter extends EntityReferenceFormatterBase {
protected $renderer;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, ImageStyleStorageInterface $image_style_storage, RendererInterface $renderer) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage);
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('current_user'), $container
->get('entity_type.manager')
->getStorage('image_style'), $container
->get('renderer'));
}
public static function defaultSettings() {
return [
'photos_display_type' => 'cover',
] + parent::defaultSettings();
}
protected function needsEntityLoad(EntityReferenceItem $item) {
return !$item
->hasNewEntity();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['photos_display_type'] = [
'#title' => 'Type',
'#type' => 'select',
'#options' => [
'cover' => 'Cover',
'images' => 'Images',
'slideshow' => 'Slideshow',
],
];
unset($element['image_link']['#options']['file']);
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$display_types = [
'none' => '',
'colorbox' => $this
->t('Display cover as link to colorbox album'),
'cover' => $this
->t('Display cover that links to album view'),
'images' => $this
->t('Display the images'),
'slideshow' => $this
->t('Display an image slideshow'),
'thumbnails' => $this
->t('Display a few thumbnails'),
];
$image_setting = $this
->getSetting('photos_display_type');
if (isset($display_types[$image_setting])) {
$summary[] = $display_types[$image_setting];
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$photosDisplayType = $this
->getSetting('photos_display_type');
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
$label = $entity
->label();
switch ($photosDisplayType) {
case 'none':
break;
case 'cover':
$albumView = [];
if (isset($entity->album) && isset($entity->album['cover_id'])) {
$coverId = $entity->album['cover_id'];
$photos_album = new PhotosAlbum($entity
->id());
$albumView = $photos_album
->getCover($coverId);
}
$elements[$delta] = $albumView;
break;
}
if (!$entity
->isNew()) {
if (!empty($items[$delta]->_attributes)) {
$elements[$delta]['#options'] += [
'attributes' => [],
];
$elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes;
unset($items[$delta]->_attributes);
}
}
else {
$elements[$delta] = [
'#plain_text' => $label,
];
}
$elements[$delta]['#cache']['tags'] = $entity
->getCacheTags();
$elements[$delta]['#cache']['tags'][] = 'photos:album:' . $entity
->id();
}
return $elements;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition
->getFieldStorageDefinition()
->getSetting('target_type') == 'node';
}
}