Browse Source

Working on Views integration of fields/entities

Stephen Ficklin 8 years ago
parent
commit
c4f37fafee

+ 11 - 2
tripal/tripal.views.inc

@@ -64,12 +64,21 @@ function tripal_entity_views_data() {
     $instances = field_info_instances('TripalEntity', $bundle->name);
     foreach ($instances as $instance) {
 
-      $field_handler = 'views_handler_field_numeric';
-      $filter_handler = 'views_handler_filter_numeric';
+      // TODO: how to determine which handler to use for each field? Perhaps
+      // fields should set their type and here we use that type to determine
+      // which handler to use. If not handler is specified then we use
+      // a default string handler.
+      $field_handler = 'views_handler_field';
+      $filter_handler = 'views_handler_filter';
       $sort_handler = 'views_handler_sort';
       $click_sortable = TRUE;
 
       $field_name = $instance['field_name'];
+
+      if ($field_name == 'feature__organism_id') {
+        $field_handler = 'chado_views_handler_field_chado_base__organism_id';
+      }
+
       $data[$bundle->name][$field_name] = array(
         'title' => $instance['label'],
         'help' => $instance['description'],

+ 33 - 7
tripal/tripal_views_query.inc

@@ -8,7 +8,6 @@
 class tripal_views_query extends views_plugin_query {
 
   public function add_field($table_alias, $field, $alias = '', $params = array()) {
-    dpm($field);
     // Make sure an alias is assigned.
     $alias = $alias ? $alias : $field;
     return $alias;
@@ -29,12 +28,39 @@ class tripal_views_query extends views_plugin_query {
     // The base table indicates our content type.
     $base_table = $view->base_table;
 
-    dpm($view);
-    $row = new stdClass;
-    $row->content_type = 10;
-    $row->organism__comment = 1000;
-    $view->result[] = $row;
-    $view->total_rows = 1;
+    // Get the bundle that the view base table represents.
+    $bundle = tripal_load_bundle_entity(array('name' => $view->base_table));
+
+    // The base table for the view is a bundle therefore the first condition
+    // must be with the content_type field.
+    $query = new TripalFieldQuery();
+    $query->fieldCondition('content_type', $bundle->id);
+
+    $results = $query->execute();
+    foreach ($results['TripalEntity'] as $entity_id => $stub) {
+      // Begin a new row for Views output.
+      $row = new stdClass;
+
+      // Get the entity object.
+      $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
+      $entity = reset($entity);
+
+      // Iterate through the fields that are added to the view and attach those
+      // to the entity.  After attaching we can get the value and include
+      // it in the output results.
+      foreach($view->field as $field_name => $handler) {
+        $field = field_info_field($field_name);
+        field_attach_load($entity->type, array($entity->id => $entity), FIELD_LOAD_CURRENT,
+            array('field_id' => $field['id']));
+        $items = field_get_items('TripalEntity', $entity, $field_name);
+        $row->$field_name = $items[0]['value'];
+      }
+
+      // Add the row to the results list.
+      $view->result[] = $row;
+    }
+
+    $view->total_rows = count($rows);
     $view->pager['current_page'] = 0;
   }
 }

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

@@ -432,7 +432,7 @@ function tripal_chado_field_storage_tquery($conditions) {
     // Set the value for this field search.
     $value = NULL;
     $subfields = explode('.', $filter);
-    print_r($subfields);
+    //print_r($subfields);
     if (count($subfields) > 1) {
       // Get the term for this field's column and replace the field_name with
       // the term.  We need to do this for the recursive function to work.
@@ -491,8 +491,8 @@ function tripal_chado_field_storage_tquery($conditions) {
   // Iterate through the filters and perform the query
   $entity_ids = array();
   foreach ($filters as $chado_table => $values) {
-    print_r($chado_table);
-    print_r($values);
+    //print_r($chado_table);
+    //print_r($values);
     // First get the matching record IDs from the Chado table.
     $schema = chado_get_schema($chado_table);
     $pkey = $schema['primary key'][0];

+ 2 - 2
tripal_chado/includes/tripal_chado.setup.inc

@@ -1692,8 +1692,8 @@ function tripal_cv_add_obo_defaults() {
   $ontologies = array(
     // array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
     // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
-    array('Sequence Ontology', 'https://raw.githubusercontent.com/The-Sequence-Ontology/SO-Ontologies/master/so-xp-simple.obo'),
-    array('Gene Ontology', 'http://geneontology.org/ontology/go.obo'),
+    array('Sequence Ontology', 'http://purl.obolibrary.org/obo/so.obo'),
+    array('Gene Ontology', 'http://purl.obolibrary.org/obo/go.obo'),
     //    array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
     //    array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
     //    array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')

+ 4 - 3
tripal_chado/tripal_chado.info

@@ -5,9 +5,10 @@ project = tripal
 package = Tripal
 version = 7.x-3.0-alpha1
 
-files[] = includes/chado_views_handler_field.inc
-files[] = includes/chado_views_handler_filter.inc
-files[] = includes/chado_views_handler_sort.inc
+files[] = views_handlers/chado_views_handler_field.inc
+files[] = views_handlers/chado_views_handler_filter.inc
+files[] = views_handlers/chado_views_handler_sort.inc
+files[] = views_handlers/chado_views_handler_field_chado_base__organism_id.inc
 
 stylesheets[all][] = theme/css/tripal_chado.css
 

+ 0 - 0
tripal_chado/includes/chado_views_handler_field.inc → tripal_chado/views_handlers/chado_views_handler_field.inc


+ 9 - 0
tripal_chado/views_handlers/chado_views_handler_field_chado_base__organism_id.inc

@@ -0,0 +1,9 @@
+<?php
+class chado_views_handler_field_chado_base__organism_id extends views_handler_field {
+
+  function render($values) {
+    $value = $this->get_value($values);
+
+    return $value['label'];
+  }
+}

+ 0 - 0
tripal_chado/includes/chado_views_handler_filter.inc → tripal_chado/views_handlers/chado_views_handler_filter.inc


+ 0 - 0
tripal_chado/includes/chado_views_handler_sort.inc → tripal_chado/views_handlers/chado_views_handler_sort.inc