FieldLink.php in (Entity Reference) Field Formatters 8
File
src/Plugin/Field/FieldFormatter/FieldLink.php
View source
<?php
namespace Drupal\field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Render\Element;
class FieldLink extends FieldWrapperBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$field_output = $this
->getFieldOutput($items, $langcode);
$elements = [];
foreach (Element::children($field_output) as $key) {
$elements[$key] = [
'#type' => 'link',
'#url' => $items
->getEntity()
->toUrl(),
'#title' => $field_output[$key],
];
}
return $elements;
}
}