You are here

function _exif_custom_taxonomy_autocomplete_validate in EXIF Custom 7

1 call to _exif_custom_taxonomy_autocomplete_validate()
exif_custom_process_entity in ./exif_custom.module
Process a given entity to see if it has EXIF data to be saved.

File

./exif_custom.module, line 360
Primary hook implementations for EXIF Custom.

Code

function _exif_custom_taxonomy_autocomplete_validate(&$element, $vocabularies) {

  // Autocomplete widgets do not send their tids in the form, so we must detect
  // them here and process them independently.
  $items = array();
  if ($tags = $element['#value']) {

    // Translate term names into actual terms.
    if (is_array($tags)) {

      // If #value is an array, it was probably a bag in the XMP, thus no need
      // to run through drupal_explode_tags().
      $typed_terms = $tags;
    }
    else {
      $typed_terms = drupal_explode_tags($tags);
    }
    foreach ($typed_terms as $typed_term) {

      // See if the term exists in the chosen vocabulary and return the tid;
      // otherwise, create a new 'autocreate' term for insert/update.
      if ($possibilities = taxonomy_term_load_multiple(array(), array(
        'name' => trim($typed_term),
        'vid' => array_keys($vocabularies),
      ))) {
        $term = array_pop($possibilities);
      }
      else {
        $vocabulary = reset($vocabularies);
        $term = array(
          'tid' => 'autocreate',
          'vid' => $vocabulary->vid,
          'name' => $typed_term,
          'vocabulary_machine_name' => $vocabulary->machine_name,
        );
      }
      $items[] = (array) $term;
    }
  }
  foreach ($items as $delta => $item) {
    if ($item['tid'] == 'autocreate') {
      $term = (object) $item;
      unset($term->tid);
      taxonomy_term_save($term);
      $items[$delta]['tid'] = $term->tid;
    }
  }
  return $items;
}