Browse Source

Updated semantic web settings. Started new contact field

Stephen Ficklin 8 years ago
parent
commit
783ae718fb

+ 4 - 2
tripal/api/tripal.entities.api.inc

@@ -387,7 +387,9 @@ function hook_add_bundle_fields($entity_type, $bundle, $term) {
   // recommended to support mashups of data via web services.
   $semantic_web = array(
     // The type is the term from a vocabulary that desribes this field..
-    'type' => '',
+    'name' => '',
+    // The accession in the namespace for this term.
+    'accession' => '',
     // The namepsace for the vocabulary (e.g. 'foaf').
     'ns' => '',
     // The URL for the namespace.  It must be that the type can be
@@ -946,7 +948,7 @@ function tripal_get_term_details($namespace, $accession) {
     $module = $stores[$keys[0]]['module'];
     $function = $module . '_vocab_get_term';
     if (function_exists($function)) {
-      return $function($vocab->namespace, $this->accession);
+      return $function($namespace, $accession);
     }
   }
 }

+ 16 - 8
tripal/includes/TripalField.inc

@@ -209,27 +209,35 @@ class TripalField {
    *       available.
    *     - 'value' is the key indicating the value of this field. It should
    *       always be set.  The value of the 'value' key will be the contents
-   *       used for web services and for downloadable content.
+   *       used for web services and for downloadable content.  The value
+   *       should be of the follow format types: 1) A single value (text,
+   *       numeric, etc.) 2) An array of key value pair. 3) If multiple entries
+   *       then cardinality should incremented and format types 1 and 2 should
+   *       be used for each item.
    *   The array may contain as many other keys at the same level as 'value'
    *   but those keys are for internal field use and are not considered the
    *   value of the field.
    *
+   *
    */
   public function load($field, $entity, $details) {
 
   }
 
   /**
-   * Formats the field for dipslay via web services.
+   * Provides a form for the 'Field Settings' of the field management page.
    *
-   * The tripal_ws module will look for this funcion to give the field the
-   * opportunity fo customize the value or array that is present in the
-   * JSON output of web services.
+   * This is an optional hook function and is similar to the
+   * hook_field_settings_form function().
    *
-   * @return
-   *   A single value or an array of values that should be used for this field.
+   * @param $field
+   *   The field structure being configured.
+   * @param $instance
+   *   The instance structure being configured.
+   * @param $has_data
+   *   TRUE if the field already has data, FALSE if not.
    */
-  public function ws_formatter($entity_type, $entity, $field, $instance, $items) {
+  public function settings_form($field, $instance, $has_data) {
 
   }
 

+ 3 - 1
tripal/tripal.module

@@ -968,7 +968,9 @@ function tripal_add_bundle_fields($entity_type, $bundle, $term) {
     'field_settings' => array(
       'semantic_web' => array(
         // The type is the term from a vocabulary that desribes this field..
-        'type' => '',
+        'name' => '',
+        // The accession of the term in the namespace.
+        'accession' => '',
         // The namepsace for the vocabulary (e.g. 'foaf').
         'ns' => '',
         // The URL for the namespace.  It must be that the type can be

+ 2 - 1
tripal_chado/includes/fields/chado_base__dbxref_id.inc

@@ -60,7 +60,8 @@ class chado_base__dbxref_id extends TripalField {
         'chado_table' => $table_name,
         'chado_column' => 'dbxref_id',
         'semantic_web' => array(
-          'type' => '',
+          'name' => '',
+          'accession' => '',
           'ns' => '',
           'nsurl' => '',
         ),

+ 89 - 103
tripal_chado/includes/fields/chado_base__organism_id.inc

@@ -11,7 +11,9 @@ class chado_base__organism_id extends TripalField {
       'description' => t('A field for specifying an organism.'),
       'default_widget' => 'chado_base__organism_id_widget',
       'default_formatter' => 'chado_base__organism_id_formatter',
-      'settings' => array(),
+      'settings' => array(
+        'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
+      ),
       'storage' => array(
         'type' => 'field_chado_storage',
         'module' => 'tripal_chado',
@@ -45,7 +47,7 @@ class chado_base__organism_id extends TripalField {
       'field_type' => 'chado_base__organism_id',
       'widget_type' => 'chado_base__organism_id_widget',
       'description' => 'Select an organism.',
-      'label' => 'Oraganism',
+      'label' => 'Organism',
       'is_required' => 0,
       'storage' => 'field_chado_storage',
       'widget_settings' => array(
@@ -55,7 +57,8 @@ class chado_base__organism_id extends TripalField {
         'chado_table' => $table_name,
         'chado_column' => 'organism_id',
         'semantic_web' => array(
-          'type' => 'organism',
+          'name' => 'organism',
+          'accession' => 'organism',
           'ns' => 'local',
           'nsurl' => '',
         ),
@@ -82,41 +85,93 @@ class chado_base__organism_id extends TripalField {
       'label' => t('Organism'),
       'field types' => array('chado_base__organism_id'),
       'settings' => array(
-        'field_display_teaser' => 0,
-        'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
       ),
     );
   }
+
   /**
-   * @see TripalField::formatter_settings_summary()
+   * @see TripalField::formatter_view()
    */
-  public function formatter_settings_summary($field, $instance, $view_mode) {
-    $display = $instance['display'][$view_mode];
-    $settings = $display['settings'];
-    $summary = 'Settings summary: ';
-    if (array_key_exists('field_display_teaser', $settings) and
-      $settings['field_display_teaser'] == 1) {
-      $summary .= 'displays the organism teaser if published.';
-    }
-    else if (array_key_exists('field_display_string', $settings)) {
-      $summary .= 'uses the token string "' . $settings['field_display_string'] . '"';
-    }
-    else {
-      $summary .= "uses display defaults.";
-    }
+  public function formatter_view(&$element, $entity_type, $entity,
+      $field, $instance, $langcode, $items, $display) {
 
-    return $summary;
+    // 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' => $items[0]['value'],
+    );
   }
 
   /**
-   * @see TripalField::formatter_settings_form()
+   * @see TripalField::widget()
    */
-  public function formatter_settings_form($field, $instance,
-      $view_mode, $form, &$form_state) {
+  public function widget_form(&$widget, $form, $form_state, $field, $instance,
+      $langcode, $items, $delta, $element) {
+
+    $options = tripal_get_organism_select_options(FALSE);
+    $widget['value'] = array(
+      '#type' => 'select',
+      '#title' => $element['#title'],
+      '#description' => $element['#description'],
+      '#options' => $options,
+      '#default_value' => count($items) > 0 ? $items[0]['organism_id'] : 0,
+      '#required' => $element['#required'],
+      '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
+      '#delta' => $delta,
+      '#element_validate' => array('chado_base__organism_id_widget_validate'),
+    );
+    $widget['add_organism'] = array(
+      '#type' => 'item',
+      '#markup' => l('Add a new species', 'admin/content/bio_data/add/species', array('attributes' => array('target' => '_blank'))),
+    );
+  }
+
+  /**
+   * @see TripalField::ws_formatter()
+   */
+  public function ws_formatter($entity_type, $entity, $field, $instance, $items) {
+
+    $organism = (array) $entity->chado_record->organism_id;
+    unset($organism->tablename);
+
+    return $organism;
+  }
+
+  /**
+   * @see TripalField::load()
+   */
+  public function load($field, $entity, $details) {
+
+    $record = $details['record'];
+    $settings = $field['settings'];
+
+    $field_name = $field['field_name'];
+    $field_type = $field['type'];
+    $field_table = $field['settings']['chado_table'];
+    $field_column = $field['settings']['chado_column'];
+
+    $organism = $record->organism_id;
+    $string = $settings['field_display_string'];
+    $value = tripal_replace_chado_tokens($string, $organism);
+
+    $entity->{$field_name}['und'][0]['value'] = $value;
+    $entity->{$field_name}['und'][0]['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]['entity_id'] = $fk_entity_id;
+      $entity->{$field_name}['und'][0]['entity_type'] = 'TripalEntity';
+    }
+  }
 
-    $display = $instance['display'][$view_mode];
-    $settings = $display['settings'];
+  /**
+   * @see TripalField::settings_form()
+   */
+  public function settings_form($field, $instance, $view_mode) {
 
+    $settings = $field['settings'];
 
     $element = array();
     $term = NULL;
@@ -125,7 +180,7 @@ class chado_base__organism_id extends TripalField {
     // Check to see if the organism bundle exists
     $term = tripal_load_term_entity(array(
       'namespace' => $field['settings']['semantic_web']['ns'],
-      'accession' => $field['settings']['semantic_web']['type']
+      'accession' => $field['settings']['semantic_web']['accession']
     ));
     if ($term) {
       $entity = tripal_load_bundle_entity(array('term_id' => $term->id));
@@ -133,28 +188,22 @@ class chado_base__organism_id extends TripalField {
 
     $element['instructions'] = array(
       '#type' => 'item',
-      '#markup' => 'Please provide the format for viewing the organism. You
-        can specify the format using tokens that correspond to each field'
+      '#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' => 'Display Format',
+      '#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['field_display_teaser'] = array(
-      '#type' => 'checkbox',
-      '#title' => 'Display teaser if available',
-      '#description' => t('If the organism that this field is associated with is
-          a published page then display the teaser rather use the display
-          format above.'),
-      '#default_value' => $settings['field_display_teaser'],
-    );
-
     $element['tokens'] = array(
       '#type' => 'fieldset',
       '#collapsed' => TRUE,
@@ -192,73 +241,10 @@ class chado_base__organism_id extends TripalField {
     );
 
     return $element;
-
-  }
-
-  /**
-   * @see TripalField::formatter_view()
-   */
-  public function formatter_view(&$element, $entity_type, $entity,
-      $field, $instance, $langcode, $items, $display) {
-
-    // Get the settings
-    $settings = $display['settings'];
-    $record = $entity->chado_record;
-
-    foreach ($items as $delta => $item) {
-      $organism = $record->organism_id;
-
-      if ($settings['field_display_teaser']) {
-      }
-      else {
-        $field_name = $field['field_name'];
-        $string = $settings['field_display_string'];
-        $field_data = $entity->$field_name;
-        $content = tripal_replace_chado_tokens($string, $organism);
-        $element[$delta] = array(
-          '#type' => 'markup',
-          '#markup' => $content,
-        );
-      }
-    }
-  }
-
-  /**
-   * @see TripalField::widget()
-   */
-  public function widget_form(&$widget, $form, $form_state, $field, $instance,
-      $langcode, $items, $delta, $element) {
-
-    $options = tripal_get_organism_select_options(FALSE);
-    $widget['value'] = array(
-      '#type' => 'select',
-      '#title' => $element['#title'],
-      '#description' => $element['#description'],
-      '#options' => $options,
-      '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
-      '#required' => $element['#required'],
-      '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
-      '#delta' => $delta,
-      '#element_validate' => array('chado_base__organism_id_widget_validate'),
-    );
-    $widget['add_organism'] = array(
-      '#type' => 'item',
-      '#markup' => l('Add a new species', 'admin/content/bio_data/add/species', array('attributes' => array('target' => '_blank'))),
-    );
-  }
-
-  /**
-   * @see TripalField::ws_formatter()
-   */
-  public function ws_formatter($entity_type, $entity, $field, $instance, $items) {
-
-    $organism = (array) $entity->chado_record->organism_id;
-    unset($organism->tablename);
-
-    return $organism;
   }
 }
 
+
 /**
  * Callback function for validating the chado_base__organism_id_widget.
  */

+ 2 - 1
tripal_chado/includes/fields/chado_feature__md5checksum.inc

@@ -63,7 +63,8 @@ function chado_feature__md5checksum_attach_info($entity_type, $bundle, $target)
       'chado_table' => $table_name,
       'chado_column' => 'md5checksum',
       'semantic_web' => array(
-        'type' => '',
+        'name' => '',
+        'accession' => '',
         'ns' => '',
         'nsurl' => '',
       ),

+ 2 - 1
tripal_chado/includes/fields/chado_feature__residues.inc

@@ -62,7 +62,8 @@ function chado_feature__residues_attach_info($entity_type, $bundle, $target) {
       'chado_table' => $table_name,
       'chado_column' => 'residues',
       'semantic_web' => array(
-        'type' => '',
+        'name' => '',
+        'accession' => '',
         'ns' => '',
         'nsurl' => '',
       ),

+ 2 - 1
tripal_chado/includes/fields/chado_feature__seqlen.inc

@@ -62,7 +62,8 @@ function chado_feature__seqlen_attach_info($entity_type, $bundle, $target) {
       'chado_table' => $table_name,
       'chado_column' => 'seqlen',
       'semantic_web' => array(
-        'type' => '',
+        'name' => '',
+        'accession' => '',
         'ns' => '',
         'nsurl' => '',
       ),

+ 23 - 8
tripal_chado/includes/fields/chado_gene__transcripts.inc

@@ -57,9 +57,10 @@ class chado_gene__transcripts extends TripalField {
         'chado_column' => $pkey,
         'base_table' => $table_name,
         'semantic_web' => array(
-          'type' => '',
-          'ns' => '',
-          'nsurl' => '',
+          'name' => 'transcript',
+          'accession' => '0000673',
+          'ns' => 'SO',
+          'nsurl' => 'http://www.sequenceontology.org',
         ),
       ),
     );
@@ -118,15 +119,19 @@ class chado_gene__transcripts extends TripalField {
     $headers = array('Feature Name', 'Unique Name', 'Type', 'Location');
     $rows = array();
     foreach ($items as $delta => $item) {
+      $entity_id = $item['entity_id'];
       $transcript = $item['value'];
+
+      // Get the field values
       $feature_name = $transcript['name'];
       $feature_uname = $transcript['unique name'];
-      $entity_id = $transcript['entity_id'];
+      $loc = $transcript['location'];
+      $type = $transcript['type'];
+
+      // Add a link i there is an entity.
       if ($entity_id) {
         $feature_name = l($feature_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
       }
-      $loc = $transcript['location'];
-      $type = $transcript['type'];
       $rows[] = array($feature_name, $feature_uname, $type, $loc);
     }
     $table = array(
@@ -163,6 +168,10 @@ class chado_gene__transcripts extends TripalField {
     // we may need to write a custom function to get the data.
     $rels = tripal_get_feature_relationships($record);
 
+    // TODO: what if other transcripts names from SO are used. In that
+    // case we should support those too (using cvtermpath table to find them).
+    // mRNA should not be hard-coded below.
+
     // Set the value to be a array of "table" rows.
     $transcripts = array();
     if (key_exists('part of', $rels['object']) &&
@@ -187,12 +196,18 @@ class chado_gene__transcripts extends TripalField {
         'type' => $transcript->record->subject_id->type_id->name,
         'unique name' => $transcript->record->subject_id->uniquename,
         'location' => $loc,
-        'entity_id' => $entity_id,
-        'entity_type' => 'TripalEntity',
+
       );
+      $entity->{$field_name}['und'][$i]['entity_id'] = $entity_id;
+      $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
       $i++;
     }
   }
+  /**
+   * @see TripalField::settings_form()
+   */
+  public function settings_form($field, $instance, $view_mode) {
+  }
   /**
    * @see TripalField::widget_form()
    */

+ 151 - 0
tripal_chado/includes/fields/chado_linker__contact.inc

@@ -0,0 +1,151 @@
+<?php
+
+class chado_linker__contact extends TripalField {
+
+  public function field_info() {
+    return array(
+      'label' => t('Contacts'),
+      'description' => t('Associates an indviddual or organization with
+          this record.'),
+      'default_widget' => 'chado_linker__contact_widget',
+      'default_formatter' => 'chado_linker__contact_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    );
+  }
+ function widget_info() {
+    return array(
+      'label' => t('Contacts'),
+      'field types' => array('chado_linker__contact'),
+    );
+  }
+
+  public function formatter_info() {
+    return array(
+      'label' => t('Contacts'),
+      'field types' => array('chado_linker__contact'),
+      'settings' => array(
+      ),
+    );
+  }
+  public function attach_info($entity_type, $bundle, $settings) {
+    $field_info = array();
+
+    $table_name = $target['data_table'];
+    $type_table = $target['type_table'];
+    $type_field = $target['field'];
+    $cv_id      = $target['cv_id'];
+    $cvterm_id  = $target['cvterm_id'];
+
+    // If the linker table does not exists then we don't want to add attach.
+    $contact_table = $table_name . '_contact';
+    if (!chado_table_exists($contact_table)) {
+      return $field_info;
+    }
+
+    $schema = chado_get_schema($contact_table);
+    $pkey = $schema['primary key'][0];
+
+    // Initialize the field array.
+    $field_info = array(
+      'field_name' => $table_name . '__contact',
+      'field_type' => 'chado_linker__contact',
+      'widget_type' => 'chado_linker__contact_widget',
+      'widget_settings' => array('display_label' => 1),
+      'description' => '',
+      'label' => 'Contacts',
+      'is_required' => 0,
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'storage' => 'field_chado_storage',
+      'field_settings' => array(
+        'chado_table' => $contact_table,
+        'chado_column' => $pkey,
+        'base_table' => $table_name,
+        'semantic_web' => array(
+          'name' => '',
+          'accession' => '',
+          'ns' => '',
+          'nsurl' => '',
+        ),
+      ),
+    );
+    return $field_info;
+  }
+
+  public function formatter_settings_summary($field, $instance, $view_mode) {
+
+  }
+
+  public function formatter_settings_form($field, $instance,
+      $view_mode, $form, &$form_state) {
+
+  }
+
+
+  public function formatter_view(&$element, $entity_type, $entity,
+      $field, $instance, $langcode, $items, $display) {
+  }
+
+
+  public function widget_form(&$widget, $form, $form_state, $field, $instance,
+      $langcode, $items, $delta, $element) {
+
+  }
+
+  public function load($field, $entity, $details) {
+    $field_name = $field['field_name'];
+    $field_type = $field['type'];
+    $field_table = $field['settings']['chado_table'];
+    $field_column = $field['settings']['chado_column'];
+
+    // Get the FK that links to the base record.
+    $schema = chado_get_schema($field_table);
+    $pkey = $schema['primary key'][0];
+    $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
+    $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
+
+    // Set some defaults for the empty record.
+    $entity->{$field_name}['und'][0] = array(
+      'value' => '',
+      $field_table . '__' . $fkey_lcolumn => '',
+      $field_table . '__' . 'contact_id' => '',
+      $field_table . '--' . 'contact__name' => '',
+    );
+
+    $linker_table = $base_table . '_contact';
+    $options = array(
+      'return_array' => 1,
+    );
+    $record = chado_expand_var($record, 'table', $linker_table, $options);
+
+    if (count($record->$linker_table) > 0) {
+      $i = 0;
+      foreach ($record->$linker_table as $index => $linker) {
+        $contact = $linker->contact_id;
+        $entity->{$field_name}['und'][$i] = array(
+          'value' => $linker->$pkey,
+          $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
+          $field_table . '__' . 'contact_id' => $contact->contact_id,
+          $field_table . '--' . 'contact__name' => $contact->name,
+        );
+        if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
+          $fk_entity_id = $entity->chado_record->$field_column->entity_id;
+          $entity->{$field_name}['und'][$i]['entity_id'] = $fk_entity_id;
+          $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
+        }
+
+        $i++;
+      }
+    }
+  }
+
+  public function settings_form($field, $instance, $has_data) {
+
+  }
+
+
+}

+ 2 - 1
tripal_chado/includes/fields/chado_linker__dbxref.inc

@@ -70,7 +70,8 @@ function chado_linker__dbxref_attach_info($entity_type, $bundle, $target) {
       'chado_column' => $pkey,
       'base_table' => $table_name,
       'semantic_web' => array(
-        'type' => '',
+        'name' => '',
+        'accession' => '',
         'ns' => '',
         'nsurl' => '',
       ),

+ 2 - 1
tripal_chado/includes/fields/chado_linker__featureloc.inc

@@ -66,7 +66,8 @@ function chado_linker__featureloc_attach_info($entity_type, $bundle, $target) {
       'chado_column' => $pkey,
       'base_table' => 'feature',
       'semantic_web' => array(
-        'type' => '',
+        'name' => '',
+        'accession' => '',
         'ns' => '',
         'nsurl' => '',
       ),

+ 2 - 1
tripal_chado/includes/fields/chado_linker__pub.inc

@@ -67,7 +67,8 @@ function chado_linker__pub_attach_info($entity_type, $bundle, $target) {
       'chado_column' => $pkey,
       'base_table' => $table_name,
       'semantic_web' => array(
-        'type' => '',
+        'name' => '',
+        'accession' => '',
         'ns' => '',
         'nsurl' => '',
       ),

+ 2 - 1
tripal_chado/includes/fields/chado_linker__relationship.inc

@@ -67,7 +67,8 @@ function chado_linker__relationship_attach_info($entity_type, $bundle, $target)
       'chado_column' => $pkey,
       'base_table' => $table_name,
       'semantic_web' => array(
-        'type' => '',
+        'name' => '',
+        'accession' => '',
         'ns' => '',
         'nsurl' => '',
       ),

+ 2 - 1
tripal_chado/includes/fields/chado_linker__synonym.inc

@@ -66,7 +66,8 @@ function chado_linker__synonym_attach_info($entity_type, $bundle, $target) {
       'chado_column' => $pkey,
       'base_table' => $table_name,
       'semantic_web' => array(
-        'type' => '',
+        'name' => '',
+        'accession' => '',
         'ns' => '',
         'nsurl' => '',
       ),

+ 1 - 2
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -327,14 +327,13 @@ 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.
-        $load_function = $field_type . '_load';
         module_load_include('inc', $field_module, 'includes/fields/' . $field_type);
-
         if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
           $field_obj = new $field_type();
           $field_obj->load($field, $entity, array('record' => $record));
         }
 
+        $load_function = $field_type . '_load';
         if (function_exists($load_function)) {
           $load_function($field, $entity, $base_table, $record);
         }

+ 33 - 51
tripal_chado/includes/tripal_chado.fields.inc

@@ -99,6 +99,28 @@ function tripal_chado_field_formatter_info() {
   }
   return $formatters;
 }
+
+/**
+ * Implements hook_field_settings_form()
+ */
+function tripal_chado_field_settings_form($field, $instance, $has_data) {
+  dpm($field);
+  $form = '';
+  $field_type = $field['type'];
+  module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
+
+  if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
+    $field_obj = new $field_type();
+    $form = $field_obj->settings_form($field, $instance, $has_data);
+  }
+
+  $function = $field_type . '_settings_form';
+  if (function_exists($function)) {
+    $form = $function($field, $instance, $has_data);
+  }
+
+  return $form;
+}
 /**
  * Implements hook_field_formatter_settings_summary().
  */
@@ -313,7 +335,9 @@ function tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $sche
       'chado_column' => $column_name,
       'semantic_web' => array(
         // The type is the term from a vocabulary that desribes this field..
-        'type' => '',
+        'name' => '',
+        // The accession ID for this term in the vocaulary.
+        'accession' => '',
         // The namepsace for the vocabulary (e.g. 'foaf').
         'ns' => '',
         // The URL for the namespace.  It must be that the type can be
@@ -384,13 +408,15 @@ function tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $sche
 
   // Set some default semantic web information
   if ($column_name == 'name') {
-    $field['field_settings']['semantic_web']['type'] = 'name';
+    $field['field_settings']['semantic_web']['name'] = 'name';
+    $field['field_settings']['semantic_web']['accession'] = 'name';
     $field['field_settings']['semantic_web']['ns'] = 'foaf';
     $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
   }
   if ($column_name == 'description' or $column_name == 'definition' or
       $column_name == 'comment') {
-    $field['field_settings']['semantic_web']['type'] = 'description';
+    $field['field_settings']['semantic_web']['name'] = 'description';
+    $field['field_settings']['semantic_web']['accession'] = 'description';
     $field['field_settings']['semantic_web']['ns'] = 'hydra';
     $field['field_settings']['semantic_web']['nsurl'] = 'http://www.w3.org/ns/hydra/core#';
   }
@@ -416,7 +442,8 @@ function tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $sche
     $field['field_type'] = 'text';
     $field['widget_type'] = 'text_textfield';
     $field['field_settings']['text_processing'] = 0;
-    $field['field_settings']['semantic_web']['type'] = 'name';
+    $field['field_settings']['semantic_web']['name'] = 'name';
+    $field['field_settings']['semantic_web']['accession'] = 'name';
     $field['field_settings']['semantic_web']['ns'] = 'foaf';
     $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
   }
@@ -425,7 +452,8 @@ function tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $sche
   // ANALYSIS TABLE
   //
   elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'program') {
-    $field['field_settings']['semantic_web']['type'] = 'SoftwareApplication';
+    $field['field_settings']['semantic_web']['name'] = 'SoftwareApplication';
+    $field['field_settings']['semantic_web']['accession'] = 'SoftwareApplication';
     $field['field_settings']['semantic_web']['ns'] = 'schema';
     $field['field_settings']['semantic_web']['nsurl'] = 'https://schema.org/';
     $field['description'] = 'The program name (e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package then provide a very brief description of the pipeline, workflow or method.';
@@ -510,52 +538,6 @@ function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_sta
   }
 }
 
-/**
- * Implements hook_field_ws_formatter().
- *
- * The hook function is called by the tripal_ws module which provides
- * the RESTful web services. If that module is not installed this function
- * is never use.d
- */
-function tripal_chado_field_ws_formatter($entity_type, $entity, $field_info,
-    $field, $items){
-
-  $field_name = $field_info['field_name'];
-  $field_type = $field_info['type'];
-
-  $chado_table = $field_info['settings']['chado_table'];
-  $chado_column  = $field_info['settings']['chado_column'];
-
-  // Check to see if this field is associated with a different entity. If so
-  // we want to return the entity_id and not let the field customize the
-  // display as we need to allow the tripal_ws module to link to the other
-  // entity.
-  if (property_exists($entity->chado_record, $chado_column) and
-      property_exists($entity->chado_record->$chado_column, 'entity_id')) {
-     $fk_entity_id = $entity->chado_record->$chado_column->entity_id;
-     $value = array(
-       'entity_id' => $fk_entity_id,
-       'entity_type' => 'TripalEntity'
-     );
-     return $value;
-  }
-
-  // See if the field file defines a formatter.
-  $function = $field_type . '_ws_formatter';
-  module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
-
-  if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
-    $field_obj = new $field_type();
-    $value = $field_obj->ws_formatter($entity_type, $entity, $field_info, $field, $items);
-  }
-
-  $value = '';
-  if (function_exists($function)) {
-    $value = $function($entity_type, $entity, $field_info, $field, $items);
-  }
-
-  return $value;
-}
 
 /**
  * Implements hook_field_is_empty().

+ 36 - 42
tripal_ws/includes/tripal_ws.rest.inc

@@ -436,9 +436,10 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
 
   // Next add in the ID and Type for this resources.
   $response['@id'] = $api_url . '/content/' . $ctype . '/' . $entity_id;
-  $response['@type'] = $vocab->namespace . ':' . $term->accession;
+  $response['@type'] = $term->name;
   $response['label'] = $entity->title;
-  $response['itemPage'] = url('/bio_data/' . $bundle->id, array('absolute' => TRUE));
+  $response['itemPage'] = url('/bio_data/' . $entity->id, array('absolute' => TRUE));
+  $response['@context'][$term->name] = $vocab->namespace . ':' . $term->accession;
 
   // Get information about the fields attached to this bundle and sort them
   // in the order they were set for the display.
@@ -466,21 +467,22 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
     // case that the field is not assigned a term, we must use the field name.
     $key = $field['field_name'];
     if (array_key_exists('semantic_web', $field['settings'])) {
-      if (array_key_exists('type', $field['settings']['semantic_web']) and
-          $field['settings']['semantic_web']['type']) {
-        $key = $field['settings']['semantic_web']['type'];
+      if (array_key_exists('name', $field['settings']['semantic_web']) and
+          $field['settings']['semantic_web']['name']) {
+        $key = $field['settings']['semantic_web']['name'];
       }
     }
 
     // Get the semantic web settings for this field.
     $field_type = '';
     if (array_key_exists('semantic_web', $field['settings'])) {
-      $field_type = $field['settings']['semantic_web']['type'];
-      if ($field_type) {
+      $field_name = $field['settings']['semantic_web']['name'];
+      if ($field_name) {
+        $accession = $field['settings']['semantic_web']['accession'];
         $ns = $field['settings']['semantic_web']['ns'];
         $nsurl = $field['settings']['semantic_web']['nsurl'];
         $response['@context'][$ns] = $nsurl;
-        $response['@context'][$key] = $ns . ':' .$field_type;
+        $response['@context'][$key] = $ns . ':' . $accession;
       }
     }
 
@@ -489,45 +491,37 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
       continue;
     }
 
-    $value = '';
-
-
-    // We want to allow the field to format itself for web services if it
-    // wants to.  If the module implements the hook_field_ws_formatter()
-    // then we'll retrieve the values from it.
-    $field_module = $field_info['storage']['module'];
-    $function = $field_module . '_field_ws_formatter';
-
-    if (function_exists($function)) {
-      $value = $function($entity->type, $entity, $field_info, $field, $items);
-    }
-
-    if (!$value) {
-      // Get the values based on cardinality
-      if (count($items) == 1) {
-        $value = $items[0]['value'];
+    $value = array();
+    for ($i = 0; $i < count($items); $i++) {
+
+      // If the value for this key is an array with an 'entity_id' key then
+      // we want to do a substitution.
+      if (array_key_exists('entity_id', $items[0])) {
+        $lentity = entity_load($items[$i]['entity_type'], array($items[$i]['entity_id']));
+        $lentity = reset($lentity);
+        $lterm = tripal_load_term_entity(array('term_id' => $lentity->term_id));
+        $lvocab = tripal_load_vocab_entity(array('vocab_id' => $lterm->vocab_id));
+        $lterm_details = tripal_get_term_details($lvocab->namespace, $lterm->namespace);
+        $value[] = array(
+          '@id'   => $api_url . '/content/' . $lterm->name . '/' . $items[0]['entity_id'],
+          '@type' => $lterm->name,
+          'label' => $lentity->title,
+        );
+
+        if (!array_key_exists($lvocab->namespace, $response['@context'])) {
+          $response['@context'][$lvocab->namespace] = $lterm_details->url;
+        }
+        // TODO: what if the term is used by multiple vocabularies.
+        $response['@context'][$lterm->name] = $lvocab->namespace . ':' . $lterm->accession;
       }
-      // If cardinality is greater than 1 then the value should be an array
       else {
-        $value = array();
-        for ($i = 0; $i < count($items); $i++) {
-          $value[] = $items[$i]['value'];
-        }
+        $value[] = $items[$i]['value'];
       }
     }
 
-    // If the value for this key is an array with an 'entity_id' key then
-    // we want to do a substitution.
-    if (is_array($value) and array_key_exists('entity_id', $value)) {
-      $lentity = entity_load($value['entity_type'], array($value['entity_id']));
-      $lentity = reset($lentity);
-      $lterm = tripal_load_term_entity(array('term_id' => $lentity->term_id));
-      $lvocab = tripal_load_vocab_entity(array('vocab_id' => $lterm->vocab_id));
-      $value = array(
-        '@id'   => $api_url . '/content/' . $lterm->name . '/' . $value['entity_id'],
-        '@type' => $lvocab->namespace . ':' . $lterm->accession,
-        'label' => $lentity->title,
-      );
+    // Convert a single value to not be an array.
+    if (count($value) == 1) {
+      $value = $value[0];
     }
 
     $response[$key] = $value;