3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
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;
16 * Plugin implementation of the 'string' formatter.
20 * label = @Translation("Plain text"),
26 * "editor" = "plain_text"
30 class StringFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
33 * Constructs a StringFormatter instance.
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
47 * @param array $third_party_settings
48 * Any third party settings settings.
49 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
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);
55 $this->entityManager = $entity_manager;
61 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
77 public static function defaultSettings() {
78 $options = parent::defaultSettings();
80 $options['link_to_entity'] = FALSE;
87 public function settingsForm(array $form, FormStateInterface $form_state) {
88 $form = parent::settingsForm($form, $form_state);
90 $entity_type = $this->entityManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
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'),
104 public function settingsSummary() {
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()]);
116 public function viewElements(FieldItemListInterface $items, $langcode) {
119 if ($this->getSetting('link_to_entity')) {
120 // For the default revision this falls back to 'canonical'.
121 $url = $this->getEntityUrl($items->getEntity());
124 foreach ($items as $delta => $item) {
125 $view_value = $this->viewValue($item);
127 $elements[$delta] = [
129 '#title' => $view_value,
134 $elements[$delta] = $view_value;
141 * Generate the output appropriate for one field item.
143 * @param \Drupal\Core\Field\FieldItemInterface $item
147 * The textual output generated as a render array.
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.
153 '#type' => 'inline_template',
154 '#template' => '{{ value|nl2br }}',
155 '#context' => ['value' => $item->value],
160 * Gets the URI elements of the entity.
162 * @param \Drupal\Core\Entity\EntityInterface $entity
165 * @return \Drupal\Core\Url
166 * The URI elements of the entity.
168 protected function getEntityUrl(EntityInterface $entity) {
169 // For the default revision this falls back to 'canonical'.
170 return $entity->toUrl('revision');