class TestBoldTextBehavior

Provides a test feature plugin.

Plugin annotation


@ParagraphsBehavior(
  id = "test_bold_text",
  label = @Translation("Test bold text plugin"),
  description = @Translation("Test bold text plugin"),
  weight = 2
)

Hierarchy

Expanded class hierarchy of TestBoldTextBehavior

File

paragraphs/tests/modules/paragraphs_test/src/Plugin/paragraphs/Behavior/TestBoldTextBehavior.php, line 22

Namespace

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

  /**
   * {@inheritdoc}
   */
  public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
    $form['bold_text'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Bold Text'),
      '#default_value' => $paragraph
        ->getBehaviorSetting($this
        ->getPluginId(), 'bold_text', FALSE),
      '#description' => $this
        ->t("Bold text for the paragraph."),
    ];
    return $form;
  }

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

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(ParagraphsType $paragraphs_type) {

    // If the name of the field is not text_paragraph_test then allow using this
    // plugin.
    if ($paragraphs_type
      ->id() != 'text_paragraph_test') {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary(Paragraph $paragraph) {
    $bold_setting = $paragraph
      ->getBehaviorSetting($this
      ->getPluginId(), 'bold_text');
    return [
      [
        'label' => $this
          ->t('Bold'),
        'value' => $bold_setting ? $this
          ->t('Yes') : $this
          ->t('No'),
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsIcon(Paragraph $paragraph) {
    $bold_setting = $paragraph
      ->getBehaviorSetting($this
      ->getPluginId(), 'bold_text');
    if ($bold_setting) {
      return [
        'bold' => [
          '#theme' => 'paragraphs_info_icon',
          '#message' => $this
            ->t('Bold: Yes.'),
          '#icon' => 'bold',
        ],
      ];
    }
    return [];
  }

}

Members

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