Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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\EntityTypeManagerInterface;
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    * The entity type manager.
34    *
35    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
36    */
37   protected $entityTypeManager;
38
39   /**
40    * Constructs a StringFormatter instance.
41    *
42    * @param string $plugin_id
43    *   The plugin_id for the formatter.
44    * @param mixed $plugin_definition
45    *   The plugin implementation definition.
46    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
47    *   The definition of the field to which the formatter is associated.
48    * @param array $settings
49    *   The formatter settings.
50    * @param string $label
51    *   The formatter label display setting.
52    * @param string $view_mode
53    *   The view mode.
54    * @param array $third_party_settings
55    *   Any third party settings settings.
56    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
57    *   The entity type manager.
58    */
59   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
60     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
61
62     $this->entityTypeManager = $entity_type_manager;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
69     return new static(
70       $plugin_id,
71       $plugin_definition,
72       $configuration['field_definition'],
73       $configuration['settings'],
74       $configuration['label'],
75       $configuration['view_mode'],
76       $configuration['third_party_settings'],
77       $container->get('entity_type.manager')
78     );
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public static function defaultSettings() {
85     $options = parent::defaultSettings();
86
87     $options['link_to_entity'] = FALSE;
88     return $options;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function settingsForm(array $form, FormStateInterface $form_state) {
95     $form = parent::settingsForm($form, $form_state);
96
97     $entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
98
99     $form['link_to_entity'] = [
100       '#type' => 'checkbox',
101       '#title' => $this->t('Link to the @entity_label', ['@entity_label' => $entity_type->getLabel()]),
102       '#default_value' => $this->getSetting('link_to_entity'),
103     ];
104
105     return $form;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function settingsSummary() {
112     $summary = [];
113     if ($this->getSetting('link_to_entity')) {
114       $entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
115       $summary[] = $this->t('Linked to the @entity_label', ['@entity_label' => $entity_type->getLabel()]);
116     }
117     return $summary;
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public function viewElements(FieldItemListInterface $items, $langcode) {
124     $elements = [];
125     $url = NULL;
126     if ($this->getSetting('link_to_entity')) {
127       // For the default revision this falls back to 'canonical'.
128       $url = $this->getEntityUrl($items->getEntity());
129     }
130
131     foreach ($items as $delta => $item) {
132       $view_value = $this->viewValue($item);
133       if ($url) {
134         $elements[$delta] = [
135           '#type' => 'link',
136           '#title' => $view_value,
137           '#url' => $url,
138         ];
139       }
140       else {
141         $elements[$delta] = $view_value;
142       }
143     }
144     return $elements;
145   }
146
147   /**
148    * Generate the output appropriate for one field item.
149    *
150    * @param \Drupal\Core\Field\FieldItemInterface $item
151    *   One field item.
152    *
153    * @return array
154    *   The textual output generated as a render array.
155    */
156   protected function viewValue(FieldItemInterface $item) {
157     // The text value has no text format assigned to it, so the user input
158     // should equal the output, including newlines.
159     return [
160       '#type' => 'inline_template',
161       '#template' => '{{ value|nl2br }}',
162       '#context' => ['value' => $item->value],
163     ];
164   }
165
166   /**
167    * Gets the URI elements of the entity.
168    *
169    * @param \Drupal\Core\Entity\EntityInterface $entity
170    *   The entity object.
171    *
172    * @return \Drupal\Core\Url
173    *   The URI elements of the entity.
174    */
175   protected function getEntityUrl(EntityInterface $entity) {
176     // For the default revision this falls back to 'canonical'.
177     return $entity->toUrl('revision');
178   }
179
180 }