StringFormatter.php 5.18 KB
Newer Older
1
2
<?php

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
12
13
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15

/**
16
 * Plugin implementation of the 'string' formatter.
17
 *
18
 * @FieldFormatter(
19
 *   id = "string",
20
21
 *   label = @Translation("Plain text"),
 *   field_types = {
22
 *     "string",
23
 *     "uri",
24
 *   },
25
 *   quickedit = {
26
 *     "editor" = "plain_text"
27
28
29
 *   }
 * )
 */
30
31
class StringFormatter extends FormatterBase implements ContainerFactoryPluginInterface {

32
33
34
35
36
37
38
  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  /**
   * Constructs a StringFormatter instance.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings settings.
56
57
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   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
61
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);

62
    $this->entityTypeManager = $entity_type_manager;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $plugin_id,
      $plugin_definition,
      $configuration['field_definition'],
      $configuration['settings'],
      $configuration['label'],
      $configuration['view_mode'],
      $configuration['third_party_settings'],
77
      $container->get('entity_type.manager')
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    $options = parent::defaultSettings();

    $options['link_to_entity'] = FALSE;
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::settingsForm($form, $form_state);

97
    $entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
98

99
100
101
102
103
104
105
106
107
108
109
110
111
    $form['link_to_entity'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Link to the @entity_label', ['@entity_label' => $entity_type->getLabel()]),
      '#default_value' => $this->getSetting('link_to_entity'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  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
128
      // For the default revision this falls back to 'canonical'.
      $url = $this->getEntityUrl($items->getEntity());
129
130
    }

131
    foreach ($items as $delta => $item) {
132
      $view_value = $this->viewValue($item);
133
134
135
      if ($url) {
        $elements[$delta] = [
          '#type' => 'link',
136
          '#title' => $view_value,
137
138
139
140
          '#url' => $url,
        ];
      }
      else {
141
        $elements[$delta] = $view_value;
142
      }
143
144
145
146
    }
    return $elements;
  }

147
148
149
150
151
152
  /**
   * Generate the output appropriate for one field item.
   *
   * @param \Drupal\Core\Field\FieldItemInterface $item
   *   One field item.
   *
153
154
   * @return array
   *   The textual output generated as a render array.
155
156
157
158
   */
  protected function viewValue(FieldItemInterface $item) {
    // The text value has no text format assigned to it, so the user input
    // should equal the output, including newlines.
159
    return [
160
161
162
      '#type' => 'inline_template',
      '#template' => '{{ value|nl2br }}',
      '#context' => ['value' => $item->value],
163
    ];
164
165
  }

166
167
168
169
170
171
172
173
174
175
176
177
178
179
  /**
   * Gets the URI elements of the entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity object.
   *
   * @return \Drupal\Core\Url
   *   The URI elements of the entity.
   */
  protected function getEntityUrl(EntityInterface $entity) {
    // For the default revision this falls back to 'canonical'.
    return $entity->toUrl('revision');
  }

180
}