StringFormatter.php 5.2 KB

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