Browse Source

Nightly check-in. Starting Views support for TripalEntity entities

Stephen Ficklin 8 years ago
parent
commit
417d637b4f
3 changed files with 112 additions and 0 deletions
  1. 1 0
      tripal/tripal.info
  2. 71 0
      tripal/tripal.views.inc
  3. 40 0
      tripal/tripal_views_query.inc

+ 1 - 0
tripal/tripal.info

@@ -12,6 +12,7 @@ scripts[]          = theme/js/tripal.js
 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
+files[] = tripal_views_query.inc
 
 dependencies[] = views
 dependencies[] = path

+ 71 - 0
tripal/tripal.views.inc

@@ -4,6 +4,23 @@
  * Integrates many of the core database tables with drupal views
  */
 
+/**
+ * Implements of hook_views_plugins().
+ */
+function tripal_views_plugins() {
+  return array(
+    'module' => 'tripal',
+    'query' => array(
+      'tripal_views_query' => array(
+        'title' => t('Tripal Entity Query'),
+        'help' => t('Query that allows you to search with Tripal entities.'),
+        'handler' => 'tripal_views_query',
+        'parent' => 'views_query',
+      ),
+    ),
+  );
+}
+
 /**
  * Describe various Tripal Core systems to Views
  *   for the creation of administrative views.
@@ -15,6 +32,60 @@ function tripal_views_data() {
   // Job Management System
   $data = tripal_views_data_jobs($data);
 
+  $data += tripal_entity_views_data();
+
+  return $data;
+}
+
+/**
+ * Integrates the TripalEntity entities with Drupal Views.
+ */
+function tripal_entity_views_data() {
+  $data = array();
+
+  // Get the list of all of the bundles (entity types) and add them
+  // as "base tables" for views.
+  $bundles = db_select('tripal_bundle', 'tb')
+    ->fields('tb')
+    ->execute();
+
+  // Iterate through the bundles.
+  while ($bundle = $bundles->fetchObject()) {
+
+    // Each bundle gets it's own "table".
+    $data[$bundle->name]['table']['group'] = t('Tripal ' . $bundle->label . ' page');
+    $data[$bundle->name]['table']['base'] = array(
+      'query class' => 'tripal_views_query',
+      'title' => t('Tripal ' . $bundle->label . ' page'),
+      'help' => t('Searches Tripal ' . $bundle->label . ' pages'),
+    );
+
+    // Now add the fields to the bundle.
+    $instances = field_info_instances('TripalEntity', $bundle->name);
+    foreach ($instances as $instance) {
+
+      $field_handler = 'views_handler_field_numeric';
+      $filter_handler = 'views_handler_filter_numeric';
+      $sort_handler = 'views_handler_sort';
+      $click_sortable = TRUE;
+
+      $field_name = $instance['field_name'];
+      $data[$bundle->name][$field_name] = array(
+        'title' => $instance['label'],
+        'help' => $instance['description'],
+        'field' => array(
+          'handler' => $field_handler,
+          'click sortable' => $click_sortable,
+        ),
+        'filter' => array(
+          'handler' => $filter_handler,
+        ),
+        'sort' => array(
+          'handler' => $sort_handler,
+        ),
+      );
+    }
+  }
   return $data;
 }
 

+ 40 - 0
tripal/tripal_views_query.inc

@@ -0,0 +1,40 @@
+<?php
+/**
+ * @file
+ * Views query plugin for Apache Solr Views.
+ * Gets its data not from the database, but from a Solr server.
+ */
+
+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;
+  }
+
+  /**
+   * We don't support construction of SQL queries.
+   * @param $get_count
+   */
+  function query($get_count = FALSE) {
+
+  }
+  /**
+   *
+   * @param  $view
+   */
+  function execute(&$view) {
+    // 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;
+    $view->pager['current_page'] = 0;
+  }
+}