You are here

function menu_link_field_formatter_view in Menu Link (Field) 7

Implements hook_field_formatter_view().

File

./menu_link.field.inc, line 404
Defines a menu link field type.

Code

function menu_link_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // If the default formatter is set to "hidden" but the field is being
  // displayed using this formatter, some module don't properly invoke
  // hook_field_formatter_prepare_view() which is essential for this formatter.
  // Let's make sure it has been invoked.
  // TODO remove if resolved.
  if ($instance['display']['default']['type'] == 'hidden') {
    list($id, , ) = entity_extract_ids($entity_type, $entity);
    $items = array(
      $id => &$items,
    );
    menu_link_field_formatter_prepare_view($entity_type, array(
      $id => $entity,
    ), $field, array(
      $id => $instance,
    ), $langcode, $items, array(
      $id => $display,
    ));
    $items = $items[$id];
  }
  switch ($display['type']) {
    case 'menu_link_link':
      foreach ($items as $delta => $item) {

        // Don't display a link if the entity wasn't saved yet, i.e. in preview.
        if (empty($item['href'])) {
          $menus = menu_get_menus();
          $element[$delta] = array(
            '#markup' => '<span class="menu-link-preview-title">' . $item['link_title'] . '</span>&nbsp;' . t('in') . '&nbsp;' . '<span class="menu-link-preview-menu">' . $menus[$item['menu_name']] . '</span>',
          );
        }
        else {
          $element[$delta] = array(
            '#type' => 'link',
            '#title' => $item['title'],
            '#href' => $item['href'],
            '#options' => $item['localized_options'],
          );
        }
      }
      break;
  }
  return $element;
}