You are here

function biblio_form in Bibliography Module 7.2

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 biblio.module \biblio_form()

Displays the Add/Edit form for a biblio entity

@global object $user

_state

Parameters

array $form:

object $biblio:

Return value

array

2 string references to 'biblio_form'
biblio_add in ./biblio.module
hook_menu callback function for admin/structure/biblio/add
biblio_page_edit in ./biblio.module
Presents the biblio editing form, or redirects to delete confirmation.

File

./biblio.module, line 1138

Code

function biblio_form($form, &$form_state, $biblio = NULL) {
  global $user;
  $fields = array();
  $form['#id'] = 'biblio-form';

  // Check to see if we've imported a biblio object using a sub-module
  if (isset($form_state['biblio_imported'])) {
    $biblio = $form_state['biblio_imported'];
  }
  $form['biblio_tabs'] = $tabs = array();
  $publication_type = !empty($form_state['biblio_type']) ? $form_state['biblio_type'] : (isset($biblio->publication_type) ? $biblio->publication_type : '');
  if (isset($_COOKIE['has_js']) && !$_COOKIE['has_js']) {
    unset($form['biblio_next']['#attributes']);
  }
  $step_two = FALSE;
  if (!empty($publication_type) && $publication_type != 'select_type' || isset($biblio)) {
    $step_two = TRUE;
  }

  /* publication type */
  $param['options'] = array(
    "enctype" => "multipart/form-data",
  );
  foreach (biblio_types() as $machine_name => $info) {
    $type_options[$machine_name] = $info->name;
  }
  asort($type_options);
  $options['select_type'] = t('Select Type...');
  $options += $type_options;
  if (!$step_two) {
    $form['biblio_type'] = array(
      '#type' => 'select',
      '#title' => t('Publication Type'),
      '#default_value' => $publication_type,
      '#options' => $options,
      '#description' => NULL,
      '#weight' => -15,
      '#attributes' => array(
        'onchange' => 'document.getElementById(\'edit-biblio-next\').click();
        document.getElementById(\'edit-biblio-next\').disabled="true"',
      ),
      '#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(),
    );
  }
  else {

    // Step two
    module_load_include('inc', 'biblio', 'includes/biblio.fields');

    // Add field instances only if they dont already exist.
    if (biblio_field_instances_missing($publication_type)) {
      biblio_add_field_instances('biblio', $publication_type);
    }

    // Create a new, empty biblio object
    if (!isset($biblio)) {
      $biblio = biblio_create($publication_type);
    }
    $wrapper = biblio_wrapper($biblio);

    // Set the title. PASS_TRHOUGH allows us to include HTML in the string.
    drupal_set_title(t('Create @name', array(
      '@name' => $options[$publication_type],
    )), PASS_THROUGH);
    $instance_info = field_info_instances('biblio', $biblio->publication_type);

    // $fielded_form becomes a placeholder for form values that are automatically
    // returned to us from the Field Attach API
    $fielded_form = $form;
    field_attach_form('biblio', $biblio, $fielded_form, $form_state);
    $tabs = array(
      '#type' => 'vertical_tabs',
      '#weight' => 3,
    );
    $tabs += biblio_form_vtabs();
    $tabs['contributors'] += biblio_contributor_widget($biblio, $form_state);

    // Add imported keywords as the default value for the biblio_keywords field
    if (isset($form_state['biblio_imported']) && isset($fielded_form['biblio_keywords'])) {
      $langcode = $fielded_form['biblio_keywords']['#language'];
      $fielded_form['biblio_keywords'][$langcode]['#default_value'] = implode(', ', $biblio->biblio_keywords);
    }

    // Add the values we created above to the real form
    foreach ($fielded_form as $field => $data) {

      // Set a vtab value, if it exists (not all array keys are valid fields)
      $vtab = isset($instance_info[$field]['settings']['vtab']) ? $instance_info[$field]['settings']['vtab'] : FALSE;

      // We don't want extra theme elements from $fielded_form.
      // Just field instances with a set vtab.
      if ($vtab !== FALSE) {
        if ($vtab == 'none' || $vtab == 'None') {

          // Fields specified to be outside of the vtabs
          $form[$field] = $data;
        }
        else {

          // Add all other fields to the vtabs
          $tabs[$vtab][$field] = $data;
        }
      }
      else {
        if (biblio_is_field_instance($field, 'biblio', $biblio->publication_type)) {
          $form[$field] = $data;
        }
      }
    }
    $form_state['biblio_fields'] = $fields;
    foreach (element_children($tabs) as $key) {
      $tab_children = element_children($tabs[$key]);
      if (empty($tab_children) && $key != 'biblio_full_text') {
        unset($tabs[$key]);
      }
    }

    // Add the buttons.
    // Save button is always available
    $form['buttons'] = array();
    $form['buttons']['#weight'] = 100;
    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#weight' => 5,
      '#submit' => array(
        'biblio_form_submit',
      ),
    );

    // Delete button is available if we're editing an existing biblio.
    if (!empty($biblio->bid)) {
      $form['buttons']['delete'] = array(
        '#access' => user_access('delete biblios'),
        '#type' => 'submit',
        '#value' => t('Delete'),
        '#weight' => 15,
        '#submit' => array(
          'biblio_form_delete_submit',
        ),
      );
    }
    $form_state['biblio'] = $biblio;
  }

  // $form['format'] = filter_form($node->format, 20);

  //$biblio_form['#tree']  = TRUE;
  $form['#validate'] = array(
    'biblio_form_validate',
  );
  $form['#cache'] = TRUE;
  $form['biblio_tabs'] += $tabs;
  biblio_hide_form_fields($form, $form_state);
  return $form;
}