Browse Source

Update hook_views_data_alter() to fix issue #789

I think there are still some adjustments to be made: some $data[<table>]['table']['join'] may not be needed and I did not succeed to add the relationship from a Tripal entity  to a Chado object. However, from Chado to Tripal entity, it is working. At least, it's better than current code that does nothing!
Valentin Guignon 6 years ago
parent
commit
1ca2a4682e
1 changed files with 88 additions and 75 deletions
  1. 88 75
      tripal_chado_views/tripal_chado_views.views.inc

+ 88 - 75
tripal_chado_views/tripal_chado_views.views.inc

@@ -600,84 +600,97 @@ function tripal_chado_views_views_data_tripal_views_tables($data) {
  */
 function tripal_chado_views_views_data_alter(&$data) {
 
-    // ADD IN NODE JOINS & RELATIONSHIPS
-    // D7 @todo: Create custom handler to allow join from Node => Base (ie: organism)
-    //           with the addition of a single relationship
-    // D7 @todo: Create custom handler to allow join from Base (ie: organism)
-    //           with the addition of a single relationship
-    // D7 @todo: Add support for Mview <-> Node joins and relationships
-    $tvi_query = db_query('SELECT * FROM {tripal_views} WHERE base_table=1');
-    foreach ($tvi_query as $tvi_row) {
-
-      //ids we'll use for queries
-      $setup_id = $tvi_row->setup_id;
-      $base_table = $tvi_row->table_name;
-      $linker_table = 'chado_' . $base_table;
-      $base_title = ucwords(str_replace('_', ' ', $base_table));
-
-      // add in joins to the node tables if the Chado schema is local
-      $is_local = isset($GLOBALS["chado_is_local"]) && $GLOBALS["chado_is_local"];
-      if ($is_local) {
-        // if a node linking table exists then add in the joins
-        if (db_table_exists($linker_table)) {
-
-          // Adds content (node) fields to chado base table field lists automatically
-          $data['node']['table']['join'][$linker_table] = array(
-            'left_field' => 'nid',
-            'field' => 'nid',
-          );
-          $data[$linker_table]['table']['join'][$base_table] = array(
-            'left_field' => $base_table . '_id',
-            'field' => $base_table . '_id',
-          );
-          $data['node']['table']['join'][$base_table] = array(
-            'left_table' => $linker_table,
-            'left_field' => 'nid',
-            'field' => 'nid',
-          );
-
-          // Adds in a chado base table => node relationship
-          // This allows controlled joining to multiple nodes per line
-          // Use Case:  link to feature and organism nodes on a feature listing
-          // D7 todo: a custom relationship handler to get from feature.organism_id => organism node
-          //      without 1st needing to add relationship to organism table
-          $base_field = $base_table . '_id';
-          $data[$linker_table][$base_field] = array(
-            'group' => $base_title,
-            'title' => $base_title . 'Node',
-            'help' => t("Links @base_title to it's node.", array('@base_title' => $base_title)),
-            'relationship' => array(
-              'handler' => 'views_handler_relationship',
-              'title' => t("@base_title => Node", array('@base_title' => $base_title)),
-              'label' => t("@base_title => Node", array('@base_title' => $base_title)),
-              'real field' => 'nid',
-              'base' => 'node',
-              'base field' => 'nid'
-            ),
-          );
-
-          // Add Chado fields to a node-based view
-          // This will only be done with relationships
-          $base_field = $base_table . '_id';
-          $data['node'][$base_field] = array(
-            'group' => $base_title,
-            'title' => $base_title,
-            'help' => t("Links node to chado @base_title.", array('@base_title' => $base_title)),
-            'relationship' => array(
-              'handler' => 'views_handler_relationship',
-              'title' => t("Node => @base_title", array('@base_title' => $base_title)),
-              'label' => t("Node => @base_title", array('@base_title' => $base_title)),
-              'real field' => 'nid',
-              'base' => $linker_table,
-              'base field' => 'nid'
-            ),
-          );
-
-        }
+  // ADD IN ENTITIES JOINS & RELATIONSHIPS
+  // D7 @todo: Create custom handler to allow join from Entity => Base (ie: organism)
+  //           with the addition of a single relationship
+  // D7 @todo: Create custom handler to allow join from Base (ie: organism)
+  //           with the addition of a single relationship
+  // D7 @todo: Add support for Mview <-> Entity joins and relationships
+  $bundles = db_select('chado_bundle', 'CB')
+      ->fields('CB', array('bundle_id', 'data_table', 'type_column', 'type_id'))
+      ->execute();
+  // Look for the record ID in the appropriate chado table.
+  foreach ($bundles as $bundle) {
+    // Ids we'll use for queries.
+    $base_table = $bundle->data_table;
+    $linker_table = 'chado_bio_data_' . $bundle->bundle_id;
+    $base_title = ucwords(str_replace('_', ' ', $base_table));
+    $bundle_name = 'bio_data_' . $bundle->bundle_id;
+  
+    // Add in joins to the tripal_entity tables if the Chado schema is local.
+    $is_local = isset($GLOBALS["chado_is_local"]) && $GLOBALS["chado_is_local"];
+    if ($is_local) {
+      if (db_table_exists($linker_table)) {
+        // Adds tripal_entity fields to chado base table field lists automatically.
+        $data['tripal_entity']['table']['join'][$linker_table] = array(
+          'left_field' => 'entity_id',
+          'field' => 'id',
+        );
+        $data['tripal_entity']['table']['join'][$base_table] = array(
+          'left_table' => $linker_table,
+          'left_field' => 'entity_id',
+          'field' => 'id',
+        );
+        $data[$linker_table]['table']['join'][$base_table] = array(
+          'left_field' => $base_table . '_id',
+          'field' => 'record_id',
+        );
+        $data[$linker_table]['table']['join']['tripal_entity'] = array(
+          'left_field' => 'id',
+          'field' => 'entity_id',
+        );
+        $data[$base_table]['table']['join'][$linker_table] = array(
+          'left_field' => 'record_id',
+          'field' => $base_table . '_id',
+        );
+        $data[$base_table]['table']['join']['tripal_entity'] = array(
+          'left_table' => $linker_table,
+          'left_field' => 'record_id',
+          'field' => $base_table . '_id',
+        );
+  
+        // Adds in a chado base table => entity relationship.
+        // This allows controlled joining to multiple entities per line.
+        // Use Case:  link to feature and organism entities on a feature listing.
+        // D7 todo: a custom relationship handler to get from feature.organism_id => organism node
+        //      without 1st needing to add relationship to organism table.
+        $base_field = $base_table . '_id';
+        $data[$linker_table][$base_field] = array(
+          'group' => $base_title,
+          'title' => $base_title . 'Entity',
+          'help' => t("Links @base_title to it's entity.", array('@base_title' => $base_title)),
+          'relationship' => array(
+            'handler' => 'views_handler_relationship',
+            'title' => t("@base_title => Entity", array('@base_title' => $base_title)),
+            'label' => t("@base_title => Entity", array('@base_title' => $base_title)),
+            'real field' => 'entity_id',
+            'base' => 'tripal_entity',
+            'base field' => 'id'
+          ),
+        );
+  
+        // Add Chado fields to a entity-based view
+        // This will only be done with relationships.
+        $base_field = $base_table . '_id';
+        $data['tripal_entity'][$base_field] = array(
+          'group' => $base_title,
+          'title' => $base_title,
+          'help' => t("Links entity to chado @base_title.", array('@base_title' => $base_title)),
+          'relationship' => array(
+            'handler' => 'views_handler_relationship',
+            'title' => t("Entity => @base_title", array('@base_title' => $base_title)),
+            'label' => t("Entity => @base_title", array('@base_title' => $base_title)),
+            'real field' => 'id',
+            'base' => $linker_table,
+            'base field' => 'entity_id'
+          ),
+        );
+  
       }
     }
+  }
 
-    return $data;
+  return $data;
 }
 
 /**