BasicStringFormatter.php 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
  3. use Drupal\Core\Field\FormatterBase;
  4. use Drupal\Core\Field\FieldItemListInterface;
  5. /**
  6. * Plugin implementation of the 'basic_string' formatter.
  7. *
  8. * @FieldFormatter(
  9. * id = "basic_string",
  10. * label = @Translation("Plain text"),
  11. * field_types = {
  12. * "string_long",
  13. * "email"
  14. * },
  15. * quickedit = {
  16. * "editor" = "plain_text"
  17. * }
  18. * )
  19. */
  20. class BasicStringFormatter extends FormatterBase {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function viewElements(FieldItemListInterface $items, $langcode) {
  25. $elements = [];
  26. foreach ($items as $delta => $item) {
  27. // The text value has no text format assigned to it, so the user input
  28. // should equal the output, including newlines.
  29. $elements[$delta] = [
  30. '#type' => 'inline_template',
  31. '#template' => '{{ value|nl2br }}',
  32. '#context' => ['value' => $item->value],
  33. ];
  34. }
  35. return $elements;
  36. }
  37. }