Browse Source

Adding new subclasses for fields/widgets/formatters

Stephen Ficklin 8 years ago
parent
commit
6957932829
33 changed files with 518 additions and 249 deletions
  1. 1 1
      tripal/api/tripal.entities.api.inc
  2. 2 2
      tripal/api/tripal.fields.api.inc
  3. 62 1
      tripal/includes/TripalField.inc
  4. 0 62
      tripal/includes/TripalFieldInstance.inc
  5. 38 34
      tripal/includes/tripal.fields.inc
  6. 0 0
      tripal_chado/includes/TripalFields.old/chado_base__dbxref_id.inc
  7. 0 0
      tripal_chado/includes/TripalFields.old/chado_base__organism_id.inc
  8. 0 0
      tripal_chado/includes/TripalFields.old/chado_feature__md5checksum.inc
  9. 0 0
      tripal_chado/includes/TripalFields.old/chado_feature__residues.inc
  10. 0 0
      tripal_chado/includes/TripalFields.old/chado_feature__seqlen.inc
  11. 0 0
      tripal_chado/includes/TripalFields.old/chado_gene__transcripts.inc
  12. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__contact.inc
  13. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__cvterm.inc
  14. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__cvterm_adder.inc
  15. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__dbxref.inc
  16. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__expression.inc
  17. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__featureloc.inc
  18. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__featurepos.inc
  19. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__genotype.inc
  20. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__phenotype.inc
  21. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__prop.inc
  22. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__prop_adder.inc
  23. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__pub.inc
  24. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__relationship.inc
  25. 0 0
      tripal_chado/includes/TripalFields.old/chado_linker__synonym.inc
  26. 0 0
      tripal_chado/includes/TripalFields.old/chado_organism__type_id.inc
  27. 194 0
      tripal_chado/includes/TripalFields/obi__organism.inc
  28. 33 0
      tripal_chado/includes/TripalFields/obi__organism_formatter.inc
  29. 46 0
      tripal_chado/includes/TripalFields/obi__organism_widget.inc
  30. 0 14
      tripal_chado/includes/tripal_chado.entity.inc
  31. 3 3
      tripal_chado/includes/tripal_chado.field_storage.inc
  32. 103 96
      tripal_chado/includes/tripal_chado.fields.inc
  33. 36 36
      tripal_chado/tripal_chado.module

+ 1 - 1
tripal/api/tripal.entities.api.inc

@@ -247,7 +247,7 @@ function tripal_create_bundle($vocabulary, $accession, $term_name, &$error = '')
   catch (Exception $e) {
     $transaction->rollback();
     $error = _drupal_decode_exception($e);
-    drupal_set_message(t("Failed to create content type': %message",
+    drupal_set_message(t("Failed to create content type: %message.",
         array('%message' => $error['!message'])), 'error');
     return FALSE;
   }

+ 2 - 2
tripal/api/tripal.fields.api.inc

@@ -85,7 +85,7 @@ function tripal_get_field_widgets() {
     foreach ($field_files as $file) {
       $field_type = $file->name;
       module_load_include('inc', $module, 'includes/TripalFields/' . $field_type);
-      if (class_exists($field_type) and get_parent_class($field_type) == 'TripalFieldWidget') {
+      if (class_exists($field_type) and is_subclass_of($field_type) == 'TripalFieldWidget') {
         $widgets[] = $field_type;
       }
     }
@@ -115,7 +115,7 @@ function tripal_get_field_formatters() {
     foreach ($field_files as $file) {
       $field_type = $file->name;
       module_load_include('inc', $module, 'includes/TripalFields/' . $field_type);
-      if (class_exists($field_type) and get_parent_class($field_type) == 'TripalFieldFormatter') {
+      if (class_exists($field_type) and is_subclass_of($field_type) == 'TripalFieldFormatter') {
         $formatters[] = $field_type;
       }
     }

+ 62 - 1
tripal/includes/TripalField.inc

@@ -139,7 +139,9 @@ class TripalField {
   public function getInstance() {
     return $this->instance;
   }
-
+  // --------------------------------------------------------------------------
+  //                            OVERRIDEABLE FUNCTIONS
+  // --------------------------------------------------------------------------
 
   /**
    *  Perform validation of the field regardless how it is updated.
@@ -265,4 +267,63 @@ class TripalField {
 
   }
 
+  /**
+   * Provides a form for the 'Field Settings' of an instance of this field.
+   *
+   * This function corresponds to the hook_field_instance_settings_form()
+   * function of the Drupal Field API.
+   *
+   * Validation of the instance settings form is not supported by Drupal, but
+   * the TripalField class does provide a mechanism for supporting validation.
+   * To allow for validation of your setting form you must call the parent
+   * in your child class:
+   *
+   * @code
+   *   $element = parent::instanceSettingsForm();
+   * @endcode
+   *
+   * Please note, the form generated with this function does not easily
+   * support AJAX calls in the same way that other Drupal forms do.  If you
+   * need to use AJAX you must manually alter the $form in your ajax call.
+   * The typical way to handle updating the form via an AJAX call is to make
+   * the changes in the form function itself but that doesn't work here.
+   */
+  public function instanceSettingsForm() {
+    $settings = $this->instance['settings'];
+    $element = array();
+
+    //     $element['semantic_web'] = array(
+    //       '#type' => 'textfield',
+    //       '#title' => 'Semantic Web',
+    //       '#description' => t('Each field must be associated with a term
+    //           from a controlled vocabulary.  This allows computer programs to understand
+    //           the data provided on this site.  Please be cautions changing these
+    //           values.  Defaults are set by Tripal and sites that use the same
+    //           terms can exchange information.'),
+    //       '#collapsed' => TRUE,
+    //       '#collapsible' => TRUE,
+    //       '#tree' => TRUE,
+    //     );
+    $element['#field'] = $this->field;
+    $element['#instance'] = $this->instance;
+    $element['#element_validate'][] = 'tripal_field_instance_settings_form_validate';
+
+    return $element;
+  }
+  /**
+  * Provides validation of the instance settings form.
+  *
+  * There is no equivalent function in the Drupal Field API. Validation
+  * of instance settings forms in Drupal is not supported. However, the
+  * TripalField provides this function to fill the gap.  See the
+  * documentation for the instanceSettingsForm() function for instructions
+  * to support use of this function.
+  *
+  * @param $form
+  * @param $form_state
+  */
+  public function instanceSettingsFormValidate($form, &$form_state) {
+
+  }
+
 }

+ 0 - 62
tripal/includes/TripalFieldInstance.inc

@@ -1,62 +0,0 @@
-<?php
-
-class TripalFieldInstance {
-  /**
-   * Provides a form for the 'Field Settings' of an instance of this field.
-   *
-   * This function corresponds to the hook_field_instance_settings_form()
-   * function of the Drupal Field API.
-   *
-   * Validation of the instance settings form is not supported by Drupal, but
-   * the TripalField class does provide a mechanism for supporting validation.
-   * To allow for validation of your setting form you must call the parent
-   * in your child class:
-   *
-   * @code
-   *   $element = parent::instanceSettingsForm();
-   * @endcode
-   *
-   * Please note, the form generated with this function does not easily
-   * support AJAX calls in the same way that other Drupal forms do.  If you
-   * need to use AJAX you must manually alter the $form in your ajax call.
-   * The typical way to handle updating the form via an AJAX call is to make
-   * the changes in the form function itself but that doesn't work here.
-   */
-  public function instanceSettingsForm() {
-    $settings = $this->instance['settings'];
-    $element = array();
-
-    //     $element['semantic_web'] = array(
-    //       '#type' => 'textfield',
-    //       '#title' => 'Semantic Web',
-    //       '#description' => t('Each field must be associated with a term
-    //           from a controlled vocabulary.  This allows computer programs to understand
-    //           the data provided on this site.  Please be cautions changing these
-    //           values.  Defaults are set by Tripal and sites that use the same
-    //           terms can exchange information.'),
-    //       '#collapsed' => TRUE,
-    //       '#collapsible' => TRUE,
-    //       '#tree' => TRUE,
-    //     );
-    $element['#field'] = $this->field;
-    $element['#instance'] = $this->instance;
-    $element['#element_validate'][] = 'tripal_field_instance_settings_form_validate';
-
-    return $element;
-  }
-  /**
-  * Provides validation of the instance settings form.
-  *
-  * There is no equivalent function in the Drupal Field API. Validation
-  * of instance settings forms in Drupal is not supported. However, the
-  * TripalField provides this function to fill the gap.  See the
-  * documentation for the instanceSettingsForm() function for instructions
-  * to support use of this function.
-  *
-  * @param $form
-  * @param $form_state
-  */
-  public function instanceSettingsFormValidate($form, &$form_state) {
-
-  }
-}

+ 38 - 34
tripal/includes/tripal.fields.inc

@@ -82,47 +82,51 @@ function tripal_field_formatter_info_alter(&$info) {
  */
 function tripal_bundle_create($bundle) {
   $field_type = 'rdfs__type';
-
-  // Add a field to this bundle for the content type.
   $field_name = $bundle->name . '_' . $field_type;
-  $field = field_create_field(array(
-    'field_name' => $field_name,
-    'type' => $field_type,
-    'cardinality' => 1,
-    'locked' => FALSE,
-    'storage' => array(
-      'type' => 'tripal_no_storage'
-    ),
-    'settings' => array(
-    ),
-  ));
+
+  // Add the field, unless it already exists.
+  if (!field_info_field($field_name)) {
+    $field = field_create_field(array(
+      'field_name' => $field_name,
+      'type' => $field_type,
+      'cardinality' => 1,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'tripal_no_storage'
+      ),
+      'settings' => array(
+      ),
+    ));
+  }
 
 
   // Add an instance of the field to the bundle.
-  $instance = field_create_instance(array(
-    'field_name' => $field_name,
-    'entity_type' => 'TripalEntity',
-    'bundle' => $bundle->name,
-    'label' => 'Resource Type',
-    'description' => 'The resource type',
-    'required' => FALSE,
-    'settings' => array(
-      'auto_attach' => TRUE,
-    ),
-    'widget' => array(
-      'type' => 'rdfs__type_widget',
+  if (!field_info_instance($bundle->type, $field_name, $bundle->name)) {
+    $instance = field_create_instance(array(
+      'field_name' => $field_name,
+      'entity_type' => 'TripalEntity',
+      'bundle' => $bundle->name,
+      'label' => 'Resource Type',
+      'description' => 'The resource type',
+      'required' => FALSE,
       'settings' => array(
-        'display_label' => 1,
+        'auto_attach' => TRUE,
       ),
-    ),
-    'display' => array(
-      'default' => array(
-        'label' => 'inline',
-        'type' => 'rdfs__type_formatter',
-        'settings' => array(),
+      'widget' => array(
+        'type' => 'rdfs__type_widget',
+        'settings' => array(
+          'display_label' => 1,
+        ),
       ),
-    ),
-  ));
+      'display' => array(
+        'default' => array(
+          'label' => 'inline',
+          'type' => 'rdfs__type_formatter',
+          'settings' => array(),
+        ),
+      ),
+    ));
+  }
 }
 
 /**

+ 0 - 0
tripal_chado/includes/TripalFields/chado_base__dbxref_id.inc → tripal_chado/includes/TripalFields.old/chado_base__dbxref_id.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_base__organism_id.inc → tripal_chado/includes/TripalFields.old/chado_base__organism_id.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_feature__md5checksum.inc → tripal_chado/includes/TripalFields.old/chado_feature__md5checksum.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_feature__residues.inc → tripal_chado/includes/TripalFields.old/chado_feature__residues.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_feature__seqlen.inc → tripal_chado/includes/TripalFields.old/chado_feature__seqlen.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_gene__transcripts.inc → tripal_chado/includes/TripalFields.old/chado_gene__transcripts.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__contact.inc → tripal_chado/includes/TripalFields.old/chado_linker__contact.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__cvterm.inc → tripal_chado/includes/TripalFields.old/chado_linker__cvterm.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__cvterm_adder.inc → tripal_chado/includes/TripalFields.old/chado_linker__cvterm_adder.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__dbxref.inc → tripal_chado/includes/TripalFields.old/chado_linker__dbxref.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__expression.inc → tripal_chado/includes/TripalFields.old/chado_linker__expression.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__featureloc.inc → tripal_chado/includes/TripalFields.old/chado_linker__featureloc.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__featurepos.inc → tripal_chado/includes/TripalFields.old/chado_linker__featurepos.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__genotype.inc → tripal_chado/includes/TripalFields.old/chado_linker__genotype.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__phenotype.inc → tripal_chado/includes/TripalFields.old/chado_linker__phenotype.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__prop.inc → tripal_chado/includes/TripalFields.old/chado_linker__prop.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__prop_adder.inc → tripal_chado/includes/TripalFields.old/chado_linker__prop_adder.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__pub.inc → tripal_chado/includes/TripalFields.old/chado_linker__pub.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__relationship.inc → tripal_chado/includes/TripalFields.old/chado_linker__relationship.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_linker__synonym.inc → tripal_chado/includes/TripalFields.old/chado_linker__synonym.inc


+ 0 - 0
tripal_chado/includes/TripalFields/chado_organism__type_id.inc → tripal_chado/includes/TripalFields.old/chado_organism__type_id.inc


+ 194 - 0
tripal_chado/includes/TripalFields/obi__organism.inc

@@ -0,0 +1,194 @@
+<?php
+
+class obi__organism extends TripalField {
+
+  // The term that this field maps to.  The format for the term should be:
+  // [vocab]:[accession] where [vocab] is the short name of the vocabulary
+  // and [acession] is the unique accession number for the term.  This term
+  // must already exist in the vocabulary storage backend. This
+  // value should never be changed once fields exist for this type.
+  public static $term = 'OBI:organism';
+
+  // The default lable for this field.
+  public static $label = 'Organism';
+
+  // The default description for this field.
+  public static $description = 'The organism to which this resource is sssociated.';
+
+  // Add any default settings elements.  If you override the globalSettingsForm()
+  // or the instanceSettingsForm() functions then you need to be sure that
+  // any settings you want those functions to manage are listed in this
+  // array.
+  public static $settings = array(
+    'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
+    'chado_table' => '',
+    'chado_column' => '',
+    'base_table' => '',
+    'semantic_web' => '',
+  );
+
+  // Provide a list of instance specific settings. These can be access within
+  // the instanceSettingsForm.  When the instanceSettingsForm is submitted
+  // then Drupal with automatically change these settings for the instnace.
+  // It is recommended to put settings at the instance level whenever possible.
+  public static $instance_settings  = array();
+  // Set this to the name of the storage backend that by default will support
+  // this field.
+  public static $storage = 'field_chado_storage';
+
+  // The default widget for this field.
+  public static $default_widget = 'OBI__organism_widget';
+
+  // The default formatter for this field.
+  public static $default_formatter = 'OBI__organism_formatter';
+
+
+  /**
+   * @see TripalField::validate()
+   */
+  public function validate($entity_type, $entity, $field, $items, &$errors) {
+
+    $settings = $this->field['settings'];
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->field['settings']['chado_table'];
+    $field_column = $this->field['settings']['chado_column'];
+
+    // Get the field values.
+    foreach ($items as $delta => $values) {
+
+      // Get the field values.
+      $organism_id = $values['chado-' . $field_table . '__organism_id'];
+      if (!$organism_id or $organism_id == 0) {
+        $errors[$field_name]['und'][0][] = array(
+          'message' =>  t("Please specify an organism."),
+          'error' => 'chado_base__organism_id'
+        );
+      }
+    }
+  }
+
+  /**
+   * @see TripalField::load()
+   */
+  public function load($entity, $details = array()) {
+
+    $record = $details['record'];
+    $settings = $this->field['settings'];
+
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->field['settings']['chado_table'];
+    $field_column = $this->field['settings']['chado_column'];
+
+    // Get the terms for each of the keys for the 'values' property.
+    $label_term = 'rdfs:label';
+    $genus_term = tripal_get_chado_semweb_term('organism', 'genus');
+    $species_term = tripal_get_chado_semweb_term('organism', 'species');
+    $infraspecific_name_term = tripal_get_chado_semweb_term('organism', 'infraspecific_name');
+    $infraspecific_type_term = tripal_get_chado_semweb_term('organism', 'type_id');
+
+    // Set some defaults for the empty record.
+    $entity->{$field_name}['und'][0] = array(
+      'value' => array(
+        $label_term => '',
+        $genus_term => '',
+        $species_term => '',
+      ),
+    );
+
+    if ($record) {
+      $organism = $record->organism_id;
+      $string = $settings['field_display_string'];
+      $label = tripal_replace_chado_tokens($string, $organism);
+      $entity->{$field_name}['und'][0]['value'] = array(
+        $label_term => $label,
+        $genus_term => $organism->genus,
+        $species_term => $organism->species,
+      );
+      // The infraspecific fiels were introdcued in Chado v1.3.
+      if (property_exists($organism, 'infraspecific_name')) {
+        $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = NULL;
+        $entity->{$field_name}['und'][0]['value'][$infraspecific_name_term] = $organism->infraspecific_name;
+        if ($organism->type_id) {
+          $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] =  $organism->type_id->name;
+        }
+      }
+      $entity->{$field_name}['und'][0]['chado-' . $field_table . '__organism_id'] = $organism->organism_id;
+
+      // Is there a published entity for this organism?
+      if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
+        $fk_entity_id = $entity->chado_record->$field_column->entity_id;
+        $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $fk_entity_id;
+      }
+    }
+  }
+
+  /**
+   * @see TripalField::globalSettingsForm()
+   */
+  public function settingsForm($has_data) {
+    $element = parent::globalSettingsForm($has_data);
+
+    $settings = $this->field['settings'];
+
+    $element['instructions'] = array(
+      '#type' => 'item',
+      '#markup' => 'You may rewrite the way this field is presented to the end-user.
+        The Rewrite Value field allows you to use tokens to indicate how the
+        value should be displayed.  Tokens will be substituted with appriorate
+        data from the database.  See the Available tokens list for the
+        tokens you may use.'
+    );
+
+    $element['field_display_string'] = array(
+      '#type' => 'textfield',
+      '#title' => 'Rewrite Value',
+      '#description' => t('Provide a mixture of text and/or tokens for the format.
+          For example: [organism.genus] [organism.species].  When displayed,
+          the tokens will be replaced with the actual value.'),
+      '#default_value' => $settings['field_display_string'],
+    );
+
+    $element['tokens'] = array(
+      '#type' => 'fieldset',
+      '#collapsed' => TRUE,
+      '#collapsible' => TRUE,
+      '#title' => 'Available Tokens'
+    );
+    $headers = array('Token', 'Description');
+    $rows = array();
+
+    // Here we use the tripal_get_chado_tokens rather than the
+    // tripal_get_entity_tokens because we can't gurantee that all organisms
+    // have entities.
+    $tokens = tripal_get_chado_tokens('organism');
+    foreach ($tokens as $token) {
+      $rows[] = array(
+        $token['token'],
+        $token['description'],
+      );
+    }
+
+    $table_vars = array(
+      'header'     => $headers,
+      'rows'       => $rows,
+      'attributes' => array(),
+      'sticky'     => FALSE,
+      'caption'    => '',
+      'colgroups'  => array(),
+      'empty'      => 'There are no tokens',
+    );
+    $element['tokens']['list'] = array(
+      '#type' => 'item',
+      '#markup' => theme_table($table_vars),
+    );
+
+    // Add in the semantic web fields.
+    $parent_elements = parent::settings_form($field, $instance, $has_data);
+    $element = array_merge($element, $parent_elements);
+
+    return $element;
+  }
+
+}

+ 33 - 0
tripal_chado/includes/TripalFields/obi__organism_formatter.inc

@@ -0,0 +1,33 @@
+<?php
+
+class obi__organism_formatter extends TripalFieldFormatter {
+
+  // The default lable for this field.
+  public static $label = 'Tripal Field.';
+
+  // The list of field types for which this formatter is appropriate.
+  public static $field_types = array('obi__organism');
+
+  // The list of default settings for this formatter.
+  public static $settings = array();
+
+  /**
+   * @see TripalFieldFormatter::view()
+   */
+  public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
+    if (count($items) > 0) {
+      $content = $items[0]['value']['rdfs:label'];
+      if (array_key_exists('entity', $items[0]['value'])) {
+        list($entity_type, $entity_id) = explode(':', $items[0]['value']['entity']);
+        $content = l(strip_tags($items[0]['value']['rdfs:label']), 'bio_data/' . $entity_id);
+      }
+
+      // The cardinality of this field is 1 so we don't have to
+      // iterate through the items array, as there will never be more than 1.
+      $element[0] = array(
+        '#type' => 'markup',
+        '#markup' => $content,
+      );
+    }
+  }
+}

+ 46 - 0
tripal_chado/includes/TripalFields/obi__organism_widget.inc

@@ -0,0 +1,46 @@
+<?php
+
+class obi__organism_widget extends TripalFieldWidget {
+
+  // The default lable for this field.
+  public static $label = 'Organism';
+
+  // The list of field types for which this formatter is appropriate.
+  public static $field_types = array('obi__organism');
+
+
+  /**
+   * @see TripalFieldWidget::form()
+   */
+  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->field['settings']['chado_table'];
+    $field_column = $this->field['settings']['chado_column'];
+
+    $organism_id = 0;
+    if (count($items) > 0 and array_key_exists('chado-' . $field_table . '__organism_id', $items[0])) {
+      $organism_id = $items[0]['chado-' . $field_table . '__organism_id'];
+    }
+
+    $widget['value'] = array(
+      '#type' => 'value',
+      '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
+    );
+    $options = tripal_get_organism_select_options(FALSE);
+    $widget['chado-' . $field_table . '__organism_id'] = array(
+      '#type' => 'select',
+      '#title' => $element['#title'],
+      '#description' => $element['#description'],
+      '#options' => $options,
+      '#default_value' => $organism_id,
+      '#required' => $element['#required'],
+      '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
+      '#delta' => $delta,
+    );
+  }
+}

+ 0 - 14
tripal_chado/includes/tripal_chado.entity.inc

@@ -1,19 +1,5 @@
 <?php
 
-/**
- * Implements hook_chado_bundle_create().
- *
- * This is a Tripal hook. It allows any module to perform tasks after
- * a bundle has been created.
- *
- * @param $bundle
- *  The TripalBundle object.
- */
-
-function tripal_chado_bundle_create($bundle) {
-
-}
-
 
 /**
  * Implements hook_entity_create().

+ 3 - 3
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -303,8 +303,8 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
         // Allow the creating module to alter the value if desired.  The
         // module should do this if the field has any other form elements
         // that need populationg besides the value which was set above.
-        tripal_load_include_field_type($field_type);
-        if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
+        tripal_load_include_field_class($field_type);
+        if (class_exists($field_type) and is_subclass_of($field_type, 'TripalField')) {
           $tfield = new $field_type($field);
           $tfield->load($entity, array('record' => $record));
         }
@@ -317,7 +317,7 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
 
         // Set an empty value by default, and let the hook function update it.
         $entity->{$field_name}['und'][0]['value'] = '';
-        tripal_load_include_field_type($field_type);
+        tripal_load_include_field_class($field_type);
         if (class_exists($field_type) && method_exists($field_type, 'load')) {
           $tfield = new $field_type($field);
           $tfield->load($entity, array('record' => $record));

+ 103 - 96
tripal_chado/includes/tripal_chado.fields.inc

@@ -1,57 +1,34 @@
 <?php
 
-function tripal_chado_field_widget_info() {
-  return array(
-    'chado_base_dbxref_id' => array(
-      'label' => 'Site specific Accession',
-      'field types' => array('tripal_key_value'),
-    ),
-    'chado_base_organism_id' => array(
-      'label' => 'Organism',
-      'field types' => array('tripal_key_value')
-    ),
-    'chado_feature_md5checsum' => array(
-      'label' => 'Sequence Checksum',
-      'field types' => array('tripal_key_value')
-    ),
-    'chado_feature_residues' => array(
-      'label' => 'Sequence',
-      'field types' => array('tripal_key_value')
-    ),
-    'chado_feature_seqlen' => array(
-      'label' => 'Sequence Length',
-      'field types' => array('tripal_key_value')
-    ),
-    'chado_gene_transcripts' => array(
-      'label' => 'Transcripts',
-      'field types' => array('tripal_key_value')
-    ),
-    'chado_linker_contact' => array(
-      'label' => 'Contacts',
-      'field types' => array('tripal_key_value')
-    ),
-    'chado_linker_dbxref' => array(
-      'label' => 'Cross References',
-      'field types' => array('tripal_key_value')
-    ),
-    'chado_linker_expression' => array(
-      'label' => 'Expression',
-      'field types' => array('tripal_key_value')
-    ),
-    'chado_linker_featureloc' => array(
-      'label' => 'Positions',
-      'field types' => array('tripal_key_value')
-    ),
-  );
+/**
+ * Implements hook_chado_bundle_create().
+ *
+ * This is a Tripal hook. It allows any module to perform tasks after
+ * a bundle has been created.
+ *
+ * @param $bundle
+ *  The TripalBundle object.
+ */
+
+function tripal_chado_bundle_create($bundle) {
+  $entity_type = $bundle->type;
+
+  // Create/Add the new fields for this bundle.
+  tripal_chado_bundle_create_fields($entity_type, $bundle);
+
+  // Create/Add the new field instances for this bundle.
+  tripal_chado_bundle_create_instances($entity_type, $bundle);
 }
 
+
+
 /**
  * Implements hook_field_create_info().
  *
  * This is a Tripal defined hook that supports integration with the
  * TripalEntity field.
  */
-function tripal_chado_create_tripalfields2($entity_type, $bundle) {
+function tripal_chado_bundle_create_fields($entity_type, $bundle) {
 
   // Get the table this bundle is mapped to.
   $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
@@ -74,21 +51,36 @@ function tripal_chado_create_tripalfields2($entity_type, $bundle) {
   $info = array();
 
   // Create the fields for each column in the table.
-  tripal_chado_create_tripalfields_base($info, $details, $entity_type, $bundle);
+  tripal_chado_bundle_create_fields_base($info, $details, $entity_type, $bundle);
 
   // Create custom fields.
-  tripal_chado_create_tripalfields_custom($info, $details, $entity_type, $bundle);
-
+  tripal_chado_bundle_create_fields_custom($info, $details, $entity_type, $bundle);
+dpm($info);
   // Create fields for linking tables.
-  tripal_chado_create_tripalfields_linker($info, $details, $entity_type, $bundle);
+  //tripal_chado_bundle_create_fields_linker($info, $details, $entity_type, $bundle);
 
-  return $info;
+  foreach ($info as $field_name => $details) {
+    $field_type = $details['type'];
+
+    // If the field already exists then skip it.
+    $field = field_info_field($details['field_name']);
+    if ($field) {
+      continue;
+    }
+
+    // Create the field.
+    $field = field_create_field($details);
+    if (!$field) {
+      tripal_set_message(t("Could not create new field: %field.",
+          array('%field' =>  $details['field_name'])), TRIPAL_ERROR);
+    }
+  }
 }
 /**
  *
  * @param unknown $details
  */
-function tripal_chado_create_tripalfields_base(&$info, $details, $entity_type, $bundle) {
+function tripal_chado_bundle_create_fields_base(&$info, $details, $entity_type, $bundle) {
 
   $table_name = $details['chado_table'];
   $type_table = $details['chado_type_table'];
@@ -218,7 +210,7 @@ function tripal_chado_create_tripalfields_base(&$info, $details, $entity_type, $
  *
  * @param unknown $details
  */
-function tripal_chado_create_tripalfields_custom(&$info, $details, $entity_type, $bundle) {
+function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type, $bundle) {
   $table_name = $details['chado_table'];
   $type_table = $details['chado_type_table'];
   $type_field = $details['chado_type_column'];
@@ -226,10 +218,11 @@ function tripal_chado_create_tripalfields_custom(&$info, $details, $entity_type,
   $cvterm_id  = $details['chado_cvterm_id'];
   $schema = chado_get_schema($table_name);
 
-  // BASE DBXREF
-  if (array_key_exists('dbxref_id', $schema['fields'])) {
-    $field_name = $table_name . '__dbxref_id';
-    $field_type = 'chado_base__dbxref_id';
+
+  // BASE ORGANISM_ID
+  if ($table_name != 'organism' and array_key_exists('organism_id', $schema['fields'])) {
+    $field_name = $bundle->name . '_obi__organism';
+    $field_type = 'obi__organism';
     $info[$field_name] = array(
       'field_name' => $field_name,
       'type' => $field_type,
@@ -240,16 +233,16 @@ function tripal_chado_create_tripalfields_custom(&$info, $details, $entity_type,
       ),
       'settings' => array(
         'chado_table' => $table_name,
-        'chado_column' => 'dbxref_id',
-        'semantic_web' => tripal_get_chado_semweb_term($table_name, 'dbxref_id'),
+        'chado_column' => 'organism_id',
       ),
     );
   }
+  return;
 
-  // BASE ORGANISM_ID
-  if ($table_name != 'organism' and array_key_exists('organism_id', $schema['fields'])) {
-    $field_name = $table_name . '__organism_id';
-    $field_type = 'chado_base__organism_id';
+  // BASE DBXREF
+  if (array_key_exists('dbxref_id', $schema['fields'])) {
+    $field_name = $table_name . '__dbxref_id';
+    $field_type = 'chado_base__dbxref_id';
     $info[$field_name] = array(
       'field_name' => $field_name,
       'type' => $field_type,
@@ -260,12 +253,14 @@ function tripal_chado_create_tripalfields_custom(&$info, $details, $entity_type,
       ),
       'settings' => array(
         'chado_table' => $table_name,
-        'chado_column' => 'organism_id',
-        'semantic_web' => tripal_get_chado_semweb_term($table_name, 'organism_id'),
+        'chado_column' => 'dbxref_id',
+        'semantic_web' => tripal_get_chado_semweb_term($table_name, 'dbxref_id'),
       ),
     );
   }
 
+
+
   // FEATURE MD5CHECKSUM
   if ($table_name == 'feature') {
     $field_name = $table_name . '__md5checksum';
@@ -373,7 +368,7 @@ function tripal_chado_create_tripalfields_custom(&$info, $details, $entity_type,
  *
  * @param unknown $details
  */
-function tripal_chado_create_tripalfields_linker(&$info, $details, $entity_type, $bundle) {
+function tripal_chado_bundle_create_fields_linker(&$info, $details, $entity_type, $bundle) {
 
   $table_name = $details['chado_table'];
   $type_table = $details['chado_type_table'];
@@ -663,7 +658,7 @@ function tripal_chado_create_tripalfields_linker(&$info, $details, $entity_type,
  * This is a Tripal defined hook that supports integration with the
  * TripalEntity field.
  */
-function tripal_chado_create_tripalfield_instance2($entity_type, $bundle) {
+function tripal_chado_bundle_create_instances($entity_type, $bundle) {
 
   $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
   $vocab = $term->vocab;
@@ -682,11 +677,22 @@ function tripal_chado_create_tripalfield_instance2($entity_type, $bundle) {
     'chado_type_column' => $mapped_table->chado_field,
   );
 
-  tripal_chado_create_tripalfield_instance_base($info, $entity_type, $bundle, $details);
-  tripal_chado_create_tripalfield_instance_custom($info, $entity_type, $bundle, $details);
-  tripal_chado_create_tripalfield_instance_linker($info, $entity_type, $bundle, $details);
+  tripal_chado_bundle_create_instances_base($info, $entity_type, $bundle, $details);
+  tripal_chado_bundle_create_instances_custom($info, $entity_type, $bundle, $details);
+  //tripal_chado_bundle_create_instances_linker($info, $entity_type, $bundle, $details);
+
+  foreach ($info as $field_name => $details) {
+    // If the field is already attached to this bundle then skip it.
+    $field = field_info_field($details['field_name']);
+    if ($field and array_key_exists('bundles', $field) and
+        array_key_exists('TripalEntity', $field['bundles']) and
+        in_array($bundle_name, $field['bundles']['TripalEntity'])) {
+      continue;
+    }
+    // Create the field instance.
+    $instance = field_create_instance($details);
+  }
 
-  return $info;
 }
 /**
  * Helper function for the hook_create_tripalfield_instance().
@@ -697,7 +703,7 @@ function tripal_chado_create_tripalfield_instance2($entity_type, $bundle) {
  * @param $bundle
  * @param $details
  */
-function tripal_chado_create_tripalfield_instance_base(&$info, $entity_type, $bundle, $details) {
+function tripal_chado_bundle_create_instances_base(&$info, $entity_type, $bundle, $details) {
   $fields = array();
 
   // Get Chado information
@@ -873,7 +879,7 @@ function tripal_chado_create_tripalfield_instance_base(&$info, $entity_type, $bu
  * @param $bundle
  * @param $details
  */
-function tripal_chado_create_tripalfield_instance_custom(&$info, $entity_type, $bundle, $details) {
+function tripal_chado_bundle_create_instances_custom(&$info, $entity_type, $bundle, $details) {
   $table_name = $details['chado_table'];
   $type_table = $details['chado_type_table'];
   $type_field = $details['chado_type_column'];
@@ -881,22 +887,26 @@ function tripal_chado_create_tripalfield_instance_custom(&$info, $entity_type, $
   $cvterm_id  = $details['chado_cvterm_id'];
   $schema = chado_get_schema($table_name);
 
-  // BASE DBXREF
-  if (array_key_exists('dbxref_id', $schema['fields'])) {
-    $field_name = $table_name . '__dbxref_id';
-    $info[$field_name] = array(
+  // BASE ORGANISM_ID
+  if ($table_name != 'organism' and array_key_exists('organism_id', $schema['fields'])) {
+    $field_name = $bundle->name . '_obi__organism';
+    $is_required = FALSE;
+    if (array_key_exists('not null', $schema['fields']['organism_id']) and
+        $schema['fields']['organism_id']['not null']) {
+      $is_required = TRUE;
+    }
+    $info[$field_name] =  array(
       'field_name' => $field_name,
       'entity_type' => $entity_type,
       'bundle' => $bundle->name,
-      'label' => 'Accession',
-      'description' => 'This field specifies the unique stable accession (ID) for
-        this record. It requires that this site have a database entry.',
-      'required' => FALSE,
+      'label' => 'Organism',
+      'description' => 'Select an organism.',
+      'required' => $is_required,
       'settings' => array(
         'auto_attach' => TRUE,
       ),
       'widget' => array(
-        'type' => 'chado_base__dbxref_id_widget',
+        'type' => 'obi__organism_widget',
         'settings' => array(
           'display_label' => 1,
         ),
@@ -904,33 +914,30 @@ function tripal_chado_create_tripalfield_instance_custom(&$info, $entity_type, $
       'display' => array(
         'default' => array(
           'label' => 'inline',
-          'type' => 'chado_base__dbxref_id_formatter',
+          'type' => 'obi__organism_formatter',
           'settings' => array(),
         ),
       ),
     );
   }
+  return;
 
-  // BASE ORGANISM_ID
-  if ($table_name != 'organism' and array_key_exists('organism_id', $schema['fields'])) {
-    $field_name = $table_name . '__organism_id';
-    $is_required = FALSE;
-    if (array_key_exists('not null', $schema['fields']['organism_id']) and
-        $schema['fields']['organism_id']['not null']) {
-      $is_required = TRUE;
-    }
-    $info[$field_name] =  array(
+  // BASE DBXREF
+  if (array_key_exists('dbxref_id', $schema['fields'])) {
+    $field_name = $table_name . '__dbxref_id';
+    $info[$field_name] = array(
       'field_name' => $field_name,
       'entity_type' => $entity_type,
       'bundle' => $bundle->name,
-      'label' => 'Organism',
-      'description' => 'Select an organism.',
-      'required' => $is_required,
+      'label' => 'Accession',
+      'description' => 'This field specifies the unique stable accession (ID) for
+        this record. It requires that this site have a database entry.',
+      'required' => FALSE,
       'settings' => array(
         'auto_attach' => TRUE,
       ),
       'widget' => array(
-        'type' => 'chado_base__organism_id_widget',
+        'type' => 'chado_base__dbxref_id_widget',
         'settings' => array(
           'display_label' => 1,
         ),
@@ -938,7 +945,7 @@ function tripal_chado_create_tripalfield_instance_custom(&$info, $entity_type, $
       'display' => array(
         'default' => array(
           'label' => 'inline',
-          'type' => 'chado_base__organism_id_formatter',
+          'type' => 'chado_base__dbxref_id_formatter',
           'settings' => array(),
         ),
       ),
@@ -1106,7 +1113,7 @@ function tripal_chado_create_tripalfield_instance_custom(&$info, $entity_type, $
  * @param unknown $bundle
  * @param unknown $details
  */
-function tripal_chado_create_tripalfield_instance_linker(&$info, $entity_type, $bundle, $details) {
+function tripal_chado_bundle_create_instances_linker(&$info, $entity_type, $bundle, $details) {
 
   $table_name = $details['chado_table'];
   $type_table = $details['chado_type_table'];

+ 36 - 36
tripal_chado/tripal_chado.module

@@ -586,42 +586,42 @@ function tripal_chado_theme($existing, $type, $theme, $path) {
   $themes = array(
 
     // Theme fields.
-    'chado_base__dbxref_id_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/TripalFields/chado_base__dbxref_id.inc',
-    ),
-    'chado_linker__dbxref_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/TripalFields/chado_linker__dbxref.inc',
-    ),
-    'chado_linker__cvterm_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/TripalFields/chado_linker__cvterm.inc',
-    ),
-    'chado_linker__synonym_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/TripalFields/chado_linker__synonym.inc',
-    ),
-    'chado_linker__relationship_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/TripalFields/chado_linker__relationship.inc',
-    ),
-    'chado_linker__relationship_instance_settings' => array(
-      'render element' => 'element',
-      'file' => 'includes/TripalFields/chado_linker__relationship.inc',
-    ),
-    'chado_linker__pub_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/TripalFields/chado_linker__pub.inc',
-    ),
-    'chado_linker__contact_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/TripalFields/chado_linker__contact.inc',
-    ),
-    'chado_linker__prop_adder_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/TripalFields/chado_linker__prop_adder.inc',
-    ),
+//     'chado_base__dbxref_id_widget' => array(
+//       'render element' => 'element',
+//       'file' => 'includes/TripalFields/chado_base__dbxref_id.inc',
+//     ),
+//     'chado_linker__dbxref_widget' => array(
+//       'render element' => 'element',
+//       'file' => 'includes/TripalFields/chado_linker__dbxref.inc',
+//     ),
+//     'chado_linker__cvterm_widget' => array(
+//       'render element' => 'element',
+//       'file' => 'includes/TripalFields/chado_linker__cvterm.inc',
+//     ),
+//     'chado_linker__synonym_widget' => array(
+//       'render element' => 'element',
+//       'file' => 'includes/TripalFields/chado_linker__synonym.inc',
+//     ),
+//     'chado_linker__relationship_widget' => array(
+//       'render element' => 'element',
+//       'file' => 'includes/TripalFields/chado_linker__relationship.inc',
+//     ),
+//     'chado_linker__relationship_instance_settings' => array(
+//       'render element' => 'element',
+//       'file' => 'includes/TripalFields/chado_linker__relationship.inc',
+//     ),
+//     'chado_linker__pub_widget' => array(
+//       'render element' => 'element',
+//       'file' => 'includes/TripalFields/chado_linker__pub.inc',
+//     ),
+//     'chado_linker__contact_widget' => array(
+//       'render element' => 'element',
+//       'file' => 'includes/TripalFields/chado_linker__contact.inc',
+//     ),
+//     'chado_linker__prop_adder_widget' => array(
+//       'render element' => 'element',
+//       'file' => 'includes/TripalFields/chado_linker__prop_adder.inc',
+//     ),
     'tripal_chado_date_combo' => array(
       'render element' => 'element',
       'file' => 'theme/tripal_chado.theme.inc',