public function sio__vocabulary_widget::form

3.x sio__vocabulary_widget.inc public sio__vocabulary_widget::form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element)

Overrides TripalFieldWidget::form

See also

TripalFieldWidget::form()

File

tripal_chado/includes/TripalFields/sio__vocabulary/sio__vocabulary_widget.inc, line 15

Class

sio__vocabulary_widget

Code

public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {

  parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);

  $settings = $this->field['settings'];
  $field_name = $this->field['field_name'];
  $field_type = $this->field['type'];
  $field_table = $this->instance['settings']['chado_table'];
  $field_column = $this->instance['settings']['chado_column'];

  // If the items array is empty then we are creating a new entity.
  // Since this is set when the entity type is created, we don't want to allow
  // content managers to change it. Thus we need to look up the value for the
  // entity type and use it here.
  if (empty($items)) {
    // Use the bundle to get the cv_id choosen for this cvterm-based entity.
    // ASSUMPTION: the cv_id is saved as the "type_value" of the bundle.
    $bundle = tripal_load_bundle_entity(array('name' => $widget['#bundle']));
    $cv = chado_get_cv(array('cv_id' => $bundle->type_value));

    // Now populate the items array with defaults based on the cv.
    if ($cv) {
      $items[$delta] = array(
        'value' => $cv->name,
        'chado-' . $field_table . '__cv_id' => $cv->cv_id,
      );
    }
    else {
      tripal_report_error(
      $field_name, 
      TRIPAL_ERROR, 
      'Unable to determine default vocabulary for :name Tripal Content Type', 
      array(':name' => $bundle->label)
      );
      drupal_set_message(t('Unable to determine default vocabulary for :name Tripal Content Type', 
      array(':name' => $bundle->label)), 'error');
    }
  }

  $widget['value'] = array(
    '#type' => 'value',
    '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  );
  $widget['chado-' . $field_table . '__cv_id'] = array(
    '#type' => 'value',
    '#value' => array_key_exists($delta, $items) ? $items[$delta]['chado-' . $field_table . '__cv_id'] : '',
  );
  $widget['vocabulary_name'] = array(
    '#type' => 'item',
    '#title' => 'Vocabulary',
    '#markup' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  );
}