View source
<?php
namespace Drupal\slick\Plugin\Field\FieldFormatter;
use Drupal\blazy\Plugin\Field\FieldFormatter\FormatterPlugin;
use Drupal\slick\SlickDefault;
use Drupal\slick\SlickFormatterInterface;
use Drupal\slick\SlickManagerInterface;
class SlickFormatterPlugin extends FormatterPlugin {
protected $isViewPrepared;
public function __construct(SlickFormatterInterface $formatter, SlickManagerInterface $manager) {
parent::__construct($formatter, $manager);
$this->formatter = $formatter;
$this->manager = $manager;
}
public function formatterInfo() {
if (!isset($this->formatterInfo)) {
$formatters = [];
$fields = SlickDefault::FIELDS;
foreach ($fields as $type) {
$name = $type == 'text' ? 'Text' : ($type == 'field_collection' ? 'FieldCollection' : ucwords($type));
$class = 'Drupal\\slick\\Plugin\\Field\\FieldFormatter\\Slick' . $name . 'Formatter';
$formatters['slick_' . $type] = [
'label' => t('Slick Carousel'),
'class' => $class,
'field types' => $type == 'text' ? SlickDefault::TEXTS : [
$type,
],
'settings' => $class::defaultSettings(),
];
}
if ($this->manager
->config('deprecated_formatter', TRUE, 'slick.settings')) {
array_pop($fields);
$formatters['slick'] = [
'label' => t('Slick Carousel (deprecated)'),
'field types' => $fields,
'settings' => SlickDefault::extendedSettings(),
];
}
$this->formatterInfo = $formatters;
}
return $this->formatterInfo;
}
public function getFormatter($type, $field, $instance, $namespace = 'slick') {
if (!isset($this->formatterType[$type])) {
$this->formatterType[$type] = FALSE;
if ($formatter = $this
->getActiveFormatter($type, $field, $instance, $namespace)) {
$this->formatterType[$type] = $formatter;
}
elseif ($this->manager
->config('deprecated_formatter', TRUE)) {
foreach (SlickDefault::FIELDS as $entity_type) {
if ($type == $entity_type) {
$plugin_id = $namespace . '_' . $type;
$class = $entity_type == 'field_collection' ? 'FieldCollection' : ucwords($entity_type);
$class = 'Drupal\\slick\\Plugin\\Field\\FieldFormatter\\Slick' . $class . 'Formatter';
$this->formatterType[$type] = new $class($plugin_id, $field, $instance, $this->formatter, $this->manager);
break;
}
}
}
}
return $this->formatterType[$type];
}
public function prepareView($entity_type, $entities, $field, $instances, &$items, $displays) {
if (!isset($this->isViewPrepared[$field['type']])) {
if (in_array($field['type'], [
'field_collection',
'paragraphs',
])) {
$is_slick = FALSE;
foreach ($displays as $id => $display) {
if ($display['type'] == 'slick' || $display['type'] == 'slick_' . $field['type']) {
$is_slick = TRUE;
break;
}
}
if ($is_slick) {
$target_type = $field['type'] . '_item';
$column = 'value';
$target_ids = [];
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
if (isset($item[$column])) {
$target_ids[] = $item[$column];
}
}
}
$target_entities = [];
if ($target_ids) {
$target_entities = entity_load($target_type, $target_ids);
}
foreach ($entities as $id => $entity) {
$rekey = FALSE;
foreach ($items[$id] as $delta => $item) {
if (is_array($target_entities) && isset($target_entities[$item[$column]]) && isset($target_entities[$item[$column]])) {
$items[$id][$delta]['entity'] = $target_entities[$item[$column]];
$has_view_access = entity_access('view', $target_type, $target_entities[$item[$column]]) !== FALSE;
$has_update_access = entity_access('update', $target_type, $target_entities[$item[$column]]) !== FALSE;
$items[$id][$delta]['access'] = $has_view_access || $has_update_access;
}
else {
unset($items[$id][$delta]);
$rekey = TRUE;
}
}
if ($rekey) {
$items[$id] = array_values($items[$id]);
}
}
}
}
$this->isViewPrepared[$field['type']] = TRUE;
}
return $this->isViewPrepared[$field['type']];
}
}