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