You are here

public function OgVocab::getFormElement in OG Vocabulary 7

Return form element.

File

includes/og_vocab.og_vocab.inc, line 41
A class used for messages.

Class

OgVocab
@file A class used for messages.

Code

public function getFormElement($entity_type, $entity, &$form, &$form_state) {
  $vid = $this->vid;
  $vocabulary = taxonomy_vocabulary_load($vid);
  $field_name = $this->field_name;
  $field = field_info_field($field_name);

  // Create a dummy entity that only holds the legal values for the
  // selected vocabulary. This is needed for autocomplete fields.
  $dummy_entity = clone $entity;
  $wrapper = entity_metadata_wrapper($entity_type, $dummy_entity);
  $rebuild_keys = FALSE;
  if ($field['cardinality'] != 1) {
    foreach ($wrapper->{$field_name}
      ->value() as $delta => $term) {
      if (empty($term)) {

        // The term has deleted, but the reference still remains in the field.
        continue;
      }
      if ($term->vid != $this->vid) {
        $wrapper->{$field_name}
          ->offsetUnset($delta);
        $rebuild_keys = TRUE;
      }
    }
  }
  if ($rebuild_keys) {
    $ids = $wrapper->{$field_name}
      ->value(array(
      'identifier' => TRUE,
    ));

    // Rebuild the deltas.
    $wrapper->{$field_name}
      ->set(array_values($ids));
  }
  ctools_include('fields');
  $mocked_field = $this
    ->getMockedField();
  $field_instance = $mocked_field['instance'];
  $field_instance['field'] = $mocked_field['field'];
  $dummy_form_state = $form_state;
  if (empty($form_state['rebuild'])) {

    // Form is "fresh" (i.e. not call from field_add_more_submit()), so
    // re-set the items-count, to show the correct amount for the mocked
    // instance.
    $dummy_form_state['field'][$field_name][LANGUAGE_NONE]['items_count'] = !empty($dummy_entity->{$field_name}[LANGUAGE_NONE]) ? count($dummy_entity->{$field_name}[LANGUAGE_NONE]) : 0;
  }
  $new_element = ctools_field_invoke_field($field_instance, 'form', $entity_type, $dummy_entity, $form, $dummy_form_state, array(
    'default' => TRUE,
  ));
  $element = $new_element[$field_name][LANGUAGE_NONE];
  if ($this->settings['widget_type'] == 'entityreference_autocomplete') {
    $element['#vid'] = $vid;
    if (!empty($element['add_more'])) {

      // Change the "Add more" button name so it adds only the needed
      // element.
      $element['add_more']['#name'] .= '__' . $vid;
    }
    foreach (array_keys($element) as $delta) {
      if (!is_numeric($delta)) {
        continue;
      }
      $sub_element =& $element[$delta]['target_id'];
      $this
        ->prepareAutoComplete($sub_element);
    }
  }
  elseif ($this->settings['widget_type'] == 'entityreference_autocomplete_tags') {
    $element['#vid'] = $vid;
    $this
      ->prepareAutoComplete($element);

    // Add own validate handler, that will be able to add "autocreate"
    // values.
    $element['#element_validate'] = array(
      '_og_vocab_autocomplete_tags_validate',
    );
  }
  $element['#element_validate'][] = 'og_vocab_sub_widget_cardinality_validate';
  $element['#og_vocab'] = $this;
  form_load_include($form_state, 'inc', 'og_vocab', '/includes/og_vocab.og_vocab');
  $form['#after_build']['og_vocab'] = 'og_vocab_element_after_build';
  $form['#validate']['og_vocab'] = 'og_vocab_element_validate';
  return $element;
}