Browse Source

Added 'Link to Node' option for name/uniquename fields -works regardless of whether the field is from the base table

laceysanderson 14 years ago
parent
commit
e05951ab12

+ 5 - 0
tripal_analysis/views/analysis.views.inc

@@ -98,6 +98,7 @@ function retrieve_analysis_views_data() {
 	  	),
 		);
 	}	
+	
 	// Field: name (varchar 255)
  	$data['analysis']['name'] = array(
     'title' => t('Name'),
@@ -116,6 +117,10 @@ function retrieve_analysis_views_data() {
       'handler' => 'views_handler_argument_string',
     ),
   );
+  // if joined to the node table add a "Link to Node" option for the field  
+  if (!$database) {
+    $data['analysis']['name']['field']['handler'] = 'views_handler_field_node_optional';
+  }
 
   // Field: description (text)
  	$data['analysis']['description'] = array(

+ 21 - 0
tripal_core/tripal_core.views.inc

@@ -1,5 +1,26 @@
 <?php
 
+/*************************************************************************
+ * Implements hook_views_handlers()
+ * Purpose: Register all custom handlers with views
+ *   where a handler describes either "the type of field", 
+ *   "how a field should be filtered", "how a field should be sorted"
+ *
+ * @return: An array of handler definitions
+ */
+function tripal_core_views_handlers() {
+ return array(
+   'info' => array(
+     'path' => drupal_get_path('module', 'tripal_core') . '/views/handlers',
+   ),
+   'handlers' => array(
+     'views_handler_field_node_optional' => array(
+       'parent' => 'views_handler_field_node',
+     ),
+   ),
+ );
+}
+    
 /*************************************************************************
  * Implements hook_views_pre_render
  * Purpose: Intercepts the view after the query has been executed

+ 109 - 0
tripal_core/views/handlers/views_handler_field_node_optional.inc

@@ -0,0 +1,109 @@
+<?php
+// $Id: views_handler_field_node.inc,v 1.7.2.2 2010/03/16 23:09:05 merlinofchaos Exp $
+/**
+ * @file
+ * Contains the basic 'node' field handler.
+ */
+
+/**
+ * Field handler to provide simple renderer that allows linking to a node.
+ */
+class views_handler_field_node_optional extends views_handler_field_node {
+
+  function construct() {
+    parent::construct();
+    $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
+    if (module_exists('translation')) {
+      $this->additional_fields['language'] = array('table' => 'node', 'field' => 'language');
+    }
+  }
+  
+  /**
+   * Add chado_* and *_node alias'd left joins to the table
+   */
+  function query() {
+    parent::query();
+    if (!preg_match('/'.$this->options[table].'/', $this->view->base_table)) {
+      // chado_* ======================================================
+      $drupal_chado_table_name = 'chado_'.$this->options[table];
+      
+      // add to table_queue--------------------------------------------
+      $drupal_chado_table_join['table'] = $drupal_chado_table_name;
+      $drupal_chado_table_join['num'] = 1;
+      $drupal_chado_table_join['alias'] = $drupal_chado_table_name;
+      $drupal_chado_table_join['relationship'] = $this->options[table];
+
+      $drupal_chado_table_join['join'] = clone($this->query->table_queue[$this->options[table]]['join']);
+      $drupal_chado_table_join['join']->table = $drupal_chado_table_name;
+      $drupal_chado_table_join['join']->left_table = $this->options[table];
+      
+      $drupal_chado_table_join['join']->definition['table'] = $drupal_chado_table_name;
+      $drupal_chado_table_join['join']->definition['left_table'] = $this->options[table];
+
+      $this->query->table_queue[$drupal_chado_table_name] = $drupal_chado_table_join;
+
+      // add to table--------------------------------------------------
+      $this->query->tables[$this->view->base_table][$drupal_chado_table_name] = array(
+        'count' => 1,
+        'alias' => $drupal_chado_table_name,
+      );
+      
+      // node ============================================================
+      $drupal_node_table_alias = $this->options[table].'_node';
+      
+      // add to table_queue--------------------------------------------
+      $drupal_node_table_join['table'] = 'node';
+      $drupal_node_table_join['num'] = 1;
+      $drupal_node_table_join['alias'] = $drupal_node_table_alias;
+      $drupal_node_table_join['relationship'] = $this->options[table];
+
+      $drupal_node_table_join['join'] = clone($this->query->table_queue['node']['join']);
+      $drupal_node_table_join['join']->left_table = $drupal_chado_table_name;
+      $drupal_node_table_join['join']->definition['left_table'] = $drupal_chado_table_name;
+      
+      $this->query->table_queue[$drupal_node_table_alias] = $drupal_node_table_join;
+
+      // add to table--------------------------------------------------
+      $this->query->tables[$this->view->base_table][$drupal_node_table_alias] = array(
+        'count' => 1,
+        'alias' => $drupal_node_table_alias,
+      );
+      
+      // add field and set alias ===========================
+      $field_alias = $this->options[table].'_node_nid';
+      $this->query->add_field($drupal_node_table_alias, 'nid');
+      $this->add_additional_fields();
+      
+      $this->aliases['nid'] = $field_alias;     
+    } else {
+      $this->aliases['nid'] = 'node_nid';
+    }
+  }
+
+  /**
+   * Render whatever the data is as a link to the node.
+   *
+   * Data should be made XSS safe prior to calling this function.
+   */
+  function render_link($data, $values) {
+    if (!empty($this->options['link_to_node']) && $data !== NULL && $data !== '') {  
+      if (!empty($values->{$this->aliases['nid']})) {
+        $this->options['alter']['make_link'] = TRUE;
+        $this->options['alter']['path'] = "node/" . $values->{$this->aliases['nid']};
+        if (isset($this->aliases['language'])) {
+          $languages = language_list();
+          if (isset($languages[$values->{$this->aliases['language']}])) {
+            $this->options['alter']['language'] = $languages[$values->{$this->aliases['language']}];
+          }
+          else {
+            unset($this->options['alter']['language']);
+          }
+        }
+      } else {
+        $this->options['alter']['make_link'] = FALSE;
+        $this->options['alter']['path'] = "";        
+      }
+    }
+    return $data;
+  }
+}

+ 8 - 0
tripal_feature/views/feature.views.inc

@@ -114,6 +114,10 @@
       'handler' => 'views_handler_argument_string',
     ),
   );
+  // if joined to the node table add a "Link to Node" option for the field  
+  if (!$database) {
+    $data['feature']['name']['field']['handler'] = 'views_handler_field_node_optional';
+  }
 
   // Field: unique name (text)
   $data['feature']['uniquename'] = array(
@@ -133,6 +137,10 @@
       'handler' => 'views_handler_argument_string',
     ),
   );
+  // if joined to the node table add a "Link to Node" option for the field  
+  if (!$database) {
+    $data['feature']['uniquename']['field']['handler'] = 'views_handler_field_node_optional';
+  }
 
   // Field: residues (text)
   $data['feature']['residues'] = array(

+ 8 - 0
tripal_library/views/library.views.inc

@@ -123,6 +123,10 @@ function retrieve_library_views_data() {
 	    'handler' => 'views_handler_argument_string',
 	  ),
 	);
+  // if joined to the node table add a "Link to Node" option for the field  
+  if (!$database) {
+    $data['library']['name']['field']['handler'] = 'views_handler_field_node_optional';
+  }
 	
 	// Field: Unique name (text)
 	$data['library']['uniquename'] = array(
@@ -139,6 +143,10 @@ function retrieve_library_views_data() {
 	    'handler' => 'views_handler_argument_string',
 	  ),
 	);
+  // if joined to the node table add a "Link to Node" option for the field  
+  if (!$database) {
+    $data['library']['uniquename']['field']['handler'] = 'views_handler_field_node_optional';
+  }
 	
 	// Field: Is obsolete (integer 0/1)
 	$data['library']['is_obsolete'] = array(

+ 8 - 0
tripal_organism/views/organism.views.inc

@@ -112,6 +112,10 @@ function retrieve_organism_views_data() {
        'handler' => 'views_handler_argument_string',
      ),
   );
+  // if joined to the node table add a "Link to Node" option for the field  
+  if (!$database) {
+    $data['organism']['abbreviation']['field']['handler'] = 'views_handler_field_node_optional';
+  }
 
   // Field: genus (varchar 255)
   $data['organism']['genus'] = array(
@@ -169,6 +173,10 @@ function retrieve_organism_views_data() {
        'handler' => 'views_handler_argument_string',
      ),
   );
+  // if joined to the node table add a "Link to Node" option for the field  
+  if (!$database) {
+    $data['organism']['common_name']['field']['handler'] = 'views_handler_field_node_optional';
+  }
 
   // Field: Comment (text)
   $data['organism']['comment'] = array(

+ 10 - 2
tripal_stock/views/stock.views.inc

@@ -107,7 +107,11 @@ function retrieve_stock_views_data() {
       'handler' => 'views_handler_argument_string',
     ),
   );
-
+  // if joined to the node table add a "Link to Node" option for the field  
+  if (!$database) {
+    $data['stock']['uniquename']['field']['handler'] = 'views_handler_field_node_optional';
+  }
+  
   //Field: name (varchar 255)
   $data['stock']['name'] = array(
     'title' => t('Name'),
@@ -126,7 +130,11 @@ function retrieve_stock_views_data() {
       'handler' => 'views_handler_argument_string',
     ),
   );
-
+  // if joined to the node table add a "Link to Node" option for the field  
+  if (!$database) {
+    $data['stock']['name']['field']['handler'] = 'views_handler_field_node_optional';
+  }
+  
   //Field: description (varchar 255)
   $data['stock']['description'] = array(
     'title' => t('Description'),