public function MenuLinkFormatter::viewElements in Menu Link (Field) 8
Same name and namespace in other branches
- 2.0.x src/Plugin/Field/FieldFormatter/MenuLinkFormatter.php \Drupal\menu_link\Plugin\Field\FieldFormatter\MenuLinkFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ MenuLinkFormatter.php, line 115
Class
- MenuLinkFormatter
- Plugin implementation of the 'menu_link' formatter.
Namespace
Drupal\menu_link\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$id = $items[$delta]
->getMenuPluginId();
$link = $this->menuLinkManager
->createInstance($id);
if ($this
->getSetting('link_to_target')) {
$elements[$delta] = Link::fromTextAndUrl($link
->getTitle(), $link
->getUrlObject())
->toRenderable();
}
else {
// Set URL to none if we don't want to link to the menu link target.
$elements[$delta] = [
'#markup' => new HtmlEscapedText($link
->getTitle()),
];
}
}
return $elements;
}