You are here

function _menu_attributes_form_alter in Menu Attributes 7

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. 6 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_menu_edit_item_alter in ./menu_attributes.module
Implements hook_form_FORM_ID_alter().
menu_attributes_form_node_form_alter in ./menu_attributes.module
Implements hook_form_FORM_ID_alter().

File

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

Code

function _menu_attributes_form_alter(array &$form, array $item = array(), 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']);
  unset($form['options']['#value']['item_attributes']);
  $form['options'][MENU_ATTRIBUTES_LINK] = array(
    '#type' => 'fieldset',
    '#title' => t('Menu link attributes'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $form['options'][MENU_ATTRIBUTES_ITEM] = array(
    '#type' => 'fieldset',
    '#title' => t('Menu item attributes'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $attributes = menu_attributes_get_menu_attribute_info();
  foreach ($attributes as $attribute => $info) {

    // If no scope is set, this attribute should be available to both link
    // and item.
    if (!isset($info['scope'])) {
      $info['scope'] = array(
        MENU_ATTRIBUTES_LINK,
        MENU_ATTRIBUTES_ITEM,
      );
    }

    // Define fields for each scope.
    foreach ($info['scope'] as $group) {

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

        // If the menu link already has this attribute, use it.
        $info['form']['#default_value'] = $item['options'][$group][$attribute];

        // Convert the classes array to a string for the form.
        if ($attribute == 'class' && is_array($info['form']['#default_value'])) {
          $info['form']['#default_value'] = implode(' ', $info['form']['#default_value']);
        }
      }
      elseif ($item['mlid']) {

        // If this is an existing link, use the raw default (usually empty).
        $info['form']['#default_value'] = $info['default'];
      }

      // If the item description is set, override the form description.
      if ($group == MENU_ATTRIBUTES_ITEM && isset($info['item_description'])) {
        $info['form']['#description'] = $info['item_description'];
      }
      $form['options'][$group][$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,
      );
    }
  }
  foreach (array(
    MENU_ATTRIBUTES_LINK,
    MENU_ATTRIBUTES_ITEM,
  ) as $group) {
    if (isset($item['options'][$group])) {
      foreach ($item['options'][$group] as $key => $value) {
        if (!isset($form['options'][$group][$key])) {
          $form['options'][$group][$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');
  }

  // Restrict access to the new form elements.
  $has_visible_children = (bool) element_get_visible_children($form['options']['attributes']);
  $user_has_access = user_access('administer menu attributes');
  $form['options']['attributes']['#access'] = $has_visible_children && $user_has_access;
  $has_visible_children = (bool) element_get_visible_children($form['options']['item_attributes']);
  $form['options']['item_attributes']['#access'] = $has_visible_children && $user_has_access;
}