3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
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;
16 * Plugin implementation of the 'string' formatter.
20 * label = @Translation("Plain text"),
26 * "editor" = "plain_text"
30 class StringFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
33 * The entity type manager.
35 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
37 protected $entityTypeManager;
40 * Constructs a StringFormatter instance.
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
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.
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);
62 $this->entityTypeManager = $entity_type_manager;
68 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
84 public static function defaultSettings() {
85 $options = parent::defaultSettings();
87 $options['link_to_entity'] = FALSE;
94 public function settingsForm(array $form, FormStateInterface $form_state) {
95 $form = parent::settingsForm($form, $form_state);
97 $entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
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'),
111 public function settingsSummary() {
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()]);
123 public function viewElements(FieldItemListInterface $items, $langcode) {
126 if ($this->getSetting('link_to_entity')) {
127 $url = $this->getEntityUrl($items->getEntity());
130 foreach ($items as $delta => $item) {
131 $view_value = $this->viewValue($item);
133 $elements[$delta] = [
135 '#title' => $view_value,
140 $elements[$delta] = $view_value;
147 * Generate the output appropriate for one field item.
149 * @param \Drupal\Core\Field\FieldItemInterface $item
153 * The textual output generated as a render array.
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.
159 '#type' => 'inline_template',
160 '#template' => '{{ value|nl2br }}',
161 '#context' => ['value' => $item->value],
166 * Gets the URI elements of the entity.
168 * @param \Drupal\Core\Entity\EntityInterface $entity
171 * @return \Drupal\Core\Url
172 * The URI elements of the entity.
174 protected function getEntityUrl(EntityInterface $entity) {
175 // For the default revision, the 'revision' link template falls back to
177 // @see \Drupal\Core\Entity\Entity::toUrl()
178 $rel = $entity->getEntityType()->hasLinkTemplate('revision') ? 'revision' : 'canonical';
179 return $entity->toUrl($rel);