3 namespace Drupal\paragraphs_test\Plugin\paragraphs\Behavior;
5 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\paragraphs\Entity\Paragraph;
8 use Drupal\paragraphs\ParagraphInterface;
9 use Drupal\paragraphs\ParagraphsBehaviorBase;
12 * Provides a test feature plugin.
14 * @ParagraphsBehavior(
15 * id = "test_text_color",
16 * label = @Translation("Test text color behavior plugin"),
17 * description = @Translation("Test text color behavior plugin"),
21 class TestTextColorBehavior extends ParagraphsBehaviorBase {
26 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
27 $form['default_color'] = [
28 '#type' => 'textfield',
29 '#title' => $this->t('Default Color'),
31 '#default_value' => $this->configuration['default_color'],
32 '#description' => $this->t("Text color for the paragraph."),
40 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
41 if ($form_state->getValue('default_color') == 'red') {
42 $form_state->setErrorByName('default_color', $this->t('Red can not be used as the default color.'));
49 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
50 $this->configuration['default_color'] = $form_state->getValue('default_color');
56 public function defaultConfiguration() {
58 'default_color' => 'blue',
65 public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
66 $form['text_color'] = [
67 '#type' => 'textfield',
68 '#title' => $this->t('Color'),
70 '#default_value' => $paragraph->getBehaviorSetting($this->getPluginId(), 'text_color', $this->configuration['default_color']),
71 '#description' => $this->t("Text color for the paragraph."),
79 public function validateBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
80 if ($form_state->getValue('text_color') != 'blue' && $form_state->getValue('text_color') != 'red') {
81 $form_state->setError($form, 'The only allowed values are blue and red.');
88 public function view(array &$build, Paragraph $paragraphs_entity, EntityViewDisplayInterface $display, $view_mode) {
89 if ($color = $paragraphs_entity->getBehaviorSetting($this->getPluginId(), 'text_color')) {
90 $build['#attributes']['class'][] = $color . '_plugin_text';
91 $build['#attached']['library'][] = 'paragraphs_test/drupal.paragraphs_test.color_text';
98 public function settingsSummary(Paragraph $paragraph) {
99 $text_color = $paragraph->getBehaviorSetting($this->pluginId, 'text_color');
100 return [$this->t('Text color: @color', ['@color' => $text_color])];