View source
<?php
namespace Drupal\jquery_carousel\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class jCarouselFieldFormatter extends ImageFormatterBase implements ContainerFactoryPluginInterface {
protected $currentUser;
protected $imageStyleStorage;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->currentUser = $current_user;
$this->imageStyleStorage = $image_style_storage;
}
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.manager')
->getStorage('image_style'));
}
public static function defaultSettings() {
return [
'theme' => 'default',
'selector' => 'rs-carousel',
'style_name' => 'thumbnail',
'itemsPerTransition' => 'auto',
'orientation' => 'horizontal',
'loop' => FALSE,
'whitespace' => FALSE,
'nextPrevActions' => TRUE,
'pagination' => FALSE,
'speed' => 'normal',
'easing' => 'swing',
'autoScroll' => TRUE,
'interval' => 8000,
'continuous' => FALSE,
'touch' => TRUE,
] + parent::defaultSettings();
}
public static function jqueryCarouselSelectorValidate(array &$element, FormStateInterface $form_state) {
$selector = $element['#value'];
$error = _jquery_carousel_config_validate($selector);
if ($error) {
$form_state
->setErrorByName('selector', t("Selector should not contain any special characters or spaces. Only special character allowed is '-'"));
}
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$carousel_config_form = jquery_carousel_config_form();
$carousel_config_form['style_name'] = [
'#type' => 'select',
'#title' => t('Image Style'),
'#description' => t('Select the image style to be associated.'),
'#options' => image_style_options(),
'#weight' => -1,
'#default_value' => '',
];
$elements = array_merge($elements, $carousel_config_form);
$elements['selector']['#element_validate'] = [
[
get_class($this),
'jqueryCarouselSelectorValidate',
],
];
foreach (array_keys($elements) as $key) {
if (isset($elements[$key]) && is_array($elements[$key])) {
$elements[$key]['#default_value'] = $this
->getSetting($key);
}
}
return $elements;
}
public function settingsSummary() {
$summary = [];
$summary[] = t('Displays multivalued image field content in form of a carousel.');
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$files = $this
->getEntitiesToView($items, $langcode);
if (empty($files)) {
return $elements;
}
$items = [];
foreach ($files as $delta => $file) {
$item = $file->_referringItem;
$items[$delta] = $item;
}
$elements = [
'#theme' => 'jquery_carousel_field_formatter',
'#elements' => $items,
'#items' => $files,
'#display' => $this->viewMode,
'#settings' => $this
->getSettings(),
];
return $elements;
}
}