You are here

function _og_vocab_autocomplete_tags_validate in OG Vocabulary 7

See also

_entityreference_autocomplete_tags_validate()

1 string reference to '_og_vocab_autocomplete_tags_validate'
OgVocab::getFormElement in includes/og_vocab.og_vocab.inc
Return form element.

File

./og_vocab.module, line 474
Give each group its own system controlled vocabularies.

Code

function _og_vocab_autocomplete_tags_validate($element, &$form_state, $form) {
  $value = array();

  // If a value was entered into the autocomplete...
  if (!empty($element['#value'])) {
    $entities = drupal_explode_tags($element['#value']);
    $value = array();
    foreach ($entities as $entity) {

      // Indicate if a value was found, or we need to create a new one.
      $found = FALSE;
      if ($tid = _og_vocab_term_exists_in_vocab($entity, $element['#vid'])) {

        // The term name already exists in the vocabulary.
        $value[] = array(
          'target_id' => $tid,
        );
        $found = TRUE;
      }
      elseif (preg_match("/.+\\((\\d+)\\)/", $entity, $matches)) {

        // Take "label (entity id)', match the id from parenthesis.
        $value[] = array(
          'target_id' => $matches[1],
        );
        $found = TRUE;
      }
      else {

        // Try to get a match from the input string when the user didn't use the
        // autocomplete but filled in a value manually.
        $field = field_info_field($element['#field_name']);
        $handler = entityreference_get_selection_handler($field);

        // TODO: We can't use validateAutocompleteInput()
        // We might need to create our own Selection plugin.

        //if ($input = $handler->validateAutocompleteInput($entity, $element, $form_state, $form)) {

        //$value[] = array('target_id' => $input);

        //}
      }
      if (!$found) {
        $vocabulary = taxonomy_vocabulary_load($element['#vid']);
        $term = array(
          'target_id' => 'autocreate',
          'vid' => $vocabulary->vid,
          'name' => $entity,
          'vocabulary_machine_name' => $vocabulary->machine_name,
        );
        $value[] = (array) $term;
      }
    }
  }
  form_set_value($element, $value, $form_state);
}