You are here

function og_menu_form_node_form_alter in Organic Groups Menu (OG Menu) 7.3

Same name and namespace in other branches
  1. 7.2 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 351
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;

  // Group type.
  if (og_is_group_type('node', $type)) {
    if (!variable_get('og_menu_hide_create_option', FALSE)) {
      $form['og_menu'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable menu for this group'),
        '#default_value' => $form['#node']->og_menu,
        '#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';
    }
    else {
      $form['#node']->og_menu = TRUE;
    }
  }
  if (og_is_group_content_type('node', $type) && variable_get('og_menu_enable_' . $type, FALSE) && isset($form['#node']->storage['og_menu'])) {

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

      // Get the group audience fields so we can make the javascript aware of
      // them.
      $settings['group_audience_fields'] = array_keys(og_get_group_audience_fields('node', $type));
      $fields = array();
      foreach (og_get_group_audience_fields('node', $type) as $field_name => $label) {
        $field = field_info_field($field_name);
        $instance = field_info_instance('node', $field_name, $type);

        // Group the data we need in the client side js code.
        // Notice: 'default' is a reserved word in js, so we use 'normal' instead.
        // This is currently the only widget in OG.
        if ($instance['widget']['type'] == 'og_complex') {
          $default_selector = $field_name . '[' . $form[$field_name]['#language'] . '][0][default]';
          $admin_selector = $field_name . '[' . $form[$field_name]['#language'] . '][0][admin]';
          $fields[$field_name]['normal'] = $instance['settings']['behaviors']['og_widget']['default']['widget_type'];
          $fields[$field_name]['normal_selector'] = $default_selector;
          $fields[$field_name]['admin'] = $instance['settings']['behaviors']['og_widget']['admin']['widget_type'];
          $fields[$field_name]['admin_selector'] = $admin_selector;
        }
        else {
          $default_selector = $field_name . '[' . $form[$field_name]['#language'] . ']';
          $fields[$field_name]['normal'] = $instance['widget']['type'];
          $fields[$field_name]['normal_selector'] = $default_selector;
        }
        $fields[$field_name]['cardinality'] = $field['cardinality'];

        // Field is visible by default.
        $fields[$field_name]['visibility'] = TRUE;

        // When using entity reference pre-populate, the field may be hidden.
        $context = og_context();
        if (isset($context['group_type']) && isset($instance['settings']['behaviors']['prepopulate']['action']) && $instance['settings']['behaviors']['prepopulate']['action'] == 'hide' && $context['group_type'] == $field['settings']['target_type']) {
          $fields[$field_name]['visibility'] = $context['gid'];
        }
      }
      $settings['group_audience_fields'] = $fields;

      // Perhaps we want to exclude certain fields.
      drupal_alter('og_menu_audience_fields', $settings['group_audience_fields'], $type);

      // 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 OG
        // ones.
        $merged_list = array_merge($available_menus, $list);
      }
      else {
        $merged_list = $list;
      }
      $link = $form['#node']->menu;

      // 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 wont be created.
      if (!isset($form['menu'])) {
        if (empty($options)) {
          return;
        }
        _og_menu_add_menufieldset($form, $options);
      }
      $settings['parent_options'] = $options;
      if ($nid = $form['nid']['#value']) {
        $form['menu']['link']['parent']['#default_value'] = $link['menu_name'] . ':' . $link['plid'];
      }
      $form['menu']['#access'] = !empty($options);
      $form['menu']['#attached']['js'][] = drupal_get_path('module', 'og_menu') . '/og_menu.js';
      $form['menu']['#attached']['js'][] = array(
        'data' => array(
          'ogMenu' => $settings,
        ),
        'type' => 'setting',
      );
      $form['menu']['#settings'] = $merged_list;
      $form['menu']['link']['parent']['#options'] = $options;
      if (!user_access('administer menu')) {
        $form['#validate'][] = 'og_menu_node_form_validate';
      }
    }
  }
}