You are here

function menu_attributes_get_attributes in Menu Attributes 8

Get menu link attributes.

Parameters

\Drupal\Core\Menu\MenuLinkInterface $menu_link_content_plugin:

Return value

array

1 call to menu_attributes_get_attributes()
menu_attributes_set_attributes in ./menu_attributes.module
Set the attributes recursively on the given menu items.

File

./menu_attributes.module, line 344
Alters the menu item form to allow the administrator to specify additional attributes for the menu link

Code

function menu_attributes_get_attributes(MenuLinkInterface $menu_link_content_plugin) {
  $attributes = [];
  if (!$menu_link_content_plugin instanceof \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent) {
    return $attributes;
  }
  try {
    $plugin_id = $menu_link_content_plugin
      ->getPluginId();
  } catch (PluginNotFoundException $e) {
    return $attributes;
  }
  if (strpos($plugin_id, ':') === FALSE) {
    return $attributes;
  }
  list($entity_type, $uuid) = explode(':', $plugin_id, 2);
  $entity = \Drupal::service('entity.repository')
    ->loadEntityByUuid($entity_type, $uuid);
  if ($entity) {
    $options = $entity->link
      ->first()->options;
    $attributes[MENU_ATTRIBUTES_LINK] = isset($options[MENU_ATTRIBUTES_LINK]) ? $options[MENU_ATTRIBUTES_LINK] : [];
    $attributes[MENU_ATTRIBUTES_ITEM] = isset($options[MENU_ATTRIBUTES_ITEM]) ? $options[MENU_ATTRIBUTES_ITEM] : [];

    // Class attribute needs special handling because it's stored as an array.
    if (isset($attributes[MENU_ATTRIBUTES_LINK]['class'])) {
      $attributes[MENU_ATTRIBUTES_LINK]['class'] = explode(' ', $attributes[MENU_ATTRIBUTES_LINK]['class']);
    }
    if (isset($attributes[MENU_ATTRIBUTES_ITEM]['class'])) {
      $attributes[MENU_ATTRIBUTES_ITEM]['class'] = explode(' ', $attributes[MENU_ATTRIBUTES_ITEM]['class']);
    }
  }
  return $attributes;
}