You are here

function menu_fields_preprocess_menu_link in Menu Item Fields 7

Menu fields link function.

File

./menu_fields.module, line 274
Main file contain hooks/functions.

Code

function menu_fields_preprocess_menu_link(&$vars) {
  $element =& $vars['element'];
  $menu_name = $element['#original_link']['menu_name'];
  if (!menu_fields_is_enabled_menu($menu_name)) {

    // This menu isn't enabled for fields,
    // so don't change the way the link is rendered.
    return;
  }
  $vars['theme_hook_suggestions'][] = 'menu_fields_menu_link';
  $render_fields = !isset($element['#original_link']['render_menu_fields']) || $element['#original_link']['render_menu_fields'];
  if (!$render_fields) {
    return;
  }
  $mlid = $element['#original_link']['mlid'];
  $entity = menu_fields_load_by_mlid($mlid);
  if (!$entity && empty($element['#below'])) {

    // No entity is found and this menu link doesn't have any children,
    // so don't change the way the link is rendered.
    return;
  }

  // If there is no existing entity, then we create a fake entity object so that
  // menu links with children use the same render pipeline.
  // @todo Remove this if/when issue #2420525 is fixed!
  if (!$entity) {
    $entity = entity_create('menu_fields', array(
      'menu_name' => menu_fields_create_machine_name($menu_name),
      'mlid' => $mlid,
    ));
  }

  // Stash the link on the entity object so we can pass it to the template.
  $entity->link = $element['#original_link'];
  $view = entity_view('menu_fields', array(
    $entity,
  ), isset($vars['#view_mode']) ? $vars['#view_mode'] : 'default');
  $element['#menu_fields'] = $view;
  $element['#attributes']['class'][] = 'menu-fields-menu-link';
  if (!empty($element['#localized_options']['attributes']['class'])) {
    $element['#localized_options']['attributes']['class'] = array_merge($element['#localized_options']['attributes']['class'], $element['#attributes']['class']);
  }
  else {
    $element['#localized_options']['attributes']['class'] = $element['#attributes']['class'];
  }
}