TestBoldTextBehavior.php in Paragraphs 8
File
tests/modules/paragraphs_test/src/Plugin/paragraphs/Behavior/TestBoldTextBehavior.php
View source
<?php
namespace Drupal\paragraphs_test\Plugin\paragraphs\Behavior;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
class TestBoldTextBehavior extends ParagraphsBehaviorBase {
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;
}
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';
}
}
public static function isApplicable(ParagraphsType $paragraphs_type) {
if ($paragraphs_type
->id() != 'text_paragraph_test') {
return TRUE;
}
return FALSE;
}
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'),
],
];
}
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 [];
}
}