You are here

function biblio_contributor_widget in Bibliography Module 7

Same name and namespace in other branches
  1. 7.2 biblio.module \biblio_contributor_widget()
1 call to biblio_contributor_widget()
biblio_form in ./biblio.module
Implements hook_form().

File

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

Code

function biblio_contributor_widget($node, &$form_state) {
  $init_count = variable_get('biblio_init_auth_count', 4);
  $contributor_count = 0;
  if (isset($form_state['values']['biblio_contributors'])) {
    $contributors = $form_state['values']['biblio_contributors'];
  }
  elseif (isset($node->biblio_contributors)) {
    $contributors = $node->biblio_contributors;
  }
  else {
    $contributors = array();
  }
  $ctypes = db_query('SELECT * FROM {biblio_contributor_type_data}');
  foreach ($ctypes as $ctype) {
    $options['roles'][$ctype->auth_type] = $ctype->title;
  }
  $options['categories'] = array(
    1 => t('Primary'),
    2 => t('Secondary'),
    3 => t('Tertiary'),
    4 => t('Subsidiary'),
    5 => t('Corporate/Institutional'),
  );

  // Container for just the contributors.
  $wrapper = array();
  $wrapper['biblio_contributors'] = array(
    '#tree' => TRUE,
    '#theme' => 'biblio_contributors',
    '#prefix' => '<div id="biblio-contributors-wrapper">',
    '#suffix' => '</div>',
  );
  foreach ($contributors as $author) {
    $wrapper['biblio_contributors'][] = biblio_contributor_form($author, $options);
    $contributor_count++;
  }
  if (isset($form_state['biblio_contrib_count'])) {
    $form_count = max(max($init_count, $contributor_count), $form_state['biblio_contrib_count']);
  }
  else {
    $form_count = max($init_count, $contributor_count);
    $form_state['biblio_contrib_count'] = $form_count;
  }
  if ($form_count > $contributor_count) {
    $author = array();
    for ($i = 0; $i < $form_count - $contributor_count; $i++) {
      $wrapper['biblio_contributors'][] = biblio_contributor_form($author, $options);
    }
  }
  $wrapper['add_more'] = array(
    '#type' => 'submit',
    '#value' => t('More contributors'),
    '#description' => t("If there aren't enough boxes above, click here to add more."),
    '#weight' => 1,
    // If no javascript action.
    '#submit' => array(
      'biblio_contributors_add_more',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'biblio_contributors_add_more_callback',
      'wrapper' => 'biblio-contributors-wrapper',
    ),
  );
  return $wrapper;
}