3 namespace Drupal\slick\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FormatterBase;
8 use Drupal\Core\Field\FieldDefinitionInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Drupal\slick\SlickFormatterInterface;
12 use Drupal\slick\SlickManagerInterface;
13 use Drupal\slick\SlickDefault;
16 * Plugin implementation of the 'Slick Text' formatter.
20 * label = @Translation("Slick Text"),
24 * "text_with_summary",
26 * quickedit = {"editor" = "disabled"}
29 class SlickTextFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
31 use SlickFormatterTrait;
34 * Constructs a SlickImageFormatter instance.
36 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, SlickFormatterInterface $formatter, SlickManagerInterface $manager) {
37 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
38 $this->formatter = $formatter;
39 $this->manager = $manager;
45 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49 $configuration['field_definition'],
50 $configuration['settings'],
51 $configuration['label'],
52 $configuration['view_mode'],
53 $configuration['third_party_settings'],
54 $container->get('slick.formatter'),
55 $container->get('slick.manager')
62 public static function defaultSettings() {
63 return SlickDefault::baseSettings();
69 public function viewElements(FieldItemListInterface $items, $langcode) {
70 // Early opt-out if the field is empty.
71 if ($items->isEmpty()) {
75 $settings = $this->getSettings();
76 $settings['vanilla'] = TRUE;
78 // Build the settings.
79 $build = ['settings' => $settings];
80 $this->formatter->buildSettings($build, $items);
82 // The ProcessedText element already handles cache context & tag bubbling.
83 // @see \Drupal\filter\Element\ProcessedText::preRenderText()
84 foreach ($items as $key => $item) {
86 '#type' => 'processed_text',
87 '#text' => $item->value,
88 '#format' => $item->format,
89 '#langcode' => $item->getLangcode(),
91 $build['items'][$key] = $element;
95 return $this->manager()->build($build);
101 public function settingsForm(array $form, FormStateInterface $form_state) {
103 $definition = $this->getScopedFormElements();
105 $this->admin()->buildSettingsForm($element, $definition);
110 * Defines the scope for the form elements.
112 public function getScopedFormElements() {
114 'current_view_mode' => $this->viewMode,
115 'no_layouts' => TRUE,
116 'plugin_id' => $this->getPluginId(),
117 'settings' => $this->getSettings(),