You are here

function mlidselector_field_widget_form in Views Menu Support 8

Same name and namespace in other branches
  1. 7 mlidselector.module \mlidselector_field_widget_form()

Implements hook_field_widget_form().

File

./mlidselector.module, line 26
Functions to provide a menu item reference widget for integer fields.

Code

function mlidselector_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Get a list of human-readable menu names for this field.
  $menus =& $instance['widget']['settings']['menus'];
  $menu_names = menu_get_menus();
  $filtered_menus = array();
  foreach ($menus as $key => $value) {
    if ($value) {
      $filtered_menus[$key] = $menu_names[$key];
    }
  }

  // Get a raw list of all menu items for the relevant menus.
  $items_raw = menu_parent_options($filtered_menus, array(
    'mlid' => 0,
  ));

  // Add special settings for this widget type.
  $menu_list = array(
    '' => t('No page selected'),
    -1 => t('Front page'),
  );

  // Get some variables we need when building the MLID lists below.
  $frontpage_path = variable_get('site_frontpage');
  $menu_counter = -4;

  // Loop through the list of menu items and process them.
  foreach ($items_raw as $key => $name) {
    $id = explode(':', $key);

    // Remove links to front page, unless the setting to include them is set.
    if (!$instance['widget']['settings']['show_front']) {

      // Check for both direct linking and links to <front>. If this is a fron
      // page link, just skip to the next item.
      $menu_item = menu_link_load($id[1]);
      $path =& $menu_item['link_path'];
      if ($path == $frontpage_path || $path == '<front>') {
        continue;
      }
    }

    // If menu item has no key (because it's a menu name, not a menu item, and
    // thus should not be available for choosing but still be visible in the
    // select list for clarity), set a negative number as key. These start on
    // -4, because previous numbers are used for special purposes.
    // (blame @Itangalo)
    if ($id[1] == 0) {
      $menu_list[$menu_counter] = $name;
      $menu_counter--;
    }
    else {
      $menu_list[$id[1]] = $name;
    }
  }

  // Get saved default value, if any.
  $value = isset($items[$delta]['value']) ? $items[$delta]['value'] : '';
  $element += array(
    'value' => array(
      '#type' => 'select',
      '#options' => $menu_list,
      '#title' => $element['#title'],
      '#default_value' => $value,
    ),
  );
  return $element;
}