View source
<?php
namespace Drupal\royalslider\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RoyalSliderFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, LinkGeneratorInterface $link_generator) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->currentUser = $current_user;
$this->linkGenerator = $link_generator;
}
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('link_generator'));
}
protected $currentUser;
protected $linkGenerator;
public static function defaultSettings() {
return array(
'royalslider_optionset' => '',
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$royalslider_optionsets = royalslider_optionset_options(FALSE);
$element['royalslider_optionset'] = array(
'#title' => t('OptionSet'),
'#type' => 'select',
'#default_value' => $this
->getSetting('royalslider_optionset'),
'#empty_option' => t('None (defaults)'),
'#options' => $royalslider_optionsets,
'#description' => array(
'#markup' => $this->linkGenerator
->generate($this
->t('Configure RoyalSlider OptionSets'), new Url('entity.royalslider_optionset.collection')),
'#access' => $this->currentUser
->hasPermission('administer site configuration'),
),
);
return $element;
}
public function settingsSummary() {
$summary = array();
$optionsets = royalslider_optionset_options(FALSE);
unset($optionsets['']);
$optionset_setting = $this
->getSetting('royalslider_optionset');
if (isset($optionsets[$optionset_setting])) {
$summary[] = t('OptionSet: @optionset', array(
'@optionset' => $optionsets[$optionset_setting],
));
}
else {
$summary[] = t('Default options');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items) {
$optionset_setting = $this
->getSetting('royalslider_optionset');
$optionset = entity_load('royalslider_optionset', $optionset_setting);
$entity = $items
->getEntity();
$fieldname = $items
->getName();
$slider_id = 'royalslider-' . $entity
->getEntityTypeId() . '-' . $entity
->id() . '-' . $fieldname;
$elements = array(
'#theme' => 'royalslider_formatter',
'#attributes' => array(
'id' => $slider_id,
),
'#items' => array(),
'#optionset' => $optionset_setting,
'#cache' => [
'tags' => $optionset
->getCacheTags(),
],
);
foreach ($items as $delta => $item) {
$elements['#items'][] = [
'#theme' => 'image_formatter',
'#item' => $item,
];
}
$elements['#attached'] = [
'library' => [
'royalslider/royalslider',
'royalslider/royalslider-rsdefault',
],
'drupalSettings' => [
'royalslider' => [
'optionsets' => [
$optionset
->label() => $optionset
->buildJsOptionset(),
],
'instances' => [
$slider_id => [
'optionset' => $optionset
->label(),
],
],
],
],
];
return $elements;
}
}