Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldFormatter / StringFormatter.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Field\FieldDefinitionInterface;
8 use Drupal\Core\Field\FieldItemInterface;
9 use Drupal\Core\Field\FormatterBase;
10 use Drupal\Core\Field\FieldItemListInterface;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Plugin implementation of the 'string' formatter.
17  *
18  * @FieldFormatter(
19  *   id = "string",
20  *   label = @Translation("Plain text"),
21  *   field_types = {
22  *     "string",
23  *     "uri",
24  *   },
25  *   quickedit = {
26  *     "editor" = "plain_text"
27  *   }
28  * )
29  */
30 class StringFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
31
32   /**
33    * Constructs a StringFormatter instance.
34    *
35    * @param string $plugin_id
36    *   The plugin_id for the formatter.
37    * @param mixed $plugin_definition
38    *   The plugin implementation definition.
39    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
40    *   The definition of the field to which the formatter is associated.
41    * @param array $settings
42    *   The formatter settings.
43    * @param string $label
44    *   The formatter label display setting.
45    * @param string $view_mode
46    *   The view mode.
47    * @param array $third_party_settings
48    *   Any third party settings settings.
49    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
50    *   The entity manager.
51    */
52   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityManagerInterface $entity_manager) {
53     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
54
55     $this->entityManager = $entity_manager;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
62     return new static(
63       $plugin_id,
64       $plugin_definition,
65       $configuration['field_definition'],
66       $configuration['settings'],
67       $configuration['label'],
68       $configuration['view_mode'],
69       $configuration['third_party_settings'],
70       $container->get('entity.manager')
71     );
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public static function defaultSettings() {
78     $options = parent::defaultSettings();
79
80     $options['link_to_entity'] = FALSE;
81     return $options;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function settingsForm(array $form, FormStateInterface $form_state) {
88     $form = parent::settingsForm($form, $form_state);
89
90     $entity_type = $this->entityManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
91
92     $form['link_to_entity'] = [
93       '#type' => 'checkbox',
94       '#title' => $this->t('Link to the @entity_label', ['@entity_label' => $entity_type->getLabel()]),
95       '#default_value' => $this->getSetting('link_to_entity'),
96     ];
97
98     return $form;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function settingsSummary() {
105     $summary = [];
106     if ($this->getSetting('link_to_entity')) {
107       $entity_type = $this->entityManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
108       $summary[] = $this->t('Linked to the @entity_label', ['@entity_label' => $entity_type->getLabel()]);
109     }
110     return $summary;
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function viewElements(FieldItemListInterface $items, $langcode) {
117     $elements = [];
118     $url = NULL;
119     if ($this->getSetting('link_to_entity')) {
120       // For the default revision this falls back to 'canonical'.
121       $url = $this->getEntityUrl($items->getEntity());
122     }
123
124     foreach ($items as $delta => $item) {
125       $view_value = $this->viewValue($item);
126       if ($url) {
127         $elements[$delta] = [
128           '#type' => 'link',
129           '#title' => $view_value,
130           '#url' => $url,
131         ];
132       }
133       else {
134         $elements[$delta] = $view_value;
135       }
136     }
137     return $elements;
138   }
139
140   /**
141    * Generate the output appropriate for one field item.
142    *
143    * @param \Drupal\Core\Field\FieldItemInterface $item
144    *   One field item.
145    *
146    * @return array
147    *   The textual output generated as a render array.
148    */
149   protected function viewValue(FieldItemInterface $item) {
150     // The text value has no text format assigned to it, so the user input
151     // should equal the output, including newlines.
152     return [
153       '#type' => 'inline_template',
154       '#template' => '{{ value|nl2br }}',
155       '#context' => ['value' => $item->value],
156     ];
157   }
158
159   /**
160    * Gets the URI elements of the entity.
161    *
162    * @param \Drupal\Core\Entity\EntityInterface $entity
163    *   The entity object.
164    *
165    * @return \Drupal\Core\Url
166    *   The URI elements of the entity.
167    */
168   protected function getEntityUrl(EntityInterface $entity) {
169     // For the default revision this falls back to 'canonical'.
170     return $entity->toUrl('revision');
171   }
172
173 }