You are here

function menu_fields_entity_view in Menu Item Fields 7

Implements hook_entity_view().

File

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

Code

function menu_fields_entity_view($entity, $type, $view_mode, $langcode) {
  if ($type != 'menu_fields') {
    return;
  }
  list(, , $bundle) = entity_extract_ids('menu_fields', $entity);
  $link = menu_link_load($entity->mlid);
  if (!$link) {
    return;
  }

  // Get our extra field display values.
  $extra_fields = field_info_extra_fields('menu_fields', $bundle, 'display');

  // Taken directly from menu.inc function menu_tree_page_data().
  $active_trail = array(
    0 => 0,
  );
  $active_path = menu_tree_get_path($link['menu_name']);
  $active_link = menu_link_get_preferred($active_path, $link['menu_name']);

  // Use all the coordinates, except the last one because there
  // can be no child beyond the last column.
  for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
    if ($link['p' . $i]) {
      $active_trail[$active_link['p' . $i]] = $active_link['p' . $i];
    }
  }
  $active_trail = array_filter($active_trail);
  if (!in_array($link['plid'], $active_trail)) {
    $active_trail[] = $link['plid'];
  }

  // This is the main link, just as it would be printed without this module,
  // only a little more wrappers around.
  if ($extra_fields['menu_fields_link']['display'][$view_mode]['visible']) {

    // We need to get all links from the same parent, otherwise the classes
    // (first, last) will not be correct.
    $parameters = array(
      'active_trail' => $active_trail,
      'only_active_trail' => FALSE,
      'min_depth' => $link['depth'],
      'max_depth' => $link['depth'],
      'conditions' => array(
        'plid' => $link['plid'],
      ),
    );

    // Probably bad for performance, but necessary,
    // see https://www.drupal.org/node/1697570
    drupal_static_reset('_menu_build_tree');
    $tree = menu_build_tree($link['menu_name'], $parameters);

    // Now find the item in the tree that represents the current link and get
    // it's key.
    $link_key = NULL;
    foreach ($tree as $key => $element) {
      if ($element['link']['mlid'] == $entity->mlid) {
        $link_key = $key;
      }
    }

    // Mark it, so that we won't run into a loop.
    $tree[$link_key]['link']['render_menu_fields'] = FALSE;

    // Prepare the output.
    $tree_data = menu_tree_output($tree);
    foreach (element_children($tree_data) as $key) {
      if ($key != $entity->mlid) {
        unset($tree_data[$key]);
      }
    }
    menu_fields_assure_localized_options_classes($tree_data);

    // And put everything into the entities content storage.
    $entity->content['menu_fields_link'] = $tree_data;
    $entity->content['menu_fields_link']['#weight'] = $extra_fields['menu_fields_link']['display'][$view_mode]['weight'];
    $entity->content['menu_fields_link']['#theme_wrappers'][] = 'menu_fields_menu_tree__' . strtr($link['menu_name'], '-', '_');
  }

  // These are the men links children.
  if ($extra_fields['menu_fields_link_children']['display'][$view_mode]['visible'] && $link['has_children']) {
    $parameters = array(
      'active_trail' => array(
        $link['plid'],
      ),
      'only_active_trail' => FALSE,
      'min_depth' => $link['depth'] + 1,
      'max_depth' => $link['depth'] + 1,
      'conditions' => array(
        'plid' => $link['mlid'],
      ),
    );
    $tree = menu_build_tree($link['menu_name'], $parameters);
    $entity->content['menu_fields_link_children'] = menu_tree_output($tree);
    $entity->content['menu_fields_link_children']['#weight'] = $extra_fields['menu_fields_link_children']['display'][$view_mode]['weight'];
  }

  // The path that the link targets.
  if ($extra_fields['menu_fields_link_path']['display'][$view_mode]['visible']) {
    $entity->content['menu_fields_link_path'] = array(
      '#type' => 'item',
      '#markup' => url($link['href']),
      '#weight' => $extra_fields['menu_fields_link_path']['display'][$view_mode]['weight'],
    );
  }

  // And the title of the link.
  if ($extra_fields['menu_fields_link_title']['display'][$view_mode]['visible']) {
    $entity->content['menu_fields_link_title'] = array(
      '#type' => 'item',
      '#markup' => $link['link_title'],
      '#weight' => $extra_fields['menu_fields_link_title']['display'][$view_mode]['weight'],
    );
  }

  // Finally, after everything has been assembled,
  // we need to add another wrapper around this item,
  // so that it appears in an <li> element with the appropriate attributes.
  $tree_data = array(
    $link,
  );
  $tree_data = menu_tree_data($tree_data, $active_trail);

  // If the access to the menu item is denied,
  // menu_tree_output will return an empty array.
  // This can happen for views preview.
  $build = menu_tree_output($tree_data);
  if (!empty($build)) {
    menu_fields_assure_localized_options_classes($build);
    $entity->content['#prefix'] = '<li' . drupal_attributes($entity->content['menu_fields_link'][$link['mlid']]['#attributes']) . '>';
    $entity->content['#suffix'] = '</li>';
  }
}