A Field Formatter plugin is needed to display the field's unique properties.
<?php
namespace Drupal\smart_link\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter;
/**
* Plugin implementation of the 'smart_link_default' formatter.
*
* @FieldFormatter(
* id = "smart_link",
* label = @Translation("Smart link"),
* field_types = {"smart_link"}
* )
*/
class SmartLinkFormatter extends StringFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element_items = [];
foreach ($items as $delta => $item) {
$element_items[$delta] = [
'#type' => 'container'
];
foreach ($item->getProperties() as $property) {
$element_items[$delta][] = [
'#type' => 'inline_template',
'#template' => '<div class="field"><label>{{ label }}</label><div>{{ value|nl2br }}</div></div>',
'#context' => [
'label' => $property->getName(),
'value' => $property->getValue()
]
];
}
}
return $element_items;
}
}
Topics