class TestTextColorBehavior

Provides a test feature plugin.

Plugin annotation


@ParagraphsBehavior(
  id = "test_text_color",
  label = @Translation("Test text color behavior plugin"),
  description = @Translation("Test text color behavior plugin"),
  weight = 1
)

Hierarchy

Expanded class hierarchy of TestTextColorBehavior

File

paragraphs/tests/modules/paragraphs_test/src/Plugin/paragraphs/Behavior/TestTextColorBehavior.php, line 21

Namespace

Drupal\paragraphs_test\Plugin\paragraphs\Behavior
View source
class TestTextColorBehavior extends ParagraphsBehaviorBase {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['default_color'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Default Color'),
      '#maxlength' => 255,
      '#default_value' => $this->configuration['default_color'],
      '#description' => $this
        ->t("Text color for the paragraph."),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('default_color') == 'red') {
      $form_state
        ->setErrorByName('default_color', $this
        ->t('Red can not be used as the default color.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['default_color'] = $form_state
      ->getValue('default_color');
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'default_color' => 'blue',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
    $form['text_color'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Color'),
      '#maxlength' => 255,
      '#default_value' => $paragraph
        ->getBehaviorSetting($this
        ->getPluginId(), 'text_color', $this->configuration['default_color']),
      '#description' => $this
        ->t("Text color for the paragraph."),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('text_color') != 'blue' && $form_state
      ->getValue('text_color') != 'red') {
      $form_state
        ->setError($form, 'The only allowed values are blue and red.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function view(array &$build, Paragraph $paragraphs_entity, EntityViewDisplayInterface $display, $view_mode) {
    if ($color = $paragraphs_entity
      ->getBehaviorSetting($this
      ->getPluginId(), 'text_color')) {
      $build['#attributes']['class'][] = $color . '_plugin_text';
      $build['#attached']['library'][] = 'paragraphs_test/drupal.paragraphs_test.color_text';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary(Paragraph $paragraph) {
    $text_color = $paragraph
      ->getBehaviorSetting($this->pluginId, 'text_color');
    return [
      [
        'label' => $this
          ->t('Text color'),
        'value' => $text_color,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParagraphsBehaviorBase::$entityFieldManager protected property The entity field manager.
ParagraphsBehaviorBase::calculateDependencies public function
ParagraphsBehaviorBase::create public static function 6
ParagraphsBehaviorBase::filterBehaviorFormSubmitValues protected function Removes default behavior form values before storing the user-set ones.
ParagraphsBehaviorBase::getConfiguration public function
ParagraphsBehaviorBase::getFieldNameOptions public function Returns list of field names for the given paragraph type and field type. Overrides ParagraphsBehaviorInterface::getFieldNameOptions
ParagraphsBehaviorBase::isApplicable public static function Returns if the plugin can be used for the provided Paragraphs type. Overrides ParagraphsBehaviorInterface::isApplicable 1
ParagraphsBehaviorBase::preprocess public function Adds a default set of helper variables for preprocessors and templates. Overrides ParagraphsBehaviorInterface::preprocess 1
ParagraphsBehaviorBase::setConfiguration public function
ParagraphsBehaviorBase::settingsIcon public function Returns a short info icon for the current behavior settings. Overrides ParagraphsBehaviorInterface::settingsIcon 1
ParagraphsBehaviorBase::submitBehaviorForm public function Submit the values taken from the form to store the values. Overrides ParagraphsBehaviorInterface::submitBehaviorForm 3
ParagraphsBehaviorBase::__construct public function Constructs a ParagraphsBehaviorBase object. 6
TestTextColorBehavior::buildBehaviorForm public function Builds a behavior perspective for each paragraph based on its type. Overrides ParagraphsBehaviorBase::buildBehaviorForm
TestTextColorBehavior::buildConfigurationForm public function Overrides ParagraphsBehaviorBase::buildConfigurationForm
TestTextColorBehavior::defaultConfiguration public function Overrides ParagraphsBehaviorBase::defaultConfiguration
TestTextColorBehavior::settingsSummary public function Returns a short summary for the current behavior settings. Overrides ParagraphsBehaviorBase::settingsSummary
TestTextColorBehavior::submitConfigurationForm public function Overrides ParagraphsBehaviorBase::submitConfigurationForm
TestTextColorBehavior::validateBehaviorForm public function Validates the behavior fields form. Overrides ParagraphsBehaviorBase::validateBehaviorForm
TestTextColorBehavior::validateConfigurationForm public function Overrides ParagraphsBehaviorBase::validateConfigurationForm
TestTextColorBehavior::view public function Extends the paragraph render array with behavior. Overrides ParagraphsBehaviorInterface::view