You are here

function og_menu_form_alter in Organic Groups Menu (OG Menu) 6

Same name and namespace in other branches
  1. 8 og_menu.module \og_menu_form_alter()
  2. 5 og_menu.module \og_menu_form_alter()
  3. 6.2 og_menu.module \og_menu_form_alter()
  4. 7.3 og_menu.module \og_menu_form_alter()
  5. 7.2 og_menu.module \og_menu_form_alter()

Implementation of hook_form_alter().

File

./og_menu.module, line 176
Integrates Menu with Organic Groups. Lots of menu forms duplication in OG context.

Code

function og_menu_form_alter(&$form, $form_state, $form_id) {

  // Node form
  if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id && og_is_group_post_type($form['#node']->type)) {

    // Only alter the form if user does not have the administer menu permission
    if (!user_access('administer menu') && user_access('administer og menu')) {
      $groups = array();
      $group_options = $form['og_nodeapi']['visible']['og_groups']['#options'];

      // Only show menus affiliated with node groups
      foreach ($group_options as $gid => $option) {

        // Handle checkboxes and selects
        if (is_array($option)) {
          $groups[] = array_shift(array_keys($option));
        }
        else {
          $groups[] = $gid;
        }
      }
      $list = array();
      $settings = array();
      $item = $form['menu']['#item'];
      $menus = og_menu_get_menus($groups);
      foreach ($menus as $menu) {
        $list[$menu['menu_name']] = $menu['title'];
      }
      $options = menu_parent_options($list, $item);
      $form['menu']['#access'] = user_access('administer og menu');
      $form['menu']['#theme'] = 'og_menu_node_form';
      $form['menu']['parent']['#options'] = $options;
      $form['#validate'][] = 'og_menu_node_form_validate';
    }
  }
  else {
    if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id && og_is_group_type($form['#node']->type)) {
      $form['og_menu'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable menu for this group'),
        '#default_value' => empty($form['#node']->og_menu) ? 0 : 1,
        '#description' => t('Check to create a menu for this group. Uncheck to delete the menu of this group.'),
      );
    }
  }

  // Administration forms
  switch ($form_id) {
    case 'menu_edit_menu':
      if ($types = og_get_types('group')) {
        $options = array(
          '' => '--',
        );
        $values = array();

        // Populate gids
        if (user_access('administer organic groups')) {
          $result = db_query("SELECT nid, title FROM {node} WHERE type IN (" . db_placeholders($types, "varchar") . ")", $types);
          while ($group = db_fetch_object($result)) {
            $options[$group->nid] = $group->title;
          }
        }
        else {
          global $user;
          foreach ($user->og_groups as $group) {
            $options[$group['nid']] = $group['title'];
          }
        }
        $value = db_result(db_query("SELECT gid FROM {og_menu} WHERE menu_name = '%s'", $form['menu_name']['#value']));

        // Add OG checkboxes to the form
        $form['og_menu_gid'] = array(
          '#title' => t('Enable this menu for the following group'),
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => $value,
          '#weight' => 1,
        );
        if (!user_access('administer menu')) {
          $form['og_menu_gid']['#required'] = TRUE;
        }
        if (!isset($form['submit']['#weight'])) {
          $form['submit']['#weight'] = 2;
        }
        $form['#submit'][] = 'og_menu_edit_menu_form_submit';
      }
      break;
    case 'menu_delete_menu_confirm':
      $form['#submit'][] = 'og_menu_delete_menu_confirm_submit';
      break;
  }
}