You are here

public function Menu::convertLinkItemsToMenuLinks in Helper 8

Convert a link field to an array of menu links.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The link field items to convert.

Return value

array The menu link items, ready for rendering in a menu.

1 call to Menu::convertLinkItemsToMenuLinks()
Menu::buildLinkFieldAsMenu in src/Menu.php
Builds a render array of a set of links like a menu.

File

src/Menu.php, line 235

Class

Menu
Provides helpers working with menus and menu links.

Namespace

Drupal\helper

Code

public function convertLinkItemsToMenuLinks(FieldItemListInterface $items) {
  $menu_links = [];

  // Generate a menu-link-style key for each link.
  $key_prefix = $items
    ->getEntity()
    ->getEntityTypeId() . ':' . $items
    ->getEntity()
    ->id() . ':' . $items
    ->getFieldDefinition()
    ->getName() . ':';
  foreach ($items as $delta => $item) {
    $link = $this
      ->convertLinkItemToLink($item);
    $menu_links[$key_prefix . $delta] = $this
      ->convertLinkToMenuLink($link);
  }
  return $menu_links;
}