You are here

function menu_attributes_menu_link_content_presave in Menu Attributes 8

Implements hook_ENTITY_TYPE_presave().

File

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

Code

function menu_attributes_menu_link_content_presave(EntityInterface $entity) {
  $item = $entity
    ->toArray();
  if (isset($item['options']['attributes']) && is_array($item['options']['attributes'])) {

    // Filter out blank attributes.
    foreach ($item['options']['attributes'] as $key => $value) {
      if (is_array($value) && empty($value) || is_string($value) && !mb_strlen($value)) {
        unset($item['options']['attributes'][$key]);
      }
    }

    // Convert classes to an array.
    if (isset($item['options']['attributes']['class']) && is_string($item['options']['attributes']['class'])) {
      $item['options']['attributes']['class'] = array_filter(explode(' ', $item['options']['attributes']['class']));
    }
  }
}