You are here

function biblio_form in Bibliography Module 7

Same name and namespace in other branches
  1. 5 biblio.module \biblio_form()
  2. 6.2 biblio.module \biblio_form()
  3. 6 biblio.module \biblio_form()
  4. 7.2 biblio.module \biblio_form()

Implements hook_form().

Create the form for collecting the information specific to this node type. This hook requires us to return some HTML that will be later placed inside the form.

File

./biblio.module, line 1281
Bibliography Module for Drupal.

Code

function biblio_form($node, &$form_state) {
  global $user;
  $path = drupal_get_path('module', 'biblio');
  if (variable_get('biblio_button_hide', 1) == 1) {
    drupal_add_js($path . '/misc/biblio.nodeformbuttonhide.js', 'file');
  }
  $fields = array();
  $form['biblio_tabs'] = $tabs = array();
  $tid = !empty($form_state['biblio_type']) ? $form_state['biblio_type'] : (isset($node->biblio_type) ? $node->biblio_type : 0);
  $step_two = !empty($tid);

  /* publication type */
  $param['options'] = array(
    "enctype" => "multipart/form-data",
  );
  $result = db_query('SELECT t.* FROM {biblio_types} as t WHERE tid > -2 AND visible = 1');
  foreach ($result as $option) {
    $results[$option->tid] = $option->name;
  }
  asort($results);
  $options[0] = t('Select Type...');
  $options += $results;
  $form['biblio_type'] = array(
    '#type' => 'select',
    '#title' => t('Publication Type'),
    '#default_value' => $tid,
    '#options' => $options,
    '#description' => NULL,
    '#weight' => 2,
    '#attributes' => array(
      'onchange' => 'document.getElementById(\'edit-biblio-next\').click()',
    ),
    '#executes_submit_callback' => TRUE,
    '#limit_validation_errors' => array(),
    '#multiple' => FALSE,
    '#required' => TRUE,
  );
  $form['biblio_next'] = array(
    '#type' => 'submit',
    '#value' => $step_two ? t('Change Publication Type') : t('Next'),
    '#limit_validation_errors' => array(),
    '#weight' => -10,
    '#submit' => array(),
  );
  if (isset($_COOKIE['has_js']) && !$_COOKIE['has_js']) {
    unset($form['biblio_next']['#attributes']);
  }
  if ($step_two) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#required' => TRUE,
      '#default_value' => trim(isset($form_state['values']['title']) ? $form_state['values']['title'] : $node->title),
      '#maxlength' => 255,
      '#size' => 120,
      '#weight' => 1,
    );

    // Build the field array used to make the form.
    $result = db_query("SELECT * FROM {biblio_fields} b\n              INNER JOIN {biblio_field_type} bt ON b.fid = bt.fid\n              INNER JOIN {biblio_field_type_data} btd ON btd.ftdid=bt.ftdid\n              WHERE bt.tid=:tid ORDER BY bt.weight ASC", array(
      ':tid' => $tid,
    ), array(
      'fetch' => PDO::FETCH_ASSOC,
    ));
    foreach ($result as $row) {
      $fields[$row['name']] = $row;
    }
    _biblio_localize_fields($fields);
    $tabs = array(
      '#type' => 'vertical_tabs',
      '#weight' => 10,
    );
    $tabs += biblio_node_form_vtabs();
    $tabs['biblio_authors'] = array(
      '#type' => 'fieldset',
      '#group' => 'biblio_tabs',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => 'Authors',
      '#description' => t('Enter a single name per line using a format such as "Smith, John K" or "John K Smith" or "J.K. Smith"'),
    );
    $tabs['biblio_authors'] += biblio_contributor_widget($node, $form_state);
    $form_state['biblio_fields'] = $fields;
    foreach ($fields as $key => $fld) {
      $options = '';
      if ($key == 'biblio_keywords') {
        $sep = check_plain(variable_get('biblio_keyword_sep', ','));

        // Is the kewords are in array form, then implode them into a string.
        if (isset($form_state['values']['biblio_keywords']) && is_array($form_state['values']['biblio_keywords'])) {
          require_once drupal_get_path('module', 'biblio') . '/includes/biblio.keywords.inc';
          $form_state['values']['biblio_keywords'] = biblio_implode_keywords($form_state['values']['biblio_keywords']);
        }
        if (!empty($node->{$key}) && is_array($node->{$key})) {
          require_once drupal_get_path('module', 'biblio') . '/includes/biblio.keywords.inc';
          $node->{$key} = biblio_implode_keywords($node->{$key});
        }
        if (empty($fld['hint'])) {
          $fld['hint'] = t('Separate keywords using the " @sep " character', array(
            '@sep' => $sep,
          ));
        }
      }
      $element = array(
        '#default_value' => isset($form_state['values'][$key]) ? $form_state['values'][$key] : (isset($node->{$key}) ? $node->{$key} : ''),
        '#type' => $fld['type'],
        '#title' => check_plain($fld['title']),
        '#size' => $fld['size'],
        '#rows' => 10,
        '#maxlength' => $fld['maxsize'],
        '#weight' => $fld['weight'] / 10,
        '#autocomplete_path' => $fld['autocomplete'] ? 'biblio/autocomplete/' . $fld['name'] : '',
        '#description' => check_plain($fld['hint']),
        '#format' => isset($node->biblio_formats[$key]) ? $node->biblio_formats[$key] : filter_default_format(),
      );

      // The required marker must be added manually. If #required is set, the
      // validation error message will not indicate which tab the element is
      // under. Instead biblio_node_form_validate() sets a custom error message.
      if ($fld['required']) {
        $element['#title'] .= ' ' . theme('form_required_marker');
      }
      if ($key == 'biblio_refereed') {
        $element['#options'] = array(
          '' => t('None'),
          'Refereed' => t('Refereed'),
          'Non-Refereed' => t('Non-Refereed'),
          'Does Not Apply' => t('Does Not Apply'),
          'Unknown' => t('Unknown'),
        );
        $element['#description'] = t('If you are not sure, set this to Unknown or Does Not Apply');
      }
      if ($fld['common'] || $fld['visible']) {
        $tabs[$fld['vtab']][$key] = $element;
      }
    }
  }
  foreach (element_children($tabs) as $key) {
    $tab_children = element_children($tabs[$key]);
    if (empty($tab_children) && $key != 'biblio_full_text') {
      unset($tabs[$key]);
    }
  }

  // $form['format'] = filter_form($node->format, 20);
  // $biblio_form['#tree']  = TRUE;.
  $form['#validate'] = array(
    'biblio_node_form_validate',
  );
  $form['#cache'] = TRUE;
  $form['biblio_tabs'] += $tabs;
  return $form;
}