You are here

mlidselector.module in Views Menu Support 8

Same filename and directory in other branches
  1. 7 mlidselector.module

Functions to provide a menu item reference widget for integer fields.

File

mlidselector.module
View source
<?php

/**
 * @file
 * Functions to provide a menu item reference widget for integer fields.
 */

/**
 * Implements hook_field_widget_info().
 */
function mlidselector_field_widget_info() {
  return array(
    'field_mlid_reference' => array(
      'label' => t('Menu item reference'),
      'field types' => array(
        'number_integer',
      ),
      'settings' => array(
        'menus' => array(),
        'show_front' => FALSE,
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
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;
}

/**
 * Implements hook_field_widget_settings_form().
 */
function mlidselector_field_widget_settings_form($field, $instance) {
  $form = array();

  // Create these variables by reference. No need to increase memory usage just
  // because we want to write variables in a readable way.
  $widget =& $instance['widget'];
  $settings =& $widget['settings'];
  if ($widget['type'] == 'field_mlid_reference') {
    $form['menus'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Selectable menus'),
      '#default_value' => $settings['menus'],
      '#options' => menu_get_menus(),
      '#description' => t('Select which menus should be possible to refer to.'),
      '#weight' => -1,
      '#required' => TRUE,
    );
    $form['show_front'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable front page menu items'),
      '#description' => t('Check this box to enable all menu items linking to the front page.'),
      '#default_value' => $settings['show_front'],
      '#weight' => -1,
    );
  }
  return $form;
}

/**
 * Impements hook_form_FORM_ID_alter().
 *
 * This form alter is introduced to hide some of the normal settings on integer
 * fields.
 */
function mlidselector_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  if ($form['instance']['widget']['type']['#value'] == 'field_mlid_reference') {
    $form['instance']['settings']['min']['#access'] = FALSE;
    $form['instance']['settings']['max']['#access'] = FALSE;
    $form['instance']['settings']['prefix']['#access'] = FALSE;
    $form['instance']['settings']['suffix']['#access'] = FALSE;
  }
}

Functions

Namesort descending Description
mlidselector_field_widget_form Implements hook_field_widget_form().
mlidselector_field_widget_info Implements hook_field_widget_info().
mlidselector_field_widget_settings_form Implements hook_field_widget_settings_form().
mlidselector_form_field_ui_field_edit_form_alter Impements hook_form_FORM_ID_alter().