Преглед изворни кода

Working on fix for issue #467. Fixed issue with tripal_views_data_alter() setting up handlers for fields that don't connect to the Tripal Storage API

Stephen Ficklin пре 6 година
родитељ
комит
efd7e3b988

+ 3 - 1
tripal/includes/tripal.field_storage.inc

@@ -14,7 +14,9 @@ function tripal_field_storage_info() {
       'label' => t('Tripal'),
       'description' => t('The NULL storage is a placeholder for field values
           that are not stored in any storage backend (e.g. entity types).'),
-      'settings' => array(),
+      'settings' => array(
+        'tripal_storage_api' => TRUE,
+      ),
     ),
   );
 }

+ 9 - 0
tripal/includes/tripal.fields.inc

@@ -88,6 +88,15 @@ function tripal_field_widget_info_alter(&$info) {
 function tripal_field_views_data($field) {
   $data = array();
 
+  // Skip fields that aren't attached to TripalEntity entities.
+  if (!array_key_exists('TripalEntity', $field['bundles'])) {
+    return $data;
+  }
+
+  if (!array_key_exists('tripal_storage_api', $field['storage']['settings'])) {
+    return $data;
+  }
+
   $field_type = $field['type'];
   $field_types = tripal_get_field_types();
 

+ 29 - 0
tripal/tripal.install

@@ -1180,3 +1180,32 @@ function tripal_update_7311() {
     throw new DrupalUpdateException('Could not perform update: '. $error);
   }
 }
+
+/**
+ * Adds a tripal_storage_api setting to all field storage details
+ */
+function tripal_update_7312() {
+  $transaction = db_transaction();
+  try {
+    $fields = db_select('field_config', 'fc')
+      ->fields('fc', ['id', 'storage_type', 'data'])
+      ->where("fc.storage_type = 'field_chado_storage' or fc.storage_type = 'tripal_no_storage'")
+      ->execute();
+    while ($field = $fields->fetchObject()) {
+      $data = unserialize($field->data); 
+      $data['storage']['settings']['tripal_storage_api'] = TRUE;
+      db_update('field_config')
+        ->fields([
+          'data' => serialize($data)
+        ])
+        ->condition('id', $field->id)
+        ->execute();
+    }
+  }
+  catch (\PDOException $e) {
+    $transaction->rollback();
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Could not perform update: '. $error);
+  }
+}
+

+ 64 - 36
tripal/tripal.views.inc

@@ -51,42 +51,65 @@ function tripal_views_data() {
  */
 function tripal_views_data_alter(&$data) {
 
-  $fields = field_info_fields();
-  $tripal_fields = tripal_get_field_types();
-
-  // Iterate through all of the fields.
-  foreach ($fields as $field) {
-
-    // Skip fields that aren't attached to TripalEntity entities.
-    if (!array_key_exists('TripalEntity', $field['bundles'])) {
-      continue;
+  foreach ($data as $data_table => $definition) {
+    if ($data_table == 'field_data_field_pub_tags') {
+      dpm($definition); 
     }
-
-    // Iterate through the bundles to which this field is attached and
-    // if it is a TripalField field then we'll call the viewsData function.
-    $bundles = $field['bundles']['TripalEntity'];
-    $result = array();
-    foreach ($bundles as $bundle_name) {
-      $field_name = $field['field_name'];
-
-      // Skip fields that aren't setup for views with this bundle.
-      // Fields should be associated with the bundle's term identifier
-      // (i.e. [vocab]__[accession].
-      $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
-      $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
-      $bundle_term_id = $term->vocab->vocabulary . '__' . $term->accession;
-      if (!array_key_exists($bundle_term_id, $data)) {
-        continue;
-      }
-
-      // Skip fields implemented using a TripalField class. These fields
-      // should already have the proper handlers because the class sets
-      // them up automatically.
-      if (in_array($field_name, $tripal_fields)) {
-        continue;
-      }
-
-      $data[$bundle_term_id][$field_name]['sort']['handler'] = 'tripal_views_handler_sort';
+    foreach ($definition as $data_column => $element) {
+      if (is_array($element) and array_key_exists('field', $element) and 
+          is_array($element['field']) and array_key_exists('field_name', $element['field'])) {
+        $field_name = $element['field']['field_name'];
+        $field = field_info_field($field_name);
+
+        // Skip fields that aren't attached to a TripalEntity content type.
+        if (!array_key_exists('TripalEntity', $field['bundles'])) {
+          continue;
+        }
+
+        // Skip fields that use the Tripal Storage API.
+        if (array_key_exists('tripal_storage_api', $field['storage']['settings'])) {
+          continue;
+        }
+
+        //  
+        // Now update views for integrating other data with our Tripal Entities.
+        //
+    
+        // Iterate through the bundles to which this field is attached and
+        // if it is a TripalField field then we'll call the viewsData function.
+        $bundles = $field['bundles']['TripalEntity'];
+        $result = array();
+        foreach ($bundles as $bundle_name) {
+
+          // Skip fields that aren't setup for views with this bundle.
+          // Fields should be associated with the bundle's term identifier
+          // (i.e. [vocab]__[accession].
+          $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
+          $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+          $bundle_term_id = $term->vocab->vocabulary . '__' . $term->accession;
+          if (!array_key_exists($bundle_term_id, $data)) {
+            continue;
+          }
+
+          // Support the taxonomy_term_reference field when it's added to a 
+          // Tripal content type
+          if ($field['type'] == 'taxonomy_term_reference') { 
+            //dpm($definition);
+            $data[$bundle_term_id][$data_table] = [
+              'title' => t('Tagged Categories'), 
+              'help' => t('Relates this Tripal content type to categories that have been assigned to it using Drupal\'s Taxonomy system.'),
+              'relationship' => array(
+                 'base' => 'tripal_entity',
+                 'base field' => 'id',
+                 'relationship field' => 'entity_id',
+                 'handler' => 'views_handler_relationship',
+                 'label' => t('Tags'),
+              ),
+            ];
+            //dpm($data[$bundle_term_id]);
+          } 
+        }
+      } 
     }
   }
 }
@@ -104,6 +127,11 @@ function tripal_views_data_fields(&$data) {
       continue;
     }
 
+    // Fields that don't connect to the Tripal Storage API should be added differently
+    if (!array_key_exists('tripal_storage_api', $field['storage']['settings'])) {
+      continue;
+    }
+
     // TODO: do we really need this hook? Just substitute the code here
     // for what that hook does... it's only called in one plac.e
     // Call the hook_field_views_data() but only for the tripal module.
@@ -482,4 +510,4 @@ function tripal_views_data_jobs(&$data) {
       'handler' => 'views_handler_sort',
     ),
   );
-}
+}

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

@@ -8,7 +8,9 @@ function tripal_chado_field_storage_info() {
     'field_chado_storage' => array(
       'label' => t('Chado'),
       'description' => t('Stores fields in the local Chado database.'),
-      'settings' => array(),
+      'settings' => array(
+        'tripal_storage_api' => TRUE,
+      ),
       // The logo_url key is supported by Tripal. It's not a Drupal key. It's
       // used for adding a logo or picture for the data store to help make it
       // more easily recognized on the  field_ui_field_overview_form. Ideally