You are here

function menu_attributes_set_attributes in Menu Attributes 8

Set the attributes recursively on the given menu items.

Parameters

array $items: An array of menu items.

1 call to menu_attributes_set_attributes()
menu_attributes_preprocess_menu in ./menu_attributes.module
Implements hook_preprocess_menu().

File

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

Code

function menu_attributes_set_attributes($items) {
  foreach ($items as &$item) {
    $menu_attributes = [];
    if (!empty($item['original_link'])) {
      $menu_attributes = menu_attributes_get_attributes($item['original_link']);
    }
    $menu_link_attributes = empty($menu_attributes[MENU_ATTRIBUTES_LINK]) ? [] : array_filter($menu_attributes[MENU_ATTRIBUTES_LINK]);
    $menu_item_attributes = empty($menu_attributes[MENU_ATTRIBUTES_ITEM]) ? [] : array_filter($menu_attributes[MENU_ATTRIBUTES_ITEM]);
    if (count($menu_link_attributes)) {
      $url_attributes = $item['url']
        ->getOption('attributes') ?: [];
      $attributes = array_merge($url_attributes, $menu_link_attributes);
      $item['url']
        ->setOption('attributes', $attributes);
    }
    if (count($menu_item_attributes)) {
      foreach ($menu_item_attributes as $attribute => $info) {
        $item['attributes']
          ->setAttribute($attribute, $info);
      }
    }
    if (!empty($item['below'])) {
      menu_attributes_set_attributes($item['below']);
    }
  }
}