<?php
namespace Drupal\paragraphs_collection_demo\Plugin\paragraphs\Behavior;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Drupal\slick\Entity\Slick;
use Drupal\slick\SlickManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParagraphsSliderPlugin extends ParagraphsBehaviorBase implements ContainerFactoryPluginInterface {
protected $slickManager;
protected $entityTypeManager;
protected $moduleHandler;
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, SlickManagerInterface $slick_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, AccountProxyInterface $currentUser) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
$this->slickManager = $slick_manager;
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->currentUser = $currentUser;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_field.manager'), $container
->get('slick.manager'), $container
->get('entity_type.manager'), $container
->get('module_handler'), $container
->get('current_user'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$paragraphs_type = $form_state
->getFormObject()
->getEntity();
if ($paragraphs_type
->isNew()) {
return [];
}
$field_name_options = $this
->getFieldsByCardinalityGreaterOne($paragraphs_type);
if ($field_name_options) {
$form['field_name'] = [
'#type' => 'select',
'#title' => $this
->t('Slider field'),
'#description' => $this
->t('Choose the field to be used as slider items.'),
'#options' => $field_name_options,
'#default_value' => $this->configuration['field_name'],
];
}
else {
$form['message'] = [
'#type' => 'container',
'#markup' => $this
->t('There are no fields available with the cardinality greater than one. Please add at least one in the <a href=":link">Manage fields</a> page.', [
':link' => Url::fromRoute("entity.{$paragraphs_type->getEntityType()->getBundleOf()}.field_ui_fields", [
$paragraphs_type
->getEntityTypeId() => $paragraphs_type
->id(),
])
->toString(),
]),
'#attributes' => [
'class' => [
'messages messages--error',
],
],
];
}
$form['slick_slider'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Slick Optionsets'),
'#description' => $this
->getOptionsetDescription($this
->getAllOptionSet()),
'#options' => $this
->getAllOptionSet(),
'#default_value' => $this->configuration['slick_slider'],
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getValue('field_name')) {
$form_state
->setErrorByName('message', $this
->t('The Slider plugin cannot be enabled if there is no field to be mapped.'));
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['field_name'] = $form_state
->getValue('field_name');
$slider_optionsets = array_intersect_key($this
->getAllOptionSet(), array_flip($form_state
->getValue('slick_slider')));
$this->configuration['slick_slider'] = array_keys($slider_optionsets);
}
public function buildBehaviorForm(ParagraphInterface $paragraphs_entity, array &$form, FormStateInterface $form_state) {
$options = $this
->getAllOptionSet();
$slider_optionset = $this
->getConfiguration()['slick_slider'];
$default_value = $paragraphs_entity
->getBehaviorSetting($this
->getPluginId(), 'slick_slider');
if (!empty($slider_optionset)) {
$options = array_intersect_key($options, array_flip($slider_optionset));
}
elseif (count($options) === 1 && empty($default_value)) {
$keys = array_keys($options);
$default_value = $keys[0];
}
$form['slick_slider'] = [
'#type' => 'select',
'#title' => $this
->t('Slider'),
'#options' => $options,
'#description' => $this
->t('Slider effect used to display the paragraph.'),
'#default_value' => $default_value,
'#required' => TRUE,
];
if (!in_array($default_value, array_keys($options))) {
$form['slick_slider']['#empty_option'] = $this
->t('- None -');
}
return $form;
}
public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$paragraph
->setBehaviorSettings($this
->getPluginId(), $form_state
->getValues());
}
public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {
$elements = [];
$field_name = $this
->getConfiguration()['field_name'];
$slider_optionset = $paragraph
->getBehaviorSetting($this
->getPluginId(), 'slick_slider');
if (isset($build[$field_name])) {
$field_slides = Element::children($build[$field_name]);
foreach ($field_slides as $delta) {
$elements['items'][]['slide'] = $build[$field_name][$delta];
}
}
if ($slider_optionset) {
$elements['options'] = $this
->getOptionSet($slider_optionset);
}
$build[$field_name] = $this->slickManager
->build($elements);
}
public function settingsSummary(Paragraph $paragraph) {
$slider_optionset = $paragraph
->getBehaviorSetting('slider', 'slick_slider');
$all_optionset = $this
->getAllOptionSet();
$summary = [];
if (in_array($slider_optionset, array_flip($all_optionset))) {
$summary = [
[
'label' => $this
->t('Slider settings'),
'value' => $all_optionset[$slider_optionset],
],
];
}
return $summary;
}
public function getAllOptionSet() {
$entities = $this->slickManager
->entityLoadMultiple('slick');
$option_sets = [];
foreach ($entities as $options_set_id => $option_set) {
$option_sets[$option_set
->id()] = $option_set
->label();
}
return $option_sets;
}
public function getOptionSet($name) {
$entity = $this->slickManager
->entityLoad($name, 'slick');
return $entity ? $entity
->getSettings() : [];
}
public function defaultConfiguration() {
return [
'field_name' => '',
'slick_slider' => [],
];
}
protected function getFieldsByCardinalityGreaterOne(ParagraphsType $paragraphs_type) {
$fields = [];
$field_definitions = $this->entityFieldManager
->getFieldDefinitions('paragraph', $paragraphs_type
->id());
foreach ($field_definitions as $name => $definition) {
if ($field_definitions[$name] instanceof FieldConfigInterface) {
$cardinality = $definition
->getFieldStorageDefinition()
->getCardinality();
if ($cardinality === 1) {
continue;
}
$fields[$name] = $definition
->getLabel();
}
}
return $fields;
}
protected function getOptionsetDescription($slick_optionset_options) {
if (empty($slick_optionset_options)) {
return [
$this
->t('There are no Slick optionsets available.'),
];
}
$description = $this
->t('Select none, to show all.');
$enable_link = Url::fromRoute('system.modules_list');
$slick_link = Url::fromRoute('entity.slick.collection');
if ($this->moduleHandler
->moduleExists('slick_ui')) {
if ($slick_link
->access($this->currentUser)) {
$description = $this
->t('Select none, to show all. To have more options, go to the <a href=":link">Slick UI config page</a> and add items there.', [
':link' => $slick_link
->toString(),
]);
}
}
else {
if ($enable_link
->access($this->currentUser)) {
$description = $this
->t('Select none, to show all. Enable the <a href=":link">Slick UI</a> from the module list to create more options.', [
':link' => $enable_link
->toString(),
]);
}
}
return $description;
}
}