materio-d9/web/modules/custom/vue_link_formatter/vue_link_formatter.module

61 lines
1.6 KiB
Plaintext
Raw Normal View History

<?php
/**
* @file
* Defines simple link field types.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
use Drupal\Core\Template\Attribute;
/**
* Implements hook_theme().
*/
function vue_link_formatter_theme() {
return [
'link_formatter_vue_link_formatter' => [
'variables' => [
'title' => NULL,
'url_title' => NULL,
'url' => NULL,
'methode' => NULL,
'event_modifiers' => NULL,
],
],
];
}
/**
* Prepares variables for button link field templates.
*
* This template outputs a separate title and link.
*
* Default template: link-formatter-button-link.html.twig.
*
* @param array $variables
* An associative array containing:
* - title: (optional) A descriptive or alternate title for the link, which
* may be different than the actual link text.
* - url_title: The anchor text for the link.
* - url: A \Drupal\Core\Url object.
*/
function template_preprocess_link_formatter_vue_link_formatter(&$variables) {
$url = $variables['url'];
$attributes = $url->getOption('attributes');
$attributes['class'][] = 'btn';
$attributes['href'] = $url->toString();
$modifiers = [];
foreach ($variables['event_modifiers'] as $modifier => $value) {
if ($value) {
$modifiers[] = $modifier;
}
}
$attributes['@click.' . implode('.', $modifiers)] = $variables['methode'];
$url->setOption('attributes', $attributes);
$variables['link'] = Link::fromTextAndUrl($variables['title'], $url )->toString();
$variables['attributes'] = new Attribute($attributes);
}