You are here

public function Menu::buildLinkFieldAsMenu in Helper 8

Builds a render array of a set of links like a menu.

Parameters

string $menu_name: The menu name (any value can be provided).

\Drupal\Core\Field\FieldItemListInterface $items: A link field item list.

Return value

array The render array.

File

src/Menu.php, line 209

Class

Menu
Provides helpers working with menus and menu links.

Namespace

Drupal\helper

Code

public function buildLinkFieldAsMenu($menu_name, FieldItemListInterface $items) {
  assert($items
    ->getFieldDefinition()
    ->getType() === 'link');
  $build = [
    '#theme' => 'menu__' . strtr($menu_name, '-', '_'),
    '#menu_name' => $menu_name,
    '#items' => $this
      ->convertLinkItemsToMenuLinks($items),
    // Make sure drupal_render() does not re-order the links.
    '#sorted' => TRUE,
  ];
  $metadata = CacheableMetadata::createFromObject($items
    ->getEntity());
  $metadata
    ->applyTo($build);
  return $build;
}