public function MenuLinkBreadcrumbFormatter::viewElements in Menu Link (Field) 8
Same name and namespace in other branches
- 2.0.x src/Plugin/Field/FieldFormatter/MenuLinkBreadcrumbFormatter.php \Drupal\menu_link\Plugin\Field\FieldFormatter\MenuLinkBreadcrumbFormatter::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/ MenuLinkBreadcrumbFormatter.php, line 126
Class
- MenuLinkBreadcrumbFormatter
- Plugin implementation of the 'menu_link_breadcrumb' formatter.
Namespace
Drupal\menu_link\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$menu_links = [];
$id = $items[$delta]
->getMenuPluginId();
$parent_ids = $this->menuLinkManager
->getParentIds($id);
if (!empty($parent_ids)) {
foreach ($parent_ids as $parent_id) {
if (!$this
->getSetting('parents_only') || $parent_id != $id) {
$menu_links[] = $this->menuLinkManager
->createInstance($parent_id);
}
}
}
$menu_links = array_reverse($menu_links);
// Get the links to add to the breadcrumb.
$links = array_map(function ($link) {
if ($this
->getSetting('link_to_target')) {
$url = $link
->getUrlObject();
}
else {
// Set URL to none if we don't want to link to the menu link target.
$url = new Url('<none>');
}
return Link::fromTextAndUrl($link
->getTitle(), $url);
}, $menu_links);
// Set up the breadcrumb.
$breadcrumb = new Breadcrumb();
$breadcrumb
->setLinks($links);
$elements[$delta] = $breadcrumb
->toRenderable();
}
return $elements;
}