Advertisement
Guest User

Custom formatter for field link

a guest
Nov 13th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\customrcpf\Plugin\Field\FieldFormatter;
  4.  
  5. use Drupal\Component\Utility\Unicode;
  6. use Drupal\Core\Field\FieldDefinitionInterface;
  7. use Drupal\Core\Field\FieldItemListInterface;
  8. use Drupal\Core\Field\FormatterBase;
  9. use Drupal\Core\Form\FormStateInterface;
  10. use Drupal\Core\Path\PathValidatorInterface;
  11. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  12. use Drupal\Core\Url;
  13. use Drupal\link\LinkItemInterface;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15.  
  16. /**
  17. * Plugin implementation of the 'rcpf_link' formatter.
  18. *
  19. * @FieldFormatter(
  20. * id = "rcpf_link",
  21. * label = @Translation("RCPF link"),
  22. * field_types = {
  23. * "link"
  24. * }
  25. * )
  26. */
  27. class RCPFLinkFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
  28.  
  29. /**
  30. * The path validator service.
  31. *
  32. * @var \Drupal\Core\Path\PathValidatorInterface
  33. */
  34. protected $pathValidator;
  35.  
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  40. return new static(
  41. $plugin_id,
  42. $plugin_definition,
  43. $configuration['field_definition'],
  44. $configuration['settings'],
  45. $configuration['label'],
  46. $configuration['view_mode'],
  47. $configuration['third_party_settings'],
  48. $container->get('path.validator')
  49. );
  50. }
  51.  
  52. /**
  53. * Constructs a new LinkFormatter.
  54. *
  55. * @param string $plugin_id
  56. * The plugin_id for the formatter.
  57. * @param mixed $plugin_definition
  58. * The plugin implementation definition.
  59. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  60. * The definition of the field to which the formatter is associated.
  61. * @param array $settings
  62. * The formatter settings.
  63. * @param string $label
  64. * The formatter label display setting.
  65. * @param string $view_mode
  66. * The view mode.
  67. * @param array $third_party_settings
  68. * Third party settings.
  69. * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
  70. * The path validator service.
  71. */
  72. public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, PathValidatorInterface $path_validator) {
  73. parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
  74. $this->pathValidator = $path_validator;
  75. }
  76.  
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public static function defaultSettings() {
  81. return [
  82. 'trim_length' => '80',
  83. 'url_only' => '',
  84. 'url_plain' => '',
  85. 'rel' => '',
  86. 'target' => '',
  87. ] + parent::defaultSettings();
  88. }
  89.  
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function viewElements(FieldItemListInterface $items, $langcode) {
  94. $element = [];
  95. $entity = $items->getEntity();
  96.  
  97. foreach ($items as $delta => $item) {
  98. $url = $item->getUrl() ?: Url::fromRoute('<none>');
  99.  
  100. $title = \Drupal::token()->replace($item->title, [$entity->getEntityTypeId() => $entity], ['clear' => TRUE]);
  101.  
  102. $element[$delta] = [
  103. '#theme' => 'customrcpf_formatter_link',
  104. '#title' => $title,
  105. '#url' => $url,
  106. ];
  107.  
  108. if (!empty($item->_attributes)) {
  109. $element[$delta]['#options'] += ['attributes' => []];
  110. $element[$delta]['#options']['attributes'] += $item->_attributes;
  111. // Unset field item attributes since they have been included in the
  112. // formatter output and should not be rendered in the field template.
  113. unset($item->_attributes);
  114. }
  115.  
  116. }
  117.  
  118. return $element;
  119. }
  120.  
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement