You are here

function oa_files_form_alter in Open Atrium Files 7.2

Implements hook_form_alter().

File

./oa_files.module, line 864

Code

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

  // We are creating a new node from the Add button in the files/folder widget
  if (isset($_GET['menu_parent']) || isset($_GET['term_parent'])) {

    // Check if this matches the content type being created.
    if (preg_match('#(.+)_node_form#', $form_id, $matches)) {
      $type = isset($_GET['type']) ? str_replace('-', '_', $_GET['type']) : 'oa_wiki_page';
      if ($matches[1] == $type) {
        $node =& $form['#node'];
        $menu_parent = isset($_GET['menu_parent']) ? $_GET['menu_parent'] : NULL;

        // When creating document nodes from files widget, auto-fill menu parent or taxonomy from URL.
        // Check "Show node instead of file" option by default.
        if (isset($form['field_oa_wiki_page_no_redirect'][LANGUAGE_NONE])) {
          $form['field_oa_wiki_page_no_redirect'][LANGUAGE_NONE]['#default_value'] = 1;
        }
        if (isset($form[OA_SECTION_FIELD][LANGUAGE_NONE])) {

          // Figure out best Section to create document page.
          $section = module_exists('oa_sections') ? oa_sections_get_best_section($node->type, $form[OA_SECTION_FIELD][LANGUAGE_NONE]['#options']) : NULL;
          if (!empty($section)) {
            $form[OA_SECTION_FIELD][LANGUAGE_NONE]['#default_value'] = $section;

            // when creating first item, menu_parent is zero, so change it to actual section
            if (empty($menu_parent)) {
              $menu_parent = $section;
            }
          }

          // move Section selection to top of node form so user can confirm it
          $form[OA_SECTION_FIELD]['#weight'] = $form['body']['#weight'] - 0.5;

          // and move the field out of any field group
          if (isset($form['#group_children'])) {
            unset($form['#group_children'][OA_SECTION_FIELD]);
          }
        }
        if (isset($menu_parent) && empty($form['menu']['link']['parent']['#default_value'])) {
          $pid = $menu_parent;
          if (module_exists('og_menu_single') && ($parent = node_load($pid)) && node_access('view', $parent) && ($mlid = og_menu_single_get_link_mlid('node', $pid))) {
            if (!isset($form['menu'])) {

              // If we got called before menu item was added, force it to be added now.
              og_menu_single_form_node_form_alter($form, $form_state);
            }
            $form['menu']['enabled']['#default_value'] = 1;
            $form['menu']['link']['parent']['#default_value'] = OG_MENU_SINGLE_MENU_NAME . ':' . $mlid;
          }
        }
        if (!empty($_GET['term_parent']) && ($term = taxonomy_term_load($_GET['term_parent']))) {

          // if using og_vocab, grab the valid vocab ids.
          $fieldname = '';
          $propname = '';
          if (module_exists('og_vocab')) {
            $vids = og_vocab_get_accessible_vocabs('node', $node->type, OG_VOCAB_FIELD);
            if (in_array($term->vid, $vids)) {
              $fieldname = 'og_vocabulary';
              $propname = 'target_id';
            }
          }
          if (empty($fieldname)) {

            // did not find og vocab, so check for global taxonomy reference fields
            $vocab = taxonomy_vocabulary_load($term->vid);
            $field_info = field_info_fields();
            $fields = field_info_instances('node', $node->type);
            foreach ($fields as $key => $field) {
              $info = $field_info[$key];
              if ($info['module'] == 'taxonomy') {
                foreach ($info['settings']['allowed_values'] as $index => $item) {
                  if ($item['vocabulary'] == $vocab->machine_name) {
                    $fieldname = $info['field_name'];
                    $propname = 'tid';
                    break;
                  }
                }
              }
            }
          }
          if (!empty($fieldname) && isset($form[$fieldname][LANGUAGE_NONE][0][$term->vid])) {
            if (is_array($form[$fieldname][LANGUAGE_NONE][0][$term->vid]['#default_value'])) {

              // Set default term when field is an array of ids.
              if (!in_array($term->tid, $form[$fieldname][LANGUAGE_NONE][0][$term->vid]['#default_value'])) {
                $form[$fieldname][LANGUAGE_NONE][0][$term->vid]['#default_value'][] = $term->tid;
              }
            }
            else {

              // Set default term when field is a string.
              if (strpos($form[$fieldname][LANGUAGE_NONE][0][$term->vid]['#default_value'], '(' . $term->tid . ')') === FALSE) {
                $form[$fieldname][LANGUAGE_NONE][0][$term->vid]['#default_value'] = $term->name . ' (' . $term->tid . ')';
                $form[$fieldname][LANGUAGE_NONE][0][$term->vid]['#init'][$term->tid] = $form[$fieldname][LANGUAGE_NONE][0][$term->vid]['#default_value'];
              }
            }
          }
        }
      }
    }
  }
}