improvements-2.x-dev/modules/improvements_paragraphs/src/Plugin/paragraphs/Behavior/StyleSelectorParagraphBehavior.php
modules/improvements_paragraphs/src/Plugin/paragraphs/Behavior/StyleSelectorParagraphBehavior.php
<?php namespace Drupal\improvements_paragraphs\Plugin\paragraphs\Behavior; use Drupal\Component\Serialization\Yaml; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element\Checkboxes; use Drupal\Core\Template\AttributeHelper; use Drupal\druhels\ArrayHelper; use Drupal\paragraphs\Entity\Paragraph; use Drupal\paragraphs\ParagraphInterface; use Drupal\paragraphs\ParagraphsBehaviorBase; /** * @ParagraphsBehavior( * id = "style_selector", * label = @Translation("Style selector"), * description = @Translation("Allows to select paragraph style from predefined list."), * weight = 103, * ) */ class StyleSelectorParagraphBehavior extends ParagraphsBehaviorBase { /** * {@inheritdoc} */ public function defaultConfiguration(): array { return [ 'styles' => [], ]; } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { $form['styles'] = [ '#type' => 'textarea', '#title' => $this->t('Styles'), '#description' => $this->t('Styles in YAML format. Example:') . ' <br /> <code> <pre>' . <<<CODE dark: name: Dark paragraph attributes: class: dark-paragraph custom_setting_name: custom_setting_value CODE . '</pre> </code> ', '#default_value' => $this->configuration['styles'] ? Yaml::encode($this->configuration['styles']) : '', '#rows' => 10, ]; return $form; } /** * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state): void { try { Yaml::decode($form_state->getValue('styles')); } catch (\Exception $exception) { $form_state->setErrorByName('styles', $exception->getMessage()); } } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { $this->configuration['styles'] = Yaml::decode($form_state->getValue('styles')); } /** * {@inheritdoc} */ public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state): array { $styles_options = []; foreach ($this->configuration['styles'] as $style_id => $style) { $styles_options[$style_id] = $style['name']; } $form['styles'] = [ '#type' => 'checkboxes', '#title' => $this->t('Style'), '#options' => $styles_options, '#default_value' => $paragraph->getBehaviorSetting($this->pluginId, 'styles', []), ]; $form['#weight'] = $this->pluginDefinition['weight']; return $form; } /** * {@inheritdoc} */ public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state): void { $filtered_values = $this->filterBehaviorFormSubmitValues($paragraph, $form, $form_state); $styles = !empty($filtered_values['styles']) ? Checkboxes::getCheckedCheckboxes($filtered_values['styles']) : []; $paragraph->setBehaviorSettings($this->pluginId, ['styles' => $styles]); } /** * {@inheritDoc} */ public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode): void { $all_behavior_settings = $paragraph->getAllBehaviorSettings(); // Not used getBehaviorSetting() for increase performance $selected_styles = $all_behavior_settings[$this->pluginId]['styles'] ?? []; $build['#paragraph_styles'] = ArrayHelper::removeExtraElements($this->configuration['styles'], $selected_styles); $build['#attributes'] = $build['#attributes'] ?? []; foreach ($selected_styles as $style_id) { if ($style_attributes = $this->configuration['styles'][$style_id]['attributes'] ?? NULL) { $build['#attributes'] = AttributeHelper::mergeCollections( $build['#attributes'], ArrayHelper::formatArrayAsAttributes($style_attributes) ); } } } }