Browse Source

Working on improving entity loading

Stephen Ficklin 8 years ago
parent
commit
f233e628a4

+ 10 - 1
tripal/includes/TripalBundleController.inc

@@ -26,7 +26,16 @@ class TripalBundleController extends EntityAPIControllerExportable {
       'is_new' => TRUE,
       'data' => '',
     );
-    return parent::create($values);
+    $bundle = parent::create($values);
+
+    // Allow modules to make additions to the entity when it's created.
+    $modules = module_implements('bundle_create');
+    foreach ($modules as $module) {
+      $function = $module . '_bundle_create';
+      $function($bundle);
+    }
+
+    return $bundle;
   }
 
   /**

+ 8 - 3
tripal/includes/TripalEntityController.inc

@@ -187,9 +187,14 @@ class TripalEntityController extends EntityAPIController {
         // SQL statement that gets called somewhere by Drupal:
         // SELECT DISTINCT SUBSTRING_INDEX(source, '/', 1) AS path FROM url_alias.
         // Perhaps we should write our own SQL to avoid this issue.
-        path_delete(array('source' => $source_url));
-        $path = array('source' => $source_url, 'alias' => $alias);
-        path_save($path);
+        drupal_write_record('url_alias', array(
+          'source' => $source_url,
+          'alias' => $alias,
+          'language' => 'und',
+        ));
+//        path_delete(array('source' => $source_url));
+//        $path = array('source' => $source_url, 'alias' => $alias);
+//        path_save($path);
       }
       // If there is only one alias matching then it might just be that we already
       // assigned this alias to this entity in a previous save.

+ 140 - 35
tripal/includes/fields/content_type.inc

@@ -1,39 +1,144 @@
 <?php
-/**
- *
- * @param $entity_type
- * @param $entity
- * @param $field
- * @param $instance
- * @param $langcode
- * @param $items
- * @param $display
- */
-function tripal_content_type_formatter(&$element, $entity_type, $entity, $field,
-    $instance, $langcode, $items, $display) {
-
-  foreach ($items as $delta => $item) {
-    $element[$delta] = array(
-      '#type' => 'markup',
-      '#markup' => $item['value'],
+
+class content_type extends TripalField {
+  /**
+   * @see TripalField::field_info()
+   */
+  function field_info() {
+    return array(
+      'label' => t('Record Type'),
+      'description' => t('The content type.'),
+      'default_widget' => 'tripal_content_type_widget',
+      'default_formatter' => 'tripal_content_type_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'tripal_no_storage',
+        'module' => 'tripal',
+        'active' => TRUE
+      ),
+    );
+  }
+  /**
+   * @see TripalField::widget_info()
+   */
+  function widget_info() {
+    return array(
+      'tripal_content_type_widget' => array(
+        'label' => t('Record Type'),
+        'field types' => array('content_type')
+      ),
+    );
+  }
+  /**
+   * @see TripalField::formatter_info()
+   */
+  function formatter_info() {
+    return array(
+      'tripal_content_type_formatter' => array(
+        'label' => t('Record Type'),
+        'field types' => array('content_type')
+      ),
+    );
+  }
+  /**
+   * @see TripalField::can_attach()
+   */
+  protected function can_attach($entity_type, $bundle, $details) {
+    // We always attach to TriplEntity entities.
+    if ($entity_type == 'TripalEntity') {
+      return TRUE;
+    }
+  }
+
+  /**
+   * @see TripalField::create_info()
+   */
+  public function create_info($entity_type, $bundle, $details) {
+
+    if (!$this->can_attach($entity_type, $bundle, $details)) {
+      return;
+    }
+
+    return array(
+      'field_name' => 'content_type',
+      'type' => 'content_type',
+      'cardinality' => 1,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'tripal_no_storage'
+      ),
+      'settings' => array(
+        'semantic_web' => 'rdfs:type',
+      ),
     );
   }
-}
-
-/**
- * Loads the field values with appropriate data.
- *
- * This function is called by the tripal_chado_field_storage_load() for
- * each property managed by the field_chado_storage storage type.  This is
- * an optional hook function that is only needed if the field has
- * multiple form elements.
- *
- * @param $field
- * @param $entity
- */
-function tripal_content_type_field_load($field, $entity) {
-
-  $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
-  $field_name = $field['field_name'];
-  $entity->{$field_name}['und'][0]['value'] = $bundle->label;
+  /**
+   * @see TripalField::create_instance_info()
+   */
+  public function create_instance_info($entity_type, $bundle, $details) {
+
+    if (!$this->can_attach($entity_type, $bundle, $details)) {
+      return;
+    }
+
+    return array(
+      'field_name' => 'content_type',
+      'entity_type' => 'TripalEntity',
+      'bundle' => $bundle->name,
+      'label' => 'Resource Type',
+      'description' => '',
+      'required' => FALSE,
+      'settings' => array(),
+      'widget' => array(
+        'type' => 'tripal_content_type_widget',
+        'settings' => array(
+          'display_label' => 1,
+        ),
+      ),
+      'display' => array(
+        'default' => array(
+          'label' => 'above',
+          'type' => 'tripal_content_type_formatter',
+          'settings' => array(),
+        ),
+      ),
+    );
+  }
+
+  /**
+   * @see TripalField::formatter_view()
+   */
+  function formatter_view(&$element, $entity_type, $entity,
+      $field, $instance, $langcode, $items, $display) {
+
+    foreach ($items as $delta => $item) {
+      $element[$delta] = array(
+        '#type' => 'markup',
+        '#markup' => $item['value'],
+      );
+    }
+  }
+  /**
+   * @see TripalField::widget_form()
+   */
+  function widget_form(&$widget, &$form, &$form_state, $field, $instance,
+      $langcode, $items, $delta, $element) {
+
+    $widget = $element;
+    switch ($instance['widget']['type']) {
+      case 'tripal_content_type_widget':
+        // There is no widget for this type.
+        break;
+    }
+
+  }
+  /**
+   * @see TripalField::load()
+   */
+  function load($field, $entity) {
+
+    $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
+    $field_name = $field['field_name'];
+    $entity->{$field_name}['und'][0]['value'] = $bundle->label;
+  }
 }

+ 41 - 85
tripal/includes/tripal.fields.inc

@@ -4,67 +4,26 @@
  * Implements hook_field_info().
  */
 function tripal_field_info() {
-  $fields = array(
-    'content_type' => array(
-      'label' => t('Record Type'),
-      'description' => t('The content type.'),
-      'default_widget' => 'tripal_content_type_widget',
-      'default_formatter' => 'tripal_content_type_formatter',
-      'settings' => array(),
-      'storage' => array(
-        'type' => 'tripal_no_storage',
-        'module' => 'tripal',
-        'active' => TRUE
-      ),
-    ),
-  );
-  return $fields;
+
+  $info = array();
+  $fields = tripal_get_fields('tripal');
+  foreach ($fields as $field) {
+    $info[] = $field->field_info();
+  }
+
 }
 /**
  * Implements hook_field_widget_info();
  */
 function tripal_field_widget_info() {
-  return array(
-    'tripal_content_type_widget' => array(
-      'label' => t('Record Type'),
-      'field types' => array('content_type')
-    ),
-  );
+
 }
 
 /**
  * Implements hook_field_formatter_info().
  */
 function tripal_field_formatter_info() {
-  return array(
-    'tripal_content_type_formatter' => array(
-      'label' => t('Record Type'),
-      'field types' => array('content_type')
-    ),
-  );
-}
 
-/**
- * Implements hook_create_info().
- *
- * This is a Tripal defined hook that supports integration with the
- * TripalEntity field.
- */
-function tripal_field_create_info($entity_type, $bundle, $term) {
-  return array(
-    'content_type' => array(
-      'field_name' => 'content_type',
-      'type' => 'content_type',
-      'cardinality' => 1,
-      'locked' => FALSE,
-      'storage' => array(
-        'type' => 'tripal_no_storage'
-      ),
-      'settings' => array(
-        'semantic_web' => 'rdf:type',
-      ),
-    ),
-  );
 }
 
 /**
@@ -74,30 +33,7 @@ function tripal_field_create_info($entity_type, $bundle, $term) {
  * TripalEntity field.
  */
 function tripal_field_create_instance_info($entity_type, $bundle, $term) {
-  return array(
-    'content_type' => array(
-      'field_name' => 'content_type',
-      'entity_type' => 'TripalEntity',
-      'bundle' => $bundle->name,
-      'label' => 'Resource Type',
-      'description' => '',
-      'required' => FALSE,
-      'settings' => array(),
-      'widget' => array(
-        'type' => 'tripal_content_type_widget',
-        'settings' => array(
-          'display_label' => 1,
-        ),
-      ),
-      'display' => array(
-        'default' => array(
-          'label' => 'above',
-          'type' => 'tripal_content_type_formatter',
-          'settings' => array(),
-        ),
-      ),
-    ),
-  );
+
 }
 
 /**
@@ -106,12 +42,34 @@ function tripal_field_create_instance_info($entity_type, $bundle, $term) {
 function tripal_field_widget_form(&$form, &$form_state, $field,
     $instance, $langcode, $items, $delta, $element) {
 
-  $widget = $element;
-  switch ($instance['widget']['type']) {
-    case 'tripal_content_type_widget':
-      // There is no widget for this type.
-      break;
+
+}
+/**
+ * Implements hook_field_create_info().
+ *
+ * This is a Tripal defined hook that supports integration with the
+ * TripalEntity field.
+ */
+function tripal_field_create_info($entity_type, $bundle, $term) {
+  $info = array();
+
+  // Find all of the files in the tripal_chado/includes/fields directory.
+  $fields_path = drupal_get_path('module', 'tripal') . '/includes/fields';
+  $field_files = file_scan_directory($fields_path, '/^.inc$/');
+
+  // Iterate through the fields, include the file and run the info function.
+  foreach ($field_files as $file) {
+    $field_type = $file->name;
+    module_load_include('inc', 'tripal', 'includes/fields/' . $field_type);
+    if (class_exists($field_type)) {
+      $field_obj = new $field_type();
+      $result = $field_obj->create_info($entity_type, $bundle, $details);
+      if (is_array($result)) {
+        $info[$result['field_name']] = $result;
+      }
+    }
   }
+  return $info;
 }
 /**
  * Implements hook_field_formatter_view().
@@ -120,14 +78,12 @@ function tripal_field_formatter_view($entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {
 
   $element = array();
-  switch ($display['type']) {
-    case 'tripal_content_type_formatter':
-      module_load_include('inc', 'tripal', 'includes/fields/content_type');
-      tripal_content_type_formatter($element, $entity_type, $entity, $field,
-          $instance, $langcode, $items, $display);
-      break;
+  $field_type = $field['type'];
+  module_load_include('inc', 'tripal', 'includes/fields/' . $field_type);
+  if (class_exists($field_type)) {
+    $field_obj = new $field_type();
+    $field_obj->formatter_view($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
   }
-  return $element;
 }
 /**
  * Implements hook_field_is_empty().

+ 1 - 0
tripal/tripal.module

@@ -531,6 +531,7 @@ function tripal_menu_alter(&$items) {
  */
 function tripal_import_api() {
   module_load_include('inc', 'tripal', 'api/tripal.d3js.api');
+  module_load_include('inc', 'tripal', 'api/tripal.fields.api');
   module_load_include('inc', 'tripal', 'api/tripal.entities.api');
   module_load_include('inc', 'tripal', 'api/tripal.files.api');
   module_load_include('inc', 'tripal', 'api/tripal.jobs.api');

+ 3 - 0
tripal_chado/includes/fields/chado_base__dbxref_id.inc

@@ -33,6 +33,9 @@ class chado_base__dbxref_id extends TripalField {
     // Check the schema for the data table if it does not have
     // a 'dbxref_id' column then we don't want to attach this field.
     $schema = chado_get_schema($table_name);
+    if (!$schema) {
+      return FALSE;
+    }
     if (array_key_exists('dbxref_id', $schema['fields'])) {
       return TRUE;
     }

+ 3 - 0
tripal_chado/includes/fields/chado_base__organism_id.inc

@@ -33,6 +33,9 @@ class chado_base__organism_id extends TripalField {
     $cvterm_id  = $details['chado_cvterm_id'];
 
     $schema = chado_get_schema($table_name);
+    if (!$schema) {
+      return FALSE;
+    }
 
     // If this is the organism table then do not attach as the organism_id
     // field is the primary key and we don't want a field for that.

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

@@ -86,7 +86,7 @@ class chado_feature__seqlen extends TripalField {
         are present then this record has no official assigned sequence.',
       'required' => FALSE,
       'settings' => array(
-        'auto_attach' => FALSE,
+        'auto_attach' => TRUE,
       ),
       'widget' => array(
         'type' => 'chado_feature__seqlen_widget',

+ 1 - 1
tripal_chado/includes/fields/chado_organism__type_id.inc

@@ -89,7 +89,7 @@ class chado_organism__type_id extends TripalField {
       'description' => 'The Infrapsecific Type.',
       'required' => FALSE,
       'settings' => array(
-        'auto_attach' => FALSE,
+        'auto_attach' => TRUE,
       ),
       'widget' => array(
         'type' => 'chado_organism__type_id_widget',

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

@@ -1,5 +1,96 @@
 <?php
 
+/**
+ * Implements hook_chado_bundle_create().
+ *
+ * This is a Tripal hook. It allows any module to perform tasks after
+ * a bundle has been craeted.
+ *
+ * @param $bundle
+ *  The TripalBundle object.
+ */
+function triipal_chado_bundle_create(&$bundle) {
+  // The details array is used to pass to the TripalEntity->create_info()
+  // function to provide more details about how the bundle is used by this
+  // module.
+  $details = array();
+
+  // Get the term.
+  $term = entity_load('TripalTerm', array($bundle->term_id));
+  $term = reset($term);
+
+  // Get the cvterm that corresponds to this TripalTerm object.
+  $vocab = entity_load('TripalVocab', array($term->vocab_id));
+  $vocab = reset($vocab);
+  $match = array(
+    'dbxref_id' => array(
+      'db_id' => array(
+        'name' => $vocab->vocabulary,
+      ),
+      'accession' => $term->accession
+    ),
+  );
+  $cvterm = chado_generate_var('cvterm', $match);
+
+  // The organism table does not have a type_id so we won't ever find
+  // a record for it in the tripal_cv_defaults table.
+  if ($cvterm->name == 'organism') {
+    $details = array(
+      'chado_cv_id' => $cvterm->cv_id->cv_id,
+      'chado_cvterm_id' => $cvterm->cvterm_id,
+      'chado_table' => 'organism',
+      'chado_type_table' => 'organism',
+      'chado_type_column' =>  '',
+    );
+  }
+  // The analysis table does not have a type_id so we won't ever find
+  // a record for it in the tripalcv_defaults table.
+  else if ($cvterm->name == 'analysis') {
+    $details = array(
+      'chado_cv_id' => $cvterm->cv_id->cv_id,
+      'chado_cvterm_id' => $cvterm->cvterm_id,
+      'chado_table' => 'analysis',
+      'chado_type_table' => 'analysis',
+      'chado_type_column' =>  '',
+    );
+  }
+  else if ($cvterm->name == 'project') {
+    $details = array(
+      'chado_cv_id' => $cvterm->cv_id->cv_id,
+      'chado_cvterm_id' => $cvterm->cvterm_id,
+      'chado_table' => 'project',
+      'chado_type_table' => 'project',
+      'chado_type_column' =>  '',
+    );
+  }
+  else {
+    // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
+    // TABLES.
+    // Look to see if this vocabulary is used as a default for any table.
+    $default = db_select('tripal_cv_defaults', 't')
+      ->fields('t')
+      ->condition('cv_id', $cvterm->cv_id->cv_id)
+      ->execute()
+      ->fetchObject();
+    if ($default) {
+      $details = array(
+        'chado_cv_id' => $cvterm->cv_id->cv_id,
+        'chado_cvterm_id' => $cvterm->cvterm_id,
+        'chado_table' => $default->table_name,
+        'chado_type_table' => $default->table_name,
+        'chado_type_column' =>  $default->field_name,
+      );
+    }
+  }
+
+  // Save the mapping information so that we can reuse it when we need to
+  // look things up for later (such as the hook_create_instance_info() function.
+  tripal_set_bundle_variable('chado_cv_id', $bundle->id, $details['chado_cv_id']);
+  tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $details['chado_cvterm_id']);
+  tripal_set_bundle_variable('chado_table', $bundle->id, $details['chado_table']);
+  tripal_set_bundle_variable('chado_type_table', $bundle->id, $details['chado_type_table']);
+  tripal_set_bundle_variable('chado_type_column', $bundle->id, $details['chado_type_column']);
+}
 
 /**
  * Implements hook_entity_create().

+ 14 - 81
tripal_chado/includes/tripal_chado.fields.inc

@@ -42,87 +42,14 @@ function tripal_chado_field_info() {
  */
 function tripal_chado_field_create_info($entity_type, $bundle, $term) {
 
-  $bundle_name = $bundle->name;
-
-  // The details array is used to pass to the TripalEntity->create_info()
-  // function to provide more details about how the bundle is used by this
-  // module.
-  $details = array();
-
-  // This array will hold details that map the bundle to tables in Chado.
-  $bundle_data = array();
-
-  // Get the cvterm that corresponds to this TripalTerm object.
-  $vocab = entity_load('TripalVocab', array($term->vocab_id));
-  $vocab = reset($vocab);
-  $match = array(
-    'dbxref_id' => array(
-      'db_id' => array(
-        'name' => $vocab->vocabulary,
-      ),
-      'accession' => $term->accession
-    ),
+  // Get the details about the mapping of this bundle to the Chado table:
+  $details = array(
+    'chado_cv_id' => tripal_get_bundle_variable('chado_cv_id', $bundle->id),
+    'chado_cvterm_id' => tripal_get_bundle_variable('chado_cvterm_id', $bundle->id),
+    'chado_table' => tripal_get_bundle_variable('chado_table', $bundle->id),
+    'chado_type_table' => tripal_get_bundle_variable('chado_type_table', $bundle->id),
+    'chado_type_column' => tripal_get_bundle_variable('chado_type_column', $bundle->id),
   );
-  $cvterm = chado_generate_var('cvterm', $match);
-
-  // The organism table does not have a type_id so we won't ever find
-  // a record for it in the tripal_cv_defaults table.
-  if ($cvterm->name == 'organism') {
-    $details = array(
-      'chado_cv_id' => $cvterm->cv_id->cv_id,
-      'chado_cvterm_id' => $cvterm->cvterm_id,
-      'chado_table' => 'organism',
-      'chado_type_table' => 'organism',
-      'chado_type_column' =>  '',
-    );
-  }
-  // The analysis table does not have a type_id so we won't ever find
-  // a record for it in the tripalcv_defaults table.
-  else if ($cvterm->name == 'analysis') {
-    $details = array(
-      'chado_cv_id' => $cvterm->cv_id->cv_id,
-      'chado_cvterm_id' => $cvterm->cvterm_id,
-      'chado_table' => 'analysis',
-      'chado_type_table' => 'analysis',
-      'chado_type_column' =>  '',
-    );
-  }
-  else if ($cvterm->name == 'project') {
-    $details = array(
-      'chado_cv_id' => $cvterm->cv_id->cv_id,
-      'chado_cvterm_id' => $cvterm->cvterm_id,
-      'chado_table' => 'project',
-      'chado_type_table' => 'project',
-      'chado_type_column' =>  '',
-    );
-  }
-  else {
-    // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
-    // TABLES.
-    // Look to see if this vocabulary is used as a default for any table.
-      $default = db_select('tripal_cv_defaults', 't')
-      ->fields('t')
-      ->condition('cv_id', $cvterm->cv_id->cv_id)
-      ->execute()
-      ->fetchObject();
-    if ($default) {
-      $details = array(
-        'chado_cv_id' => $cvterm->cv_id->cv_id,
-        'chado_cvterm_id' => $cvterm->cvterm_id,
-        'chado_table' => $default->table_name,
-        'chado_type_table' => $default->table_name,
-        'chado_type_column' =>  $default->field_name,
-      );
-    }
-  }
-
-  // Save the mapping information so that we can reuse it when we need to
-  // look things up for later (such as the hook_create_instance_info() function.
-  tripal_set_bundle_variable('chado_cv_id', $bundle->id, $details['chado_cv_id']);
-  tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $details['chado_cvterm_id']);
-  tripal_set_bundle_variable('chado_table', $bundle->id, $details['chado_table']);
-  tripal_set_bundle_variable('chado_type_table', $bundle->id, $details['chado_type_table']);
-  tripal_set_bundle_variable('chado_type_column', $bundle->id, $details['chado_type_column']);
 
   $base_fields = tripal_chado_field_create_base('create_info', $entity_type, $bundle, $details);
   $custom_fields = tripal_chado_field_create_info_custom($entity_type, $bundle, $details);
@@ -187,6 +114,9 @@ function tripal_chado_field_create_info_custom($entity_type, $bundle, $details)
  *   tripal_chado_field_create_instance_info() functions.
  */
 function tripal_chado_field_create_base($step, $entity_type, $bundle, $details) {
+  $fields = array();
+
+  // Get Chado information
   $table_name = $details['chado_table'];
   $type_table = $details['chado_type_table'];
   $type_field = $details['chado_type_column'];
@@ -194,8 +124,11 @@ function tripal_chado_field_create_base($step, $entity_type, $bundle, $details)
   // Iterate through the columns of the table and see if fields have been
   // created for each one. If not, then create them.
   $schema = chado_get_schema($table_name);
+  if (!$schema) {
+    return $fields();
+  }
+
   $columns = $schema['fields'];
-  $fields = array();
   foreach ($columns as $column_name => $details) {
     $field_name = $table_name . '__' . $column_name;
 

+ 3 - 2
tripal_chado/includes/tripal_chado.migrate.inc

@@ -607,7 +607,8 @@ function tripal_chado_migrate_selected_types($tv3_content_types) {
     // If term doesn't exist, create a new bundle for this term
     if (!$term) {
       print("Creating bundle for term '" . $tv3_content_type['term_name'] . "'...\n");
-      $success = tripal_create_bundle($tv3_content_type['vocabulary'], $tv3_content_type['accession'], $tv3_content_type['term_name']);
+      $success = tripal_create_bundle($tv3_content_type['vocabulary'],
+          $tv3_content_type['accession'], $tv3_content_type['term_name']);
       $term = tripal_load_term_entity($tv3_content_type);
     }
     // Create bundle name
@@ -618,7 +619,7 @@ function tripal_chado_migrate_selected_types($tv3_content_types) {
       'sync_node' => 1,
       'bundle_name' => $bundle_name
     );
-    tripal_chado_publish_records ($value);
+    tripal_chado_publish_records($value);
   }
 }
 

+ 16 - 2
tripal_chado/includes/tripal_chado.semweb.inc

@@ -17,6 +17,21 @@ function tripal_chado_populate_chado_semweb_table() {
 
   // Add in vocabularies of terms that will be used for the semantic web
   //
+  tripal_insert_db(array(
+    'name' => 'rdfs',
+    'description' => 'Resource Description Framework Schema',
+    'url' => 'https://www.w3.org/TR/rdf-schema/',
+    'urlprefix' => 'https://www.w3.org/TR/rdf-schema/#ch_',
+  ));
+  tripal_insert_cv('rdfs','Resource Description Framework Schema');
+  $name = tripal_insert_cvterm(array(
+    'id' => 'rdfs:type',
+    'name' => 'type',
+    'cv_name' => 'rdfs',
+    'definition' => 'rdf:type is an instance of rdf:Property that is used to state that a resource is an instance of a class.',
+  ));
+
+  tripal_insert_cv('foaf','Friend of a Friend');
   tripal_insert_db(array(
     'name' => 'foaf',
     'description' => 'Friend of a Friend. A dictionary of people-related terms that can be used in structured data).',
@@ -77,7 +92,7 @@ function tripal_chado_populate_chado_semweb_table() {
   tripal_insert_cv('sbo','Systems Biology');
 
   //
-  // SET TERM DEFAULTS
+  // ADD TERMS AND SET TERM DEFAULTS
   //
   $name = tripal_insert_cvterm(array(
     'id' => 'schema:name',
@@ -345,4 +360,3 @@ function tripal_chado_semweb_form_submit($form, &$form_state) {
 function tripal_chado_semweb_form_ajax_callback($form, $form_state) {
   return $form;
 }
-    

+ 86 - 47
tripal_ws/includes/tripal_ws.rest.inc

@@ -469,25 +469,23 @@ function tripal_ws_get_content($api_url, &$response, $ws_path, $ctype, $entity_i
     }
   }
 
-  // Get the TripalEntity and attach all the fields.
-  $entity = entity_load('TripalEntity', array('id' => $entity_id));
+  // Get the TripalEntity
+  $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
   $entity = reset($entity);
 
-
   // Next add in the ID and Type for this resources.
   $response['@id'] = $api_url . '/content/' . $ctype . '/' . $entity_id;
   $response['@type'] = $vocab->vocabulary . ':' . $term->accession;
   $response['label'] = $entity->title;
   $response['itemPage'] = url('/bio_data/' . $entity->id, array('absolute' => TRUE));
 
-
   // Get information about the fields attached to this bundle and sort them
   // in the order they were set for the display.
   // TODO: should we allow for custom ordering of fields for web services
   // or use the default display ordering?
-  $fields = field_info_instances('TripalEntity', $bundle->name);
+  $instances = field_info_instances('TripalEntity', $bundle->name);
 
-  uasort($fields, function($a, $b) {
+  uasort($instances, function($a, $b) {
     $a_weight = (is_array($a) && isset($a['display']['default']['weight'])) ? $a['display']['default']['weight'] : 0;
     $b_weight = (is_array($b) && isset($b['display']['default']['weight'])) ? $b['display']['default']['weight'] : 0;
 
@@ -496,30 +494,79 @@ function tripal_ws_get_content($api_url, &$response, $ws_path, $ctype, $entity_i
     }
     return ($a_weight < $b_weight) ? -1 : 1;
   });
-  // Iterate throught the fields and add each value to the response.
+
+  // Iterate through the fields and add each value to the response.
   //$response['fields'] = $fields;
-  foreach ($fields as $field_name => $field) {
-    $field_info = field_info_field($field_name);
-    $settings = $field_info['settings'];
-
-    // By default, the label for the key in the output should be the
-    // term from the vocabulary that the field is assigned. But in the
-    // 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', $settings) and $settings['semantic_web']) {
-      list($vocabulary, $accession) = explode(':', $settings['semantic_web']);
-      $term = tripal_get_term_details($vocabulary, $accession);
-      $key = $term['name'];
-      $context_vocabs[$vocabulary] = $term['url'];
-      $context_terms[$key] = $settings['semantic_web'];
-    }
+  foreach ($instances as $field_name => $instance) {
 
     // Skip hidden fields.
-    if ($field['display']['default']['type'] == 'hidden') {
+    if ($instance['display']['default']['type'] == 'hidden') {
       continue;
     }
 
-    $value = array();
+    // Get the information about this field. It will have settings different
+    // from the instance.
+    $field = field_info_field($field_name);
+
+    // Get the details for this field for the JSON-LD response.
+    $details = tripal_ws_get_field_JSON_LD($field, $instance);
+    print_r($details);
+    $context_vocabs += $details['context']['vocabs'];
+    $context_terms += $details['context']['terms'];
+    $response += $details['value'];
+  }
+
+  // Lastly, add in the terms used into the @context section.
+  $context_terms['label'] = 'rdfs:label';
+  $context_terms['itemPage'] = 'schema:itemPage';
+
+  $response['@context'] = array_merge($context_vocabs, $context_terms);
+
+//   $response['operation'][] = array(
+//     '@type' => 'hydra:DeleteResourceOperation',
+//     'hydra:method' => 'DELETE'
+//   );
+//   $response['operation'][] = array(
+//     '@type' => 'hydra:ReplaceResourceOperation',
+//     'hydra:method' => 'POST'
+//   );
+
+}
+
+/**
+ *
+ */
+function tripal_ws_get_field_JSON_LD($field, $instance) {
+
+  // Initialized the context array.
+  $context = array();
+  $context['vocabs'] = array();
+  $context['terms'] = array();
+
+  // Get the field  settings.
+  $field_name = $instance['field_name'];
+  $field_settings = $field['settings'];
+
+  // By default, the label for the key in the output should be the
+  // term from the vocabulary that the field is assigned. But in the
+  // case that the field is not assigned a term, we must use the field name.
+  $key = $field_name;
+  if (array_key_exists('semantic_web', $field_settings) and $field_settings['semantic_web']) {
+    list($vocabulary, $accession) = explode(':', $field_settings['semantic_web']);
+    $term = tripal_get_term_details($vocabulary, $accession);
+    $key = $term['name'];
+    $context['vocabs'][$vocabulary] = $term['url'];
+    $context['terms'][$key] = $field_settings['semantic_web'];
+  }
+
+  // Don't show fields that are not meant to be auto attached, even if the
+  // loading of the entity pulled a value for the field from the cache.
+  // We want the end-user to specifically load these.
+  $value = array();
+  $instance_settings = $instance['settings'];
+  if (array_key_exists('auto_attach', $instance_settings) and
+      $instance_settings['auto_attach'] === TRUE) {
+
     $items = field_get_items('TripalEntity', $entity, $field_name);
     for ($i = 0; $i < count($items); $i++) {
 
@@ -539,41 +586,33 @@ function tripal_ws_get_content($api_url, &$response, $ws_path, $ctype, $entity_i
 
         if (property_exists($lvocab, 'vocabulary') and !array_key_exists($lvocab->vocabulary, $response['@context'])) {
           $lterm_details = tripal_get_term_details($lvocab->vocabulary, $lterm->vocabulary);
-          $response['@context'][$lvocab->vocabulary] = $lterm_details->url;
+          $context[$lvocab->vocabulary] = $lterm_details->url;
         }
       }
       else {
         $value[] = $items[$i]['value'];
       }
     }
-
-    // Convert a single value to not be an array.
-    if (count($value) == 1) {
-      $value = $value[0];
-    }
-
-    $response[$key] = $value;
+  }
+  // If the value shouldn't be attached by default then create a link for the
+  // caller to retrieve the information.
+  else {
+    $value[] = $api_url . '/content/' . $ctype . '/' . $entity_id;
+  }
+  // Convert a single value to not be an array.
+  if (count($value) == 1) {
+    $value = $value[0];
   }
 
-  // Lastly, add in the terms used into the @context section.
-  $context_terms['label'] = 'rdfs:label';
-  $context_terms['itemPage'] = 'schema:itemPage';
-
-  $response['@context'] = array_merge($context_vocabs, $context_terms);
-
-  $response['operation'][] = array(
-    '@type' => 'hydra:DeleteResourceOperation',
-    'hydra:method' => 'DELETE'
+  return array(
+    'context' => $context,
+    'value' => array(
+      $key => $value
+    ),
   );
-  $response['operation'][] = array(
-    '@type' => 'hydra:ReplaceResourceOperation',
-    'hydra:method' => 'POST'
-  );
-
 }
 
 
-
 /**
  * Provides the Hydra compatible apiDocumentation page that describes this API.
  *