vue_link_formatter.module 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @file
  4. * Defines simple link field types.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Drupal\Core\Link;
  8. use Drupal\Core\Template\Attribute;
  9. /**
  10. * Implements hook_theme().
  11. */
  12. function vue_link_formatter_theme() {
  13. return [
  14. 'link_formatter_vue_link_formatter' => [
  15. 'variables' => [
  16. 'title' => NULL,
  17. 'url_title' => NULL,
  18. 'url' => NULL,
  19. 'methode' => NULL,
  20. 'event_modifiers' => NULL,
  21. ],
  22. ],
  23. ];
  24. }
  25. /**
  26. * Prepares variables for button link field templates.
  27. *
  28. * This template outputs a separate title and link.
  29. *
  30. * Default template: link-formatter-button-link.html.twig.
  31. *
  32. * @param array $variables
  33. * An associative array containing:
  34. * - title: (optional) A descriptive or alternate title for the link, which
  35. * may be different than the actual link text.
  36. * - url_title: The anchor text for the link.
  37. * - url: A \Drupal\Core\Url object.
  38. */
  39. function template_preprocess_link_formatter_vue_link_formatter(&$variables) {
  40. $url = $variables['url'];
  41. $attributes = $url->getOption('attributes');
  42. $attributes['class'][] = 'btn';
  43. $attributes['href'] = $url->toString();
  44. $modifiers = [];
  45. foreach ($variables['event_modifiers'] as $modifier => $value) {
  46. if ($value) {
  47. $modifiers[] = $modifier;
  48. }
  49. }
  50. $attributes['@click.' . implode('.', $modifiers)] = $variables['methode'];
  51. $url->setOption('attributes', $attributes);
  52. $variables['link'] = Link::fromTextAndUrl($variables['title'], $url )->toString();
  53. $variables['attributes'] = new Attribute($attributes);
  54. }