You are here

function views_menu_reference_field_widget_form in Views Menu Reference 7

Implements hook_field_widget_form().

File

./views_menu_reference.module, line 176
views_menu_reference module core implementations.

Code

function views_menu_reference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $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 the list of allowed menus and their menu items.
  $items_raw = menu_parent_options($filtered_menus, array(
    'mlid' => 0,
  ));

  // Create the menu list.
  $menu_list = array(
    '' => t('None'),
  );
  $menu_counter = -4;

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

      // Has no key: Is a menu parent item.
      $menu_list[$menu_counter] = $name;
      $menu_counter--;
    }
    else {
      $menu_list[$id[1]] = $name;
    }
  }

  // The mlid selector.
  $element['mlid'] = array(
    '#type' => 'select',
    '#options' => $menu_list,
    '#title' => t('Menu item'),
    '#default_value' => isset($items[$delta]['mlid']) ? $items[$delta]['mlid'] : '',
  );

  // Create the select list.
  $depth_list = array(
    '0' => t('0 (Only the menu entry itself)'),
  );
  for ($i = 1; $i <= 9; $i++) {
    $depth_list[$i] = t($i . ' (Only the entry)');
    $depth_list[$i . '+'] = t($i . '+ (Incl. subentries)');
  }

  // The depth selector
  $element['depth'] = array(
    '#type' => 'select',
    '#options' => $depth_list,
    '#title' => t('Depth'),
    '#default_value' => isset($items[$delta]['depth']) ? $items[$delta]['depth'] : '0',
    '#description' => t('The depth is being calculated from the selected menu item on. This makes it possible to select a whole menu level X levels under the selected element including or excluding sub menu items.'),
  );

  // To prevent an extra required indicator, disable the required flag on the
  // base element since all the sub-fields are already required if desired.
  $element['#required'] = FALSE;
  return $element;
}