<?php
namespace Drupal\paragraphs_collection\Plugin\paragraphs\Behavior;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Drupal\paragraphs_collection\StyleDiscoveryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;
protected $yamlStyleDiscovery;
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, StyleDiscoveryInterface $yaml, AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
$this->yamlStyleDiscovery = $yaml;
$this->currentUser = $current_user;
}
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('paragraphs_collection.style_discovery'), $container
->get('current_user'));
}
public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'paragraphs_collection/plugin_admin';
$wrapper_id = Html::getUniqueId('style-wrapper');
$form['style_wrapper'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'paragraphs-plugin-inline-container',
'multiple-lines',
'paragraphs-style-select',
],
'id' => $wrapper_id,
],
];
$paragraph_styles = $this
->getStyles($paragraph);
foreach (array_keys($this->configuration['groups']) as $group_id) {
$group_default = $this->configuration['groups'][$group_id]['default'];
$default_style = $paragraph_styles[$group_id] ?? NULL;
$style_options = $this
->getStyleOptions($group_id, $group_default, TRUE);
$disabled = FALSE;
if ($default_style && !$paragraph
->isNew()) {
$default_style_definition = $this->yamlStyleDiscovery
->getStyle($default_style);
if ($default_style_definition && !$this->yamlStyleDiscovery
->isAllowedAccess($default_style_definition)) {
$style_options[$default_style] = $default_style_definition['title'];
$disabled = TRUE;
}
}
if ($disabled && !$paragraph
->isNew() || count($style_options) > 1 || count($style_options) === 1 && !$group_default) {
$form['style_wrapper']['styles'][$group_id] = [
'#type' => 'select',
'#title' => $this->yamlStyleDiscovery
->getGroupWidgetLabel($group_id),
'#options' => $style_options,
'#default_value' => $default_style,
'#attributes' => [
'class' => [
'paragraphs-style',
],
],
'#disabled' => $disabled,
'#attributes' => [
'class' => [
'paragraphs-plugin-form-element',
],
],
];
if (empty($group_default)) {
$form['style_wrapper']['styles'][$group_id]['#empty_option'] = $this
->t('- Default -');
}
}
}
if (empty($form['style_wrapper']['styles'])) {
$form = [];
return [];
}
return $form;
}
public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$paragraph
->setBehaviorSettings($this->pluginId, $form_state
->getValue('style_wrapper', []));
}
protected function getStyleOptions($group = '', $default_style_key = '', $access_check = FALSE) {
$styles = $this->yamlStyleDiscovery
->getStyleOptions($group, $access_check);
if (isset($styles[$default_style_key])) {
return [
$default_style_key => $this
->t('- @name -', [
'@name' => $styles[$default_style_key],
]),
] + $styles;
}
return $styles;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['groups'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Style groups'),
'#description' => $this
->t('Restrict available styles to certain style groups.'),
'#options' => $this->yamlStyleDiscovery
->getStyleGroupsLabel(),
'#default_value' => array_keys($this->configuration['groups']),
'#ajax' => [
'callback' => [
$this,
'updateDefaultStyle',
],
'wrapper' => 'style-wrapper',
],
];
$group_key = [
'behavior_plugins',
$this
->getPluginId(),
'settings',
'groups',
];
$groups = $form_state
->getCompleteFormState()
->getValue($group_key, $this->configuration['groups']);
$form['groups_defaults'] = [
'#type' => 'container',
'#prefix' => '<div id="style-wrapper">',
'#suffix' => '</div>',
];
foreach (array_keys(array_filter($groups)) as $group_id) {
$default = '';
if (!empty($this->configuration['groups'][$group_id]['default'])) {
$default = $this->configuration['groups'][$group_id]['default'];
}
$group_label = $this->yamlStyleDiscovery
->getGroupLabel($group_id);
$form['groups_defaults'][$group_id]['default'] = [
'#type' => 'select',
'#title' => $this
->t('@label default style', [
'@label' => $group_label,
]),
'#empty_option' => $this
->t('- None -'),
'#options' => $this->yamlStyleDiscovery
->getStyleOptions($group_id),
'#description' => $this
->t('Default option for the @label group on a behavior form.', [
'@label' => $group_label,
]),
'#default_value' => $default,
];
}
return $form;
}
public static function updateDefaultStyle(array $form, FormStateInterface $form_state) {
$group_select = $form_state
->getTriggeringElement();
$settings_form = NestedArray::getValue($form, array_slice($group_select['#array_parents'], 0, -2));
return $settings_form['groups_defaults'];
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
if (empty($this->yamlStyleDiscovery
->getStyleGroups())) {
$form_state
->setErrorByName('message', $this
->t('There is no style group available, the style plugin can not be enabled.'));
}
if (!array_filter($form_state
->getValue('groups'))) {
$form_state
->setErrorByName('groups', $this
->t('The style plugin cannot be enabled if no groups are selected.'));
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['groups'] = $form_state
->getValue('groups_defaults');
}
public function defaultConfiguration() {
return [
'groups' => [],
];
}
public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {
$paragraph_styles = $this
->getStyles($paragraph);
foreach ($paragraph_styles as $enabled_style) {
$style = $this->yamlStyleDiscovery
->getStyle($enabled_style);
if ($style) {
$build['#attributes']['class'][] = 'paragraphs-behavior-' . $this
->getPluginId() . '--' . $style['name'];
if (!isset($build['#attached']['library'])) {
$build['#attached']['library'] = [];
}
$build['#attached']['library'] = array_merge($style['libraries'], $build['#attached']['library']);
if (!empty($style['classes'])) {
$build['#attributes']['class'] = array_merge($style['classes'], $build['#attributes']['class']);
}
if (!empty($style['attributes'])) {
$build['#attributes'] = array_merge($style['attributes'], $build['#attributes']);
}
}
}
}
public function settingsSummary(Paragraph $paragraph) {
$style_options = $this->yamlStyleDiscovery
->getStyleOptions();
$summary = [];
if ($styles = $paragraph
->getBehaviorSetting($this
->getPluginId(), 'styles')) {
foreach ($styles as $group_id => $style) {
if (isset($style_options[$style]) && (!isset($this->configuration['groups'][$group_id]) || $style != $this->configuration['groups'][$group_id]['default'])) {
$summary[] = [
'label' => $this->yamlStyleDiscovery
->getGroupWidgetLabel($group_id),
'value' => $style_options[$style],
];
}
}
}
return $summary;
}
public static function ajaxStyleSelect(array $form, FormStateInterface $form_state) {
$select = $form_state
->getTriggeringElement();
$style_discovery = \Drupal::getContainer()
->get('paragraphs_collection.style_discovery');
$styles = $style_discovery
->getStyles();
$description = '';
if (isset($styles[$select['#value']]['description'])) {
$description = $styles[$select['#value']]['description'];
}
$return_form = NestedArray::getValue($form, array_slice($select['#array_parents'], 0, -2));
$return_form['style_wrapper']['style_description']['#markup'] = $description;
return $return_form;
}
public function getStyleTemplates(ParagraphInterface $paragraph) {
if ($paragraph
->getParagraphType()
->hasEnabledBehaviorPlugin('style')) {
$templates = [];
$paragraph_styles = $this
->getStyles($paragraph);
foreach ($paragraph_styles as $group_name => $paragraph_style) {
if ($style = $this->yamlStyleDiscovery
->getStyle($paragraph_style)) {
if (!empty($style['template'])) {
$templates[] = $style['template'];
}
}
}
return $templates;
}
return NULL;
}
public function getStyles(ParagraphInterface $paragraph) {
$defaults = [];
foreach ($this->configuration['groups'] as $group_id => $group_configuration) {
$defaults[$group_id] = $group_configuration['default'];
}
if ($styles_config = $paragraph
->getBehaviorSetting('style', 'styles')) {
foreach ($styles_config as $group_id => $style) {
if ($style && $this->yamlStyleDiscovery
->getStyle($style) && isset($this->configuration['groups'][$group_id])) {
$defaults[$group_id] = $style;
}
}
}
elseif ($style_config = $paragraph
->getBehaviorSetting('style', 'style')) {
if ($style = $this->yamlStyleDiscovery
->getStyle($style_config)) {
foreach (array_keys($this->configuration['groups']) as $group_id) {
if (in_array($group_id, $style['groups'])) {
$defaults[$group_id] = $style_config;
}
}
}
}
return $defaults;
}
}