You are here

function _menu_attributes_form_alter in Menu Attributes 6

Same name and namespace in other branches
  1. 8 menu_attributes.module \_menu_attributes_form_alter()
  2. 6.2 menu_attributes.module \_menu_attributes_form_alter()
  3. 7 menu_attributes.module \_menu_attributes_form_alter()

Add the menu attributes to a menu item edit form.

Parameters

$form: The menu item edit form passed by reference.

$item: The optional existing menu item for context.

2 calls to _menu_attributes_form_alter()
menu_attributes_form_alter in ./menu_attributes.module
Implements hook_form_alter().
menu_attributes_form_menu_edit_item_alter in ./menu_attributes.module
Implements hook_form_FORM_ID_alter().

File

./menu_attributes.module, line 164
Alters the menu item form to allow the administrator to specify additional attributes for the menu link

Code

function _menu_attributes_form_alter(&$form, $item = array(), &$complete_form) {
  $form['options']['#tree'] = TRUE;
  $form['options']['#weight'] = 50;

  // Unset the previous value so that the new values get saved.
  unset($form['options']['#value']['attributes']);
  $form['options']['attributes'] = array(
    '#type' => 'fieldset',
    '#title' => t('Menu item attributes'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );
  $attributes = menu_attributes_get_menu_attribute_info();
  foreach ($attributes as $attribute => $info) {

    // Merge in the proper default value.
    if (isset($item['options']['attributes'][$attribute])) {

      // If the menu link already has this attribute, use it.
      $info['form']['#default_value'] = $item['options']['attributes'][$attribute];
    }
    elseif ($item['mlid']) {

      // If this is an existing link, use the raw default (usually empty).
      $info['form']['#default_value'] = $info['default'];
    }
    $form['options']['attributes'][$attribute] = $info['form'] + array(
      '#access' => $info['enabled'],
    );
  }

  // Add form values for the reset of $item['options'] and
  // $item['options']['attributes'] so the values will carry over during save.
  foreach ($item['options'] as $key => $value) {
    if ($key !== 'attributes' && !isset($form['options'][$key])) {
      $form['options'][$key] = array(
        '#type' => 'value',
        '#value' => $value,
      );
    }
  }
  if (isset($item['options']['attributes'])) {
    foreach ($item['options']['attributes'] as $key => $value) {
      if (!isset($form['options']['attributes'][$key])) {
        $form['options']['attributes'][$key] = array(
          '#type' => 'value',
          '#value' => $value,
        );
      }
    }
  }

  // Hide the 'description' field since we will be using our own 'title' field.
  if (isset($form['description'])) {
    $form['description']['#access'] = FALSE;

    // Because this form uses a special $form['description'] field which is
    // really the 'title' attribute, we need to add special pre-submit handling
    // to ensure our field gets saved as the title attribute.
    array_unshift($complete_form['#submit'], '_menu_attributes_form_submit');
  }
}