You are here

function menu_token_form_menu_edit_item_alter in Menu Token 7

Same name and namespace in other branches
  1. 6 menu_token.admin.inc \menu_token_form_menu_edit_item_alter()

Implementation of hook_form_FORM_ID_alter().

File

./menu_token.module, line 378
Main module file for the Menu Token module.

Code

function menu_token_form_menu_edit_item_alter(&$form, &$form_state) {
  if (in_array($form['module']['#value'], array(
    'menu',
    'system',
  ))) {
    $types = menu_token_get_plugin_types();
    $options = $form['options']['#value'];

    // Replace fake path (/menutoken/ouruid) with user inputed one.
    if (!empty($options['menu_token_link_path']) && !empty($options['menu_token_link_uuid'])) {
      $form['menu_token_uuid'] = array(
        '#type' => 'hidden',
        '#value' => $options['menu_token_link_uuid'],
      );
      $form['link_path']['#default_value'] = $options['menu_token_link_path'];
    }
    $form['link_title']['#weight'] = -5;
    $form['link_path']['#weight'] = -4;
    $form['menu_token_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('<strong>Use tokens</strong> in title and in path.'),
      '#description' => t('Active this option in order to use Menu token.'),
      '#default_value' => isset($options['menu_token_link_path']),
      '#weight' => -3,
    );
    $form['menu_token_options'] = array(
      '#type' => 'fieldset',
      '#title' => t('Menu Token options'),
      '#collapsible' => TRUE,
      '#weight' => -2,
      '#states' => array(
        'visible' => array(
          ':input[name="menu_token_enabled"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    foreach ($types as $type => $items) {
      $info = token_get_info($type);
      $default = NULL;
      if (isset($form_state['values']['menu_token_type_' . $type])) {
        $default = $form_state['values']['menu_token_type_' . $type];
      }
      elseif (!empty($options['menu_token_data'][$type])) {
        $default = $options['menu_token_data'][$type]['plugin'];
      }
      $form['menu_token_options'][$type] = array(
        '#type' => 'container',
      );
      $form['menu_token_options'][$type]['menu_token_type_' . $type] = array(
        '#type' => 'select',
        '#title' => t('Method for') . ' ' . $info['name'],
        '#description' => $info['description'],
        '#options' => array(
          '_none' => t('Disabled'),
        ),
        '#default_value' => isset($default) && in_array($default, array_keys($items)) ? $default : array(
          '_none',
        ),
        '#ajax' => array(
          'callback' => 'menu_token_method_callback',
          'wrapper' => 'menu-token-method-options-' . $type,
          'method' => 'replace',
          'effect' => 'fade',
        ),
      );
      foreach ($items as $name => $label) {
        $form['menu_token_options'][$type]['menu_token_type_' . $type]['#options'][$name] = $label;
      }
      $form['menu_token_options'][$type]['menu_token_method_options_wrapper'] = array(
        '#type' => 'container',
        '#prefix' => '<div id="menu-token-method-options-' . $type . '">',
        '#suffix' => '</div>',
      );
      if (isset($default) && ($handler = menu_token_get_handler($default))) {
        if ($append = $handler
          ->form_options($options['menu_token_data'][$type]['options'])) {
          $form['menu_token_options'][$type]['menu_token_method_options_wrapper']['menu_token_method_options'] = array(
            '#type' => 'fieldset',
            '#title' => t('Method options'),
            '#collapsible' => TRUE,
          ) + $append;
        }
      }
    }
    $form['menu_token_options']['menu_token_title_localize'] = array(
      '#type' => 'checkbox',
      '#title' => t('Localize resulting title.'),
      '#description' => t('After replacing all tokens, pass resulting title through t() function. <strong>FOR SECURITY</strong>, only when it has no tokens.'),
      '#default_value' => isset($options['menu_token_options']['title_localize']) ? $options['menu_token_options']['title_localize'] : '',
    );
    $form['menu_token_options']['menu_token_clear'] = array(
      '#type' => 'checkbox',
      '#title' => t('Remove token if replacement is not present'),
      '#description' => t('If the replacement token is not available on the page being viewed, the token will be removed if checked.'),
      '#default_value' => isset($options['menu_token_options']['clear']) ? $options['menu_token_options']['clear'] : '',
    );

    // Create new fieldset.
    $form['menu_token_replacement_patterns'] = array(
      '#type' => 'fieldset',
      '#title' => t('Replacement patterns'),
      '#collapsible' => FALSE,
      '#weight' => -1,
      '#states' => array(
        'visible' => array(
          ':input[name="menu_token_enabled"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['menu_token_replacement_patterns']['patterns'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array_keys($types),
      '#dialog' => TRUE,
    );

    // Add custom validation and submit functions.
    array_unshift($form['#validate'], 'menu_token_form_menu_edit_item_validate');
    array_unshift($form['#submit'], 'menu_token_form_menu_edit_item_submit');
    foreach (array_keys(menu_token_get_plugins()) as $plugin) {
      if ($handler = menu_token_get_handler($plugin)) {
        $handler
          ->form_alter($form, $form_state);
      }
    }
  }
}