You are here

function og_menu_form_node_form_alter in Organic Groups Menu (OG Menu) 7.2

Same name and namespace in other branches
  1. 7.3 og_menu.module \og_menu_form_node_form_alter()

Implements hook_form_FORMID_alter().

Alter the node form's menu form. We modify the forms for group content and group content types.

File

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

Code

function og_menu_form_node_form_alter(&$form, &$form_state) {
  $type = $form['#node']->type;
  $link = $form['#node']->menu;
  $is_group_type = og_is_group_type('node', $type);
  $is_group_content = og_is_group_content_type('node', $type);

  // Group content.
  if ($is_group_content) {

    // Check if we are here thanks to an ajax call, if so, add url context
    if (isset($form_state['complete form']) && $form_state['complete form']['#action']) {
      $action = parse_url($form_state['complete form']['#action']);
      parse_str($action['query'], $_GET);
    }

    // Check if we get any group context from the url
    $gids = array_values(og_get_context_by_url());
    $context_based_access = FALSE;
    $group_based_access = FALSE;
    if (!empty($gids) && is_array($gids)) {
      foreach ($gids as $gid) {
        if (og_user_access($gid, 'administer og menu')) {
          $context_based_access = TRUE;
        }
      }
    }
    if (!$context_based_access && ($group = og_context())) {

      // Performance, might be a gotcha later on.
      $group_based_access = og_user_access($group->gid, 'administer og menu');
    }
    if (user_access('administer og menu') || $context_based_access || $group_based_access) {
      global $user;
      $groups = array();
      $groups = og_get_entity_groups('user', $user);
      $list = array();
      $settings = array();
      $menus = og_menu_get_group_menus($groups);
      if (!empty($menus)) {
        foreach ($menus as $menu) {
          $list[$menu->mname] = $menu->mtitle;
          $settings[$menu->mname] = $menu->gid;
        }

        // If user has administer menu permission, also show other menu options.
        if (user_access('administer menu')) {

          // Gets menus available to this content type.
          $type_menus = variable_get('menu_options_' . $type, array(
            'main-menu' => 'main-menu',
          ));
          $available_menus = array();

          // Get all existing menu's with their name.
          $result = db_query("SELECT menu_name, title FROM {menu_custom} ORDER BY title");
          while ($menuObj = $result
            ->fetchObject()) {
            if (in_array($menuObj->menu_name, $type_menus)) {
              $available_menus[$menuObj->menu_name] = $menuObj->title;
            }
          }

          // We want to merge the menus the user has available anyway and the OG ones
          $merged_list = array_merge($available_menus, $list);
        }
        else {
          $merged_list = $list;
        }
        $menus = og_menu_get_group_menus();
        foreach ($menus as $menu) {
          $settings[$menu->mname] = $menu->gid;
        }

        // Menu parent options will format the list in a way Drupal expects and give children, etc
        $item = array();
        if (isset($form['menu']['link']['mlid']['#value'])) {
          $item = menu_link_load($form['menu']['link']['mlid']['#value']);
        }
        else {
          $item['mlid'] = 0;
        }
        $options = menu_parent_options($merged_list, $item);
        if ($nid = $form['nid']['#value']) {
          $form['menu']['link']['parent']['#default_value'] = $link['menu_name'] . ':' . $link['plid'];
        }
        $form['menu']['#access'] = user_access('administer og menu') + $group_based_access + $context_based_access;
        $form['menu']['#theme'] = 'og_menu_node_form';
        $form['menu']['#settings'] = $merged_list;
        $form['menu']['link']['parent']['#options'] = $options;
        if (!user_access('administer menu')) {
          $form['#validate'][] = 'og_menu_node_form_validate';
        }
      }
    }
  }

  // Group type.
  if ($is_group_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 all menus associated with this group.'),
    );

    // @todo If we're going to delete all of the groups menus,
    //       we should ask the user for confirmation

    //$form['#submit'][] = 'og_menu_group_form_validate';
  }
}