Pull merge.
[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       $url = $this->getEntityUrl($items->getEntity());
128     }
129
130     foreach ($items as $delta => $item) {
131       $view_value = $this->viewValue($item);
132       if ($url) {
133         $elements[$delta] = [
134           '#type' => 'link',
135           '#title' => $view_value,
136           '#url' => $url,
137         ];
138       }
139       else {
140         $elements[$delta] = $view_value;
141       }
142     }
143     return $elements;
144   }
145
146   /**
147    * Generate the output appropriate for one field item.
148    *
149    * @param \Drupal\Core\Field\FieldItemInterface $item
150    *   One field item.
151    *
152    * @return array
153    *   The textual output generated as a render array.
154    */
155   protected function viewValue(FieldItemInterface $item) {
156     // The text value has no text format assigned to it, so the user input
157     // should equal the output, including newlines.
158     return [
159       '#type' => 'inline_template',
160       '#template' => '{{ value|nl2br }}',
161       '#context' => ['value' => $item->value],
162     ];
163   }
164
165   /**
166    * Gets the URI elements of the entity.
167    *
168    * @param \Drupal\Core\Entity\EntityInterface $entity
169    *   The entity object.
170    *
171    * @return \Drupal\Core\Url
172    *   The URI elements of the entity.
173    */
174   protected function getEntityUrl(EntityInterface $entity) {
175     // For the default revision, the 'revision' link template falls back to
176     // 'canonical'.
177     // @see \Drupal\Core\Entity\Entity::toUrl()
178     $rel = $entity->getEntityType()->hasLinkTemplate('revision') ? 'revision' : 'canonical';
179     return $entity->toUrl($rel);
180   }
181
182 }