VueLinkFormatter.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Drupal\vue_link_formatter\Plugin\Field\FieldFormatter;
  3. use Drupal\Component\Utility\Unicode;
  4. use Drupal\Core\Field\FieldItemListInterface;
  5. use Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter;
  6. use Drupal\Core\Form\FormStateInterface;
  7. /**
  8. * Plugin implementation of the 'link_separate' formatter.
  9. *
  10. * @todo https://www.drupal.org/node/1829202 Merge into 'link' formatter once
  11. * there is a #type like 'item' that can render a compound label and content
  12. * outside of a form context.
  13. *
  14. * @FieldFormatter(
  15. * id = "vue_link_formatter",
  16. * label = @Translation("Vue Link"),
  17. * field_types = {
  18. * "link"
  19. * }
  20. * )
  21. */
  22. class VueLinkFormatter extends LinkFormatter {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function defaultSettings() {
  27. return [
  28. 'trim_length' => 80,
  29. 'link_text' => '',
  30. 'event_modifiers' => NULL,
  31. 'methode' => '',
  32. ] + parent::defaultSettings();
  33. }
  34. public function settingsForm(array $parentForm, FormStateInterface $form_state) {
  35. $parentForm = parent::settingsForm($parentForm, $form_state);
  36. $settings = $this->getSettings();
  37. $form['link_text'] = [
  38. '#type' => 'textfield',
  39. '#title' => $this->t('Link text, leave empty for default'),
  40. '#default_value' => $settings['link_text'],
  41. ];
  42. $form['methode'] = [
  43. '#type' => 'textfield',
  44. '#title' => $this->t('Vue methode for @click vue-attribute.'),
  45. '#default_value' => $settings['methode'],
  46. '#required' => TRUE,
  47. ];
  48. $form['event_modifiers'] = [
  49. '#type' => 'checkboxes',
  50. '#title' => $this->t('Event modifiers'),
  51. '#default_value' => $settings['event_modifiers'],
  52. // '#empty_option' => $this->t('None'),
  53. '#options' => [
  54. 'prevent' => $this->t('.prevent'),
  55. 'stop' => $this->t('.stop'),
  56. 'capture' => $this->t('.capture'),
  57. 'self' => $this->t('.self'),
  58. 'once' => $this->t('.once'),
  59. 'passive' => $this->t('.passive'),
  60. ],
  61. ];
  62. return $form + $parentForm;
  63. }
  64. public function settingsSummary() {
  65. $settings = $this->getSettings();
  66. $summary = [];
  67. if (!empty($settings['link_text'])) {
  68. $summary[] = $this->t('Link text: @text"', ['@text' => $settings['link_text']]);
  69. }
  70. if (!empty($settings['methode'])) {
  71. $summary[] = $this->t('Click Methode: @text', ['@text' => $settings['methode']]);
  72. }
  73. if (!empty($settings['event_modifiers'])) {
  74. $summary[] = $this->t('Event modifier: @text', ['@text' => implode('.', $settings['event_modifiers'])]);
  75. }
  76. // TODO: add key event
  77. return $summary;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function viewElements(FieldItemListInterface $items, $langcode) {
  83. $element = array();
  84. $entity = $items->getEntity();
  85. $settings = $this->getSettings();
  86. foreach ($items as $delta => $item) {
  87. // By default use the full URL as the link text.
  88. $url = $this->buildUrl($item);
  89. $link_title = $url->toString();
  90. // If the link text field value is available, use it for the text.
  91. if (empty($settings['url_only']) && !empty($item->title)) {
  92. // Unsanitized token replacement here because the entire link title
  93. // gets auto-escaped during link generation in
  94. // \Drupal\Core\Utility\LinkGenerator::generate().
  95. $link_title = \Drupal::token()->replace($item->title, [$entity->getEntityTypeId() => $entity], ['clear' => TRUE]);
  96. }
  97. if (!empty($settings['link_text'])) {
  98. $link_title = $this->t($settings['link_text']);
  99. }
  100. // The link_separate formatter has two titles; the link text (as in the
  101. // field values) and the URL itself. If there is no link text value,
  102. // $link_title defaults to the URL, so it needs to be unset.
  103. // The URL version may need to be trimmed as well.
  104. if (empty($item->title) && empty($settings['link_text'])) {
  105. $link_title = NULL;
  106. }
  107. $url_title = $url->toString();
  108. if (!empty($settings['trim_length'])) {
  109. $link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE);
  110. $url_title = Unicode::truncate($url_title, $settings['trim_length'], FALSE, TRUE);
  111. }
  112. $element[$delta] = array(
  113. '#theme' => 'link_formatter_vue_link_formatter',
  114. '#title' => $link_title,
  115. '#url_title' => $url_title,
  116. '#url' => $url,
  117. '#methode' => $settings['methode'],
  118. '#event_modifiers' => $settings['event_modifiers'],
  119. );
  120. $attributes = [];
  121. if (!empty($item->_attributes)) {
  122. // Set our RDFa attributes on the <a> element that is being built.
  123. $url->setOption('attributes', $item->_attributes);
  124. // Unset field item attributes since they have been included in the
  125. // formatter output and should not be rendered in the field template.
  126. unset($item->_attributes);
  127. }
  128. }
  129. return $element;
  130. }
  131. }