5886ef1f2979fe10c810fe3154528d0eb9244acc
[yaffs-website] / web / modules / contrib / slick / src / Plugin / Field / FieldFormatter / SlickTextFormatter.php
1 <?php
2
3 namespace Drupal\slick\Plugin\Field\FieldFormatter;
4
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;
14
15 /**
16  * Plugin implementation of the 'Slick Text' formatter.
17  *
18  * @FieldFormatter(
19  *   id = "slick_text",
20  *   label = @Translation("Slick Text"),
21  *   field_types = {
22  *     "text",
23  *     "text_long",
24  *     "text_with_summary",
25  *   },
26  *   quickedit = {"editor" = "disabled"}
27  * )
28  */
29 class SlickTextFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
30
31   use SlickFormatterTrait;
32
33   /**
34    * Constructs a SlickImageFormatter instance.
35    */
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;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
46     return new static(
47       $plugin_id,
48       $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')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function defaultSettings() {
63     return SlickDefault::baseSettings();
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function viewElements(FieldItemListInterface $items, $langcode) {
70     // Early opt-out if the field is empty.
71     if ($items->isEmpty()) {
72       return [];
73     }
74
75     $settings = $this->getSettings();
76     $settings['vanilla'] = TRUE;
77
78     // Build the settings.
79     $build = ['settings' => $settings];
80     $this->formatter->buildSettings($build, $items);
81
82     // The ProcessedText element already handles cache context & tag bubbling.
83     // @see \Drupal\filter\Element\ProcessedText::preRenderText()
84     foreach ($items as $key => $item) {
85       $element = [
86         '#type'     => 'processed_text',
87         '#text'     => $item->value,
88         '#format'   => $item->format,
89         '#langcode' => $item->getLangcode(),
90       ];
91       $build['items'][$key] = $element;
92       unset($element);
93     }
94
95     return $this->manager()->build($build);
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function settingsForm(array $form, FormStateInterface $form_state) {
102     $element    = [];
103     $definition = $this->getScopedFormElements();
104
105     $this->admin()->buildSettingsForm($element, $definition);
106     return $element;
107   }
108
109   /**
110    * Defines the scope for the form elements.
111    */
112   public function getScopedFormElements() {
113     return [
114       'current_view_mode' => $this->viewMode,
115       'no_layouts'        => TRUE,
116       'plugin_id'         => $this->getPluginId(),
117       'settings'          => $this->getSettings(),
118     ];
119   }
120
121 }