You are here

function groupmenu_form_node_form_alter in Group Menu 7

Same name and namespace in other branches
  1. 8 groupmenu.module \groupmenu_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

./groupmenu.module, line 174
Integrates menu with Group.

Code

function groupmenu_form_node_form_alter(&$form, &$form_state) {
  $node = $form_state['node'];
  $type = $node->type;
  if (!empty($node->nid) && !isset($node->group)) {
    return;
  }
  if (variable_get('groupmenu_enable_' . $type, FALSE)) {

    // Available menus were discovered in groupmenu_node_prepare().
    $menus = $form['#node']->storage['groupmenu'];
    $list = array();
    if (!empty($menus)) {
      $settings['mlid'] = empty($form['#node']->menu['mlid']) ? 0 : $form['#node']->menu['mlid'];
      $settings['administer_group'] = user_access('configure group module');
      foreach ($menus as $menu) {
        $list[$menu['menu_name']] = $menu['title'];
        $settings['menus'][$menu['menu_name']] = $menu['gid'];
      }

      // If user has administer menu permission, also show other menu options.
      $settings['standard_parent_options'] = array();
      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 menus with their name.
        $result = db_query("SELECT menu_name, title FROM {menu_custom} ORDER BY title");
        while ($menu = $result
          ->fetchObject()) {
          if (in_array($menu->menu_name, $type_menus)) {
            $available_menus[$menu->menu_name] = $menu->title;
          }
        }
        $settings['standard_parent_options'] = menu_parent_options($available_menus, $type);

        // We want to merge the menus the user has available anyway and the
        // Group ones.
        $merged_list = array_merge($available_menus, $list);
      }
      else {
        $merged_list = $list;
      }

      // Get the group that this node is linked to.
      $gid = !empty($node->group) ? $node->group : 0;
      $settings['group_id'] = $gid;

      // Menu parent options will format the list in a way Drupal expects and
      // give children, etc.
      $options = menu_parent_options($merged_list, array(
        'mlid' => 0,
      ));

      // If user does not have administer menu, this field set won't be created.
      if (!isset($form['menu'])) {
        if (empty($options)) {
          return;
        }
        $link = $form['#node']->menu;
        _groupmenu_add_menufieldset($form, $options, $link);
      }
      $settings['parent_options'] = $options;
      if (isset($node->group)) {

        // Set the default menu based upon the group.
        $selected_group_menu = array();
        foreach ($node->storage['groupmenu'] as $group_menus) {
          if ($group_menus['gid'] == $node->group) {
            $selected_group_menu = $group_menus;
            break;
          }
        }
        foreach (array_keys($options) as $option) {
          list($menu_id, ) = explode(':', $option);
          if ($menu_id == $selected_group_menu['menu_name']) {
            $parent = empty($form['#node']->menu['plid']) ? $option : $menu_id . ':' . $form['#node']->menu['plid'];
            $form['menu']['link']['parent']['#default_value'] = $parent;
            break;
          }
        }
      }
      $form['menu']['#access'] = !empty($options);
      $form['menu']['#attached']['js'][] = drupal_get_path('module', 'groupmenu') . '/groupmenu.js';
      $form['menu']['#attached']['js'][] = array(
        'data' => array(
          'groupMenu' => $settings,
        ),
        'type' => 'setting',
      );
      $form['menu']['#settings'] = $merged_list;
      $form['menu']['link']['parent']['#options'] = $options;
      if (!user_access('administer menu')) {
        $form['#validate'][] = 'groupmenu_node_form_validate';
      }
    }
  }
}