FlexsliderEntityFormatter.php in Flex Slider 8.2
File
flexslider_fields/src/Plugin/Field/FieldFormatter/FlexsliderEntityFormatter.php
View source
<?php
namespace Drupal\flexslider_fields\Plugin\Field\FieldFormatter;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\flexslider\Entity\Flexslider;
class FlexsliderEntityFormatter extends EntityReferenceEntityFormatter {
use FlexsliderFormatterTrait;
public static function defaultSettings() {
return parent::defaultSettings() + self::getDefaultSettings();
}
public function settingsSummary() {
return array_merge(parent::settingsSummary(), $this
->buildSettingsSummary());
}
public function settingsForm(array $form, FormStateInterface $form_state) {
return parent::settingsForm($form, $form_state) + $this
->buildSettingsForm();
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$entities = parent::viewElements($items, $langcode);
if (empty($entities)) {
return [];
}
$formatter_settings = $this
->getSettings();
if ($optionset = Flexslider::load($formatter_settings['optionset'])) {
$cache_tags = $optionset
->getCacheTags();
}
else {
$cache_tags = [];
}
$items = [];
foreach ($entities as $delta => &$entity) {
if ($cache_tags) {
$entity['#cache']['tags'] = Cache::mergeTags($entity['#cache']['tags'], $cache_tags);
}
$item = [];
$item['slide'] = $entity;
$items[$delta] = $item;
}
return [
'#theme' => 'flexslider',
'#flexslider' => [
'settings' => $formatter_settings,
'items' => $items,
],
'#prefix' => '<div class="flexslider-field-wrapper">',
'#suffix' => '</div>',
];
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return parent::isApplicable($field_definition) && $field_definition
->getFieldStorageDefinition()
->isMultiple();
}
public function calculateDependencies() {
return parent::calculateDependencies() + $this
->getOptionsetDependencies();
}
public function onDependencyRemoval(array $dependencies) {
$changed = parent::onDependencyRemoval($dependencies);
if ($this
->optionsetDependenciesDeleted($dependencies)) {
$changed = TRUE;
}
return $changed;
}
}