class data__accession_widget

Hierarchy

Expanded class hierarchy of data__accession_widget

2 string references to 'data__accession_widget'
data__accession.inc in tripal_chado/includes/TripalFields/data__accession/data__accession.inc
tripal_chado_bundle_instances_info_custom in tripal_chado/includes/tripal_chado.fields.inc
Helper function for the hook_create_tripalfield_instance().

File

tripal_chado/includes/TripalFields/data__accession/data__accession_widget.inc, line 2

View source
class data__accession_widget extends ChadoFieldWidget {
  // The default lable for this field.
  public static $default_label = 'Site Accession';

  // The list of field types for which this formatter is appropriate.
  public static $field_types = array('data__accession');

  /**
   * @see TripalFieldWidget::form()
   */
  public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
    parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);

    $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'];

    // Get the field defaults.
    $dbxref_id = '';
    $db_id = '';
    $accession = '';

    // If the field already has a value then it will come through the $items
    // array.  This happens when editing an existing record.
    if (count($items) > 0 and array_key_exists($delta, $items)) {
      $dbxref_id = $items[$delta]['chado-' . $field_table . '__' . $field_column];
      $db_id = $items[$delta]['db_id'];
      $accession = $items[$delta]['accession'];
    }

    // Check $form_state['values'] to see if an AJAX call set the values.
    if (array_key_exists('values', $form_state) and 
      array_key_exists($field_name, $form_state['values'])) {
      $dbxref_id = isset($form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $field_column]) ? $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $field_column] : '';
      $db_id = isset($form_state['values'][$field_name]['und'][$delta]['db_id']) ? $form_state['values'][$field_name]['und'][$delta]['db_id'] : '';
      $accession = isset($form_state['values'][$field_name]['und'][$delta]['accession']) ? $form_state['values'][$field_name]['und'][$delta]['accession'] : '';
    }

    $schema = chado_get_schema('dbxref');
    $options = chado_get_db_select_options();

    //$widget['#element_validate'] = array('chado_base__dbxref_id_widget_validate');
    $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
    $widget['#suffix'] = "</span>";

    $widget['value'] = array(
      '#type' => 'value',
      '#value' => $dbxref_id,
    );

    $widget['chado-' . $field_table . '__' . $field_column] = array(
      '#type' => 'value',
      '#default_value' => $dbxref_id,
    );

    $widget['db_id'] = array(
      '#type' => 'select',
      '#title' => t('Database'),
      '#options' => $options,
      '#required' => $element['#required'],
      '#default_value' => $db_id,
      '#ajax' => array(
        'callback' => "data__accession_widget_form_ajax_callback",
        'wrapper' => "$field_name-dbxref--db-id",
        'effect' => 'fade',
        'method' => 'replace'
      ),
    );
    $widget['accession'] = array(
      '#type' => 'textfield',
      '#title' => t('Accession'),
      '#default_value' => $accession,
      '#required' => $element['#required'],
      '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
      '#size' => 15,
      '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
      '#disabled' => $db_id ? FALSE : TRUE,
    );
  }

  /**
   * @see TripalFieldWidget::submit()
   */
  public function validate($element, $form, &$form_state, $langcode, $delta) {
    $field_name = $this->field['field_name'];
    $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'];

    $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
    $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
    $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];

    // If user did not select a database, we want to remove dbxref_id from the
    // field. We use '__NULL__' because this field is part of the base table
    // and this tells the Chado backend to insert a null rather than an empty
    // string.
    if (!$db_id) {
      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = '__NULL__';
    }
    // If the dbxref_id does not match the db_id + accession then the user
    // has selected a new dbxref record and we need to update the hidden
    // value accordingly.
    if ($db_id and $accession) {
      $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
      if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
        $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
        $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
      }
    }
  }

  /**
   * @see TripalFieldWidget::submit()
   */
  public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
    $field_name = $this->field['field_name'];
    $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'];

    $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
    $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
    $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];

    // If the accession doesn't exist then add it.
    if ($db_id and $accession) {
      $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
      if (!$dbxref) {
        $values = array(
          'db_id' => $db_id,
          'accession' => $accession,
        );
        $dbxref = chado_insert_dbxref($values);
        $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
        $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
      }
    }
  }

  /**
   * @see TripalFieldWidget::theme()
   */
  public function theme($element) {
    $layout = "
      <div class=\"primary-dbxref-widget\">
        <div class=\"primary-dbxref-widget-item\">" .
      drupal_render($element['db_id']) . "
        </div>
        <div class=\"primary-dbxref-widget-item\">" .
      drupal_render($element['accession']) . "
        </div>
      </div>
    ";

    $fieldset = array(
      '#title' => $element['#title'],
      '#value' => '',
      '#description' => $element['#description'],
      '#children' => $layout,
    );

    return theme('fieldset', array('element' => $fieldset));
  }
}

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
data__accession_widget::$default_label public static property Overrides TripalFieldWidget::$default_label
data__accession_widget::$field_types public static property Overrides TripalFieldWidget::$field_types
data__accession_widget::form public function Overrides TripalFieldWidget::form
data__accession_widget::submit public function Overrides TripalFieldWidget::submit
data__accession_widget::theme public function Overrides TripalFieldWidget::theme
data__accession_widget::validate public function Overrides TripalFieldWidget::validate
TripalFieldWidget::info public static function Provides information about the widgets provided by this field.
TripalFieldWidget::__construct public function Instantiates a new TripalFieldWidget object.