Adds the basic Tooltip Text Formatter (no javascript).

This commit is contained in:
2021-02-18 09:32:13 +00:00
parent f3452c23a2
commit 3abcb9ccdc
3 changed files with 90 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
<?php
use Drupal\Core\Template\Attribute;
/**
* @file
* Implements hook functions needed by cdesai_tff module.
*/
/**
* Implements hook_theme().
*/
function cdesai_tff_theme($existing, $type, $theme, $path) {
return [
'cdesai_tff_tooltip_formatter' => [
'template' => 'cdesai-tff-tooltip-formatter',
'variables' => [
'value' => NULL,
'attributes' => [],
],
],
];
}
/**
* Preprocessor for cdesai_tff_tooltip_formatter theme.
*/
function template_preprocess_cdesai_tff_tooltip_formatter(&$variables) {
//$attributes = new Attribute($variables['attributes']);
}
@@ -0,0 +1,57 @@
<?php
namespace Drupal\cdesai_tff\Plugin\Field\FieldFormatter;
use Drupal\cdesai_tff\DesaiTextFormatterBase;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
/**
* Implementation of custom Tooltip text formatter.
*
* @FieldFormatter(
* id = "cdesai_tff_tooltip",
* label = @Translation("Custom Tooltip Formatter"),
* field_types = {
* "string",
* "text"
* }
* )
*/
class TooltipTextFormatter extends DesaiTextFormatterBase {
/**
* @inheritDoc
*/
function getCssClassId() {
return 'cdesai-tff-tooltip';
}
/**
* @inheritDoc
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#theme' => 'cdesai_tff_tooltip_formatter',
'#value' => $this->getValue($item),
'#attributes' => [
'class' => [$this->getCssClassId()],
],
];
}
return $elements;
}
/**
* @inheritDoc
*/
function getValue(FieldItemInterface $item) {
return $item->getString();
}
}
@@ -0,0 +1,3 @@
<div{{ attributes }}>
{{ value }}
</div>