Browse Source

Still working on querying fields with views

Stephen Ficklin 8 years ago
parent
commit
ba4cb29c2b

+ 16 - 0
tripal/includes/TripalFields/TripalField.inc

@@ -277,7 +277,23 @@ class TripalField {
   public function viewsData() {
     $data = array();
 
+    $bundle_name = $this->instance['bundle'];
+    $field_name = $this->field['field_name'];
 
+    $data[$bundle_name][$field_name] = array(
+      'title' => $this->instance['label'],
+      'help' => $this->instance['description'],
+      'field' => array(
+        'handler' => 'tripal_views_handler_field',
+        'click sortable' => TRUE,
+      ),
+      'filter' => array(
+        'handler' => 'views_handler_filter_string',
+      ),
+      'sort' => array(
+        'handler' => 'views_handler_sort',
+      ),
+    );
 
     return $data;
   }

+ 5 - 15
tripal/includes/tripal.fields.inc

@@ -88,28 +88,18 @@ function tripal_field_widget_info_alter(&$info) {
 function tripal_field_views_data($field) {
   $data = array();
 
-  $field_name = $field['field_name'];
   $field_type = $field['type'];
+  $field_types = tripal_get_field_types();
 
   // Iterate through the bundles to which this field is attached and
   // if it is a TripalField field then we'll call the viewsDataAlater function.
   $bundles = $field['bundles']['TripalEntity'];
   foreach ($bundles as $bundle_name) {
     $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
-    $data[$bundle_name][$field_name] = array(
-      'title' => $instance['label'],
-      'help' => $instance['description'],
-      'field' => array(
-        'handler' => 'views_handler_field',
-        'click sortable' => TRUE,
-      ),
-      'filter' => array(
-        'handler' => 'views_handler_filter_string',
-      ),
-      'sort' => array(
-        'handler' => 'views_handler_sort',
-      ),
-    );
+    if (in_array($field_type, $field_types)) {
+      $tfield = new $field_type($field, $instance);
+      $data += $tfield->viewsData();
+    }
   }
   return $data;
 }

+ 1 - 0
tripal/tripal.info

@@ -9,6 +9,7 @@ configure = admin/tripal
 stylesheets[all][] = theme/css/tripal.css
 scripts[]          = theme/js/tripal.js
 
+files[] = views_handlers/tripal_views_handler_field.inc
 files[] = views_handlers/tripal_views_handler_filter_string_selectbox.inc
 files[] = views_handlers/tripal_views_handler_field_entity.inc
 files[] = views_handlers/tripal_views_handler_field_entity_status.inc

+ 20 - 71
tripal/tripal.views.inc

@@ -50,44 +50,30 @@ function tripal_views_data_fields(&$data) {
       continue;
     }
 
-    // Call the hook_field_views_data() hooks.
-    // $module = $field['module'];
-    //$result = (array) module_invoke($module, 'field_views_data', $field);
-
-    // Iterate through the bundles to which this field is attached and
-    // add the fields that are attached to the bundle.
-    $bundles = $field['bundles']['TripalEntity'];
-    $field_name = $field['field_name'];
-    $field_type = $field['type'];
-    foreach ($bundles as $bundle_name) {
-      $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
-      $data[$bundle_name][$field_name] = array(
-        'title' => $instance['label'],
-        'help' => $instance['description'],
-        'field' => array(
-          'handler' => 'views_handler_field',
-          'click sortable' => TRUE,
-        ),
-        'filter' => array(
-          'handler' => 'views_handler_filter_string',
-        ),
-        'sort' => array(
-          'handler' => 'views_handler_sort',
-        ),
-      );
+    // Call the hook_field_views_data() but only for the tripal module.
+    // Otherwise the other modules will expect that this is an SQL-based
+    // view.
+    $result = (array) module_invoke('tripal', 'field_views_data', $field);
+
+    // Set defaults for the field if no data array was returned.
+    if (empty($result)) {
+      // Iterate through the bundles to which this field is attached and
+      // if it is a TripalField field then we'll call the viewsDataAlater function.
+      $bundles = $field['bundles']['TripalEntity'];
+      $result = array();
+      foreach ($bundles as $bundle_name) {
+        $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
+        $tfield = new TripalField($field, $instance);
+        $result += $tfield->viewsData();
+      }
     }
 
-    // Set defaults for the fielf if no data array was returned.
-    //if (empty($result)) {
-      //$result = tripal_views_default_views_data($field);
-    //}
-
     // Call the hook_field_views_data_alter() hook.
-    //drupal_alter('field_views_data', $result, $field, $module);
+    drupal_alter('field_views_data', $result, $field, $module);
 
-    //if (is_array($result)) {
-      //$data = drupal_array_merge_deep($result, $data);
-    //}
+    if (is_array($result)) {
+      $data = drupal_array_merge_deep($result, $data);
+    }
   }
 }
 /**
@@ -111,43 +97,6 @@ function tripal_entity_views_data(&$data) {
        'title' => t($bundle->label),
        'help' => t('Tripal ' . $bundle->label . ' pages'),
      );
-
-//     // Now add the fields to the bundle.
-//     $instances = field_info_instances('TripalEntity', $bundle->name);
-//     foreach ($instances as $instance) {
-
-//       // 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 = 'tripal_views_handler_field_entity_default_formatter';
-//       $filter_handler = 'tripal_views_handler_filter_entity_string';
-//       $sort_handler = 'tripal_views_handler_sort_entity_string';
-//       $click_sortable = TRUE;
-
-//       $field_name = $instance['field_name'];
-
-//       if ($field_name == 'content_type') {
-//         $field_handler = 'views_handler_field';
-//       }
-
-//       $data['tripal_entity'][$field_name] = array(
-//         'group' => $bundle->label,
-//         'title' => $instance['label'],
-//         'help' => t(' Appears in: @bundles.', array('@bundles' => $bundle->label)) . $instance['description'],
-//         'handler' => '',
-//         'field' => array(
-//           'handler' => $field_handler,
-//           'click sortable' => $click_sortable,
-//         ),
-//         'filter' => array(
-//           'handler' => $filter_handler,
-//         ),
-//         'sort' => array(
-//           'handler' => $sort_handler,
-//         ),
-//       );
-//     }
   }
 }
 

+ 82 - 4
tripal/tripal_views_query.inc

@@ -7,6 +7,8 @@ class tripal_views_query extends views_plugin_query {
 
   var $fields;
 
+  var $filters;
+
   /**
    *
    */
@@ -14,15 +16,47 @@ class tripal_views_query extends views_plugin_query {
     parent::init($base_table, $base_field, $options);
 
     $this->fields = array();
+    $this->where = array();
+
+    // Creqte the TripalFieldQuery object.
+    $this->query = new TripalFieldQuery();
 
-    $this->query = new EntityFieldQuery;
+    // Make suer we only query on the entities for this bundle type.
     $this->query->entityCondition('entity_type', 'TripalEntity');
+    $this->query->entityCondition('bundle', $base_table);
   }
   /**
    *
    */
-  public function add_field($table_alias, $field, $alias = '', $params = array()) {
-    $this->fields[] = $field;
+  public function add_field($table_alias, $field_name, $alias = '', $params = array()) {
+    $this->fields[] = array(
+      'table_alias' => $table_alias,
+      'field_name' => $field_name,
+      'alias' => $alias,
+      'params' => $params
+    );
+  }
+
+  /**
+   *
+   * @param $group
+   * @param $field
+   * @param $value
+   * @param $operator
+   */
+  public function add_where($group, $field_name, $value = NULL, $operator = NULL) {
+    // Remove the preceeding period from the $field_name
+    $field_name = preg_replace('/^\./', '', $field_name);
+
+    $this->filters[] = array(
+      'group' => $group,
+      'field_name' => $field_name,
+      'value' => $value,
+      'op' => $operator
+    );
+    if ($value) {
+      $this->query->fieldCondition($field_name, $value, $value, $op);
+    }
   }
 
   /**
@@ -30,6 +64,50 @@ class tripal_views_query extends views_plugin_query {
    * @param  $view
    */
   function execute(&$view) {
-    dpm($view);
+    $query = $this->query;
+
+    $start = microtime(TRUE);
+
+    // Execute the count query
+    $cquery = clone $query;
+    $cquery->count();
+    $num_records = $cquery->execute();
+    $views->total_rows = count($num_records['TripalEntity']);
+
+    $results = $query->execute();
+
+    // Iterate through the entities that were returned and get the field
+    // values that are requested.  Those go in the view->result array.
+    $i = 0;
+    $view->result = array();
+    foreach ($results['TripalEntity'] as $entity_id => $stub) {
+      $entities = array($entity_id => $stub);
+      $view->result[$i] = new stdClass();
+      foreach ($this->fields as $details) {
+        $field_name = $details['field_name'];
+        $field = field_info_field($field_name);
+        module_invoke($field['storage']['module'], 'field_storage_load', 'TripalEntity',
+            $entities, FIELD_LOAD_CURRENT, array($field['id'] => array($entity_id)));
+        $view->result[$i]->$field_name = $entities[$entity_id]->$field_name['und'][0]['value'];
+      }
+      $i++;
+    }
+    $view->execute_time = microtime(TRUE) - $start;
+    $view->current_page = 0;
+    dpm($view->query);
+    dpm($view->result);
+  }
+
+  /**
+   * Generate a query and a countquery from all of the information supplied
+   * to the object.
+   *
+   * @param $get_count
+   *   Provide a countquery if this is true, otherwise provide a normal query.
+   *
+   * @return SelectQuery
+   *   A SelectQuery object.
+   */
+  function query($get_count = FALSE) {
   }
 }

+ 20 - 0
tripal/views_handlers/tripal_views_handler_field.inc

@@ -0,0 +1,20 @@
+<?php
+/**
+ * @file
+ *   Views field handler for basic TripalFields fields.
+ */
+
+/**
+ * Views field handler for basic TripalFields fields.
+ */
+class tripal_views_handler_field extends views_handler_field {
+  function query() {
+    parent::query();
+    // We need to add an alias to our TripalFields so that the
+    // views can find the results.  With SQL it sets the alias for each
+    // field and expects to find that alias in the results array.  Without
+    // setting this alias Views can't find our results from our
+    // tripal_views_query plugin.
+    $this->field_alias = $this->real_field;
+  }
+}

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

@@ -103,6 +103,7 @@ function tripal_chado_bundle_create_fields_base(&$info, $details, $entity_type,
       'locked' => FALSE,
       'storage' => array(
         'type' => 'field_chado_storage',
+        'sql' => array(),
       ),
     );
 
@@ -162,8 +163,6 @@ function tripal_chado_bundle_create_fields_base(&$info, $details, $entity_type,
       $base_info['settings']['text_processing'] = 0;
     }
 
-
-
     $info[$field_name] = $base_info;
   }
 }

+ 1 - 1
tripal_chado/tripal_chado.module

@@ -808,7 +808,7 @@ function tripal_chado_job_describe_args($callback, $args) {
 function tripal_chado_node_delete($node) {
   $nid = $node->nid;
   $sql = "UPDATE chado_entity SET nid = NULL WHERE nid = :nid";
-  db_query($sql, array('nid' => $nid));
+  //db_query($sql, array('nid' => $nid));
 }