values are not being set

Created on 10 December 2023, about 1 year ago

I have created a custom plugin and want to set the values I obtained from the fields, but the
values are not being set
The submitConfigurationForm function is not executed

<?php

declare(strict_types=1);

namespace Drupal\custom_style_layout_paragraphs\Plugin\StyleOption;

use Drupal\Core\Form\FormStateInterface;
use Drupal\style_options\Plugin\StyleOptionPluginBase;
use Drupal\style_options\StyleOptionStyleTrait;

/**
 * Define the image attribute option plugin.
 *
 * @StyleOption(
 *   id = "custom_background_color",
 *   label = @Translation("Custom Background Color"),
 * )
 */
class CustomBackgroundColor extends StyleOptionPluginBase {

  use StyleOptionStyleTrait;

  /**
   * {@inheritDoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
    $form['custom_background_color'] = [
      '#type' => 'textfield',
      '#title' => $this->getLabel(),
      '#default_value' => $this->getValue('custom_background_color') ?? $this->getDefaultValue(),
      '#wrapper_attributes' => [
        'class' => [$this->getConfiguration()['custom_background_color'] ?? ''],
      ],
      '#description' => $this->getConfiguration('description'),
    ];
    $allowed_colors = [];
    foreach ($this->getConfiguration()['options'] as $option) {
      $allowed_colors[] = $option['color'];
    }
    $form['custom_background_color']['#color_values'] = implode(',', $allowed_colors);
    if ($this->hasConfiguration('options')) {
      $form['custom_background_color']['#type'] = 'color_picker';
    }
    if ($this->hasConfiguration('multiple')) {
      $form['custom_background_color']['#multiple'] = TRUE;
    }

    return $form;
  }

  public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
    parent::submitConfigurationForm($form, $form_state);

    $value = $form_state->getValue('custom_background_color');

    if (!empty($value)) {
      $this->setValue('custom_background_color', $value);
    }
    else {
      $this->setValue('custom_background_color', NULL);
    }
  }

  /**
   * {@inheritDoc}
   */
  public function build(array $build) {
    $value = $this->getValue('custom_background_color') ?? NULL;
    if (!empty($value)) {
      if ($this->getConfiguration('method') == 'css') {
        $this->generateStyle($build, ['#color' => $value]);
      }
      else {
        $build['#attributes']['style'][] = "background-color: $value;";
      }
    }

    return $build;
  }

}
🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇮🇷Iran genius.ha

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @genius.ha
  • Issue was unassigned.
  • 🇦🇹Austria crmn

    maybe its because of filterBehaviorFormSubmitValues() in ParagraphsBehaviorBase (extended by StyleOptionBehavior):
    /*
    * Default implementation considers a value to be default if and only if it is
    * an empty value. Behavior plugins that do not consider all empty values to
    * be default should override this method or
    * \Drupal\paragraphs\ParagraphsBehaviorBase::submitBehaviorForm.
    */

  • Did you solve this problem. I can not figure it out. It happens only for paragraphs. For layout elements the submitConfigurationForm is executed.

  • Did you solve this problem? It happens only for paragraphs. For layout elements the submitConfigurationForm is executed.

  • Did you solve this problem? It happens only for paragraphs. For layout elements the submitConfigurationForm is executed.

Production build 0.71.5 2024