3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
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;
15 * Plugin implementation of the 'string' formatter.
19 * label = @Translation("Plain text"),
25 * "editor" = "plain_text"
29 class StringFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
32 * Constructs a StringFormatter instance.
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
46 * @param array $third_party_settings
47 * Any third party settings settings.
48 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
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);
54 $this->entityManager = $entity_manager;
60 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
76 public static function defaultSettings() {
77 $options = parent::defaultSettings();
79 $options['link_to_entity'] = FALSE;
86 public function settingsForm(array $form, FormStateInterface $form_state) {
87 $form = parent::settingsForm($form, $form_state);
89 $entity_type = $this->entityManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
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'),
103 public function settingsSummary() {
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()]);
115 public function viewElements(FieldItemListInterface $items, $langcode) {
118 if ($this->getSetting('link_to_entity')) {
119 // For the default revision this falls back to 'canonical'
120 $url = $items->getEntity()->urlInfo('revision');
123 foreach ($items as $delta => $item) {
124 $view_value = $this->viewValue($item);
126 $elements[$delta] = [
128 '#title' => $view_value,
133 $elements[$delta] = $view_value;
140 * Generate the output appropriate for one field item.
142 * @param \Drupal\Core\Field\FieldItemInterface $item
146 * The textual output generated as a render array.
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.
152 '#type' => 'inline_template',
153 '#template' => '{{ value|nl2br }}',
154 '#context' => ['value' => $item->value],