You are here

function biblio_create_contributor_refs in Bibliography Module 7.2

Creates contributor reference fields based on user-entered contributor names and categories. Also creates Contributor entities for those contributors that don't already exist.

Parameters

array &$form_state:

1 call to biblio_create_contributor_refs()
biblio_form_submit in ./biblio.module
Process data that was submitted from the biblio add form. This handles the additions of Biblio and Contributor entities.

File

./biblio.module, line 3022

Code

function biblio_create_contributor_refs(&$form_state) {
  module_load_include('inc', 'biblio', 'includes/biblio.contributors');
  $categories = biblio_contributor_categories();

  // Reset the existing values, so we can unlink a ref by deleting a name in
  // the form.
  // @todo use the field language, not 'und'
  foreach ($categories as $info) {
    $form_state['values'][$info['field']]['und'] = array();
  }
  $delta = 0;
  foreach ($form_state['values']['biblio_contributors'] as $contrib) {
    if ($contrib['rank'] == '') {
      $contrib['rank'] = $delta;
    }
    $delta++;

    // Don't process fields left blank
    if (empty($contrib['name']) || empty($contrib['category'])) {
      continue;
    }
    $contributor = biblio_get_contributor_by_name($contrib['name']);
    if (!$contributor) {

      // Name not found in db. Contributor entity needs to be created
      $contributor = biblio_contributor_create($contrib['name']);
      $wrapper = biblio_wrapper($contributor, 'biblio_contributor');
      $wrapper->biblio_contributor_name
        ->set($contrib['name']);

      // Save the contributor entity so we can get a CID
      biblio_contributor_save($contributor);
    }
    $contrib['cid'] = $contributor->cid;

    // Get the field associated with the contributor's category
    $field = $categories[$contrib['category']]['field'];

    // Whether or not the CID already exists for this biblio in the same
    // contributor category (handles duplicate contributors in the same biblio)
    $id_exists = FALSE;
    foreach ($form_state['values'][$field]['und'] as $info) {
      if ($info['target_id'] == $contributor->cid) {
        $id_exists = TRUE;
      }
    }

    // Add the target CID and rank to the form values array, as if a user
    // had submitted these values
    if (!$id_exists) {
      $form_state['values'][$field]['und'][$contrib['rank']] = array(
        'target_id' => (string) $contrib['cid'],
        '_weight' => $contrib['rank'],
      );
    }
  }
}