Переглянути джерело

Merge pull request #678 from tripal/649-tv3-add_unpublish_functionality

Add unpublish orphaned entities functionality
Stephen Ficklin 6 роки тому
батько
коміт
e320ae195d

+ 1 - 0
docs/user_guide/content_types.rst

@@ -13,3 +13,4 @@ New in Tripal v3 is the ability to create your own content types and manage thei
    ./content_types/configuring_page_display
    ./content_types/field_loading
    ./content_types/field_permissions
+   ./content_types/cleaning_orphans

BIN
docs/user_guide/content_types/cleaning_orphans.1.png


BIN
docs/user_guide/content_types/cleaning_orphans.2.png


BIN
docs/user_guide/content_types/cleaning_orphans.3.png


+ 15 - 0
docs/user_guide/content_types/cleaning_orphans.rst

@@ -0,0 +1,15 @@
+Handling Orphaned Entities
+==========================
+It is common for site developers to work directly with Chado, especially as they become more familiar with it. And sometimes, they may purposefully or accidentally remove data from Chado that is utilized by published Tripal entities. This results in an entity being "orphaned".  The entity will still have a page on the website but the data about that entity is no longer available.  If this occurs you can easily remove published entities that are "orphaned" by navigating to **Administer > Tripal Content Types** and clicking the **Unpublish Orphaned Content** link.  The following page appears.
+
+.. image:: cleaning_orphans.1.png
+
+Next, select the content type that has missing data in the **Content Type** select box.  For example, suppose an organism was removed:
+
+.. image:: cleaning_orphans.2.png
+
+A list of at most 10 entities that are orphaned are shown. This list is just for convenience and does not show all of the orphaned entities that will be removed. If the content type has no orphaned entities then nothing is shown.  You can submit a job to clean the orphaned entities by clicking the **Unpublish Orphaned Entities** button.  
+
+.. image:: cleaning_orphans.3.png
+
+If you have automatic job execution enabled then the cleanup will occur automatically. Otherwise you should manually execute the job on the command-line using Drush as instructed.

+ 91 - 0
tripal/api/tripal.entities.api.inc

@@ -1649,3 +1649,94 @@ function tripal_update_entity($bundle_name, $values) {
 
 
 }
+
+
+/**
+ * Removes orphaned entities.
+ * 
+ * An orphaned entity can occur if the module that created the entity 
+ * unknowingly lost its underlying record in its data store.  Such a case 
+ * could happen if someone directly removed the record from the data store 
+ * outside of the module's control. This function allows each module
+ * to report if any orphans are missing for a given bundle type.
+ * 
+ * @param integer $bundle_id
+ *   The numeric ID of the bundle.
+ * @param TripalJob $job
+ *   (Optional). If this function is executed via the Tripal Jobs system then
+ *   this argument is provided.
+ */
+function tripal_unpublish_orphans(int $bundle_id, TripalJob $job = NULL) {
+  $bundlec = entity_get_controller('TripalBundle');
+  $ids = $bundlec->deleteOrphans($bundle_id, $job);
+}
+
+/**
+ * A hook for modules to delete details for orphaned entities.
+ * 
+ * This hook is called by the TripalBundleController.  Modules that create
+ * entities should use this hook to clean up entities that are orphaned. The
+ * list of $ids passed should be entities who are already known to
+ * be orphaned. These IDs are found by the TripalBundleController using the
+ * results from the hook_bundle_find_orphans() function. 
+ * 
+ * An implementation of this hook should not try to clean up the entity itself,
+ * but rather it should only clean up its own records used to manage the 
+ * relationship between the entity and the underlying data that the
+ * module provides.
+ * 
+ * An orphaned entity can occur if the module that created the entity 
+ * unknowingly lost its underlying record in its data store.  Such a case 
+ * could happen if someone directly removed the record from the data store 
+ * outside of the module's control. This function allows each module
+ * to report if any orphans are missing for a given bundle type.
+ * 
+ * @param TripalBundle $bundle
+ *   A TripalBundle object for the bundle whose entities are orphaned.
+ * @param array $ids
+ *   A list of entity IDs known to be orphaned.
+ * @param TripalJob $job
+ *   An optional Tripal Job object. This is provided when this function is
+ *   called using the Tripal Jobs system.  Implementors of this hook can
+ *   use the addItemsHandled() function to indicate how many entities were
+ *   cleaned up.
+ * @return integer
+ *   The number of entitites that were cleaned up.
+ */
+function hook_bundle_delete_orphans(TripalBundle $bundle, array $ids, TripalJob $job = NULL) { 
+
+    // See the tripal_chado_bundle_delete_orphans() function for an example.
+    
+}
+
+/**
+ * A hook for modules to report on oprhaned entities.
+ * 
+ * An orphaned entity can occur if the module that created the entity 
+ * unknowingly lost its underlying record in its data store.  Such a case 
+ * could happen if someone directly removed the record from the data store 
+ * outside of the module's control. This function allows each module
+ * to report if any orphans are missing for a given bundle type.
+ * 
+ * @param TripalBunldle $bundle
+ *   A TripalBundle object for the bundle that should be checked for 
+ *   orphaned entities.
+ * @param bool $count
+ *   TRUE if the function should return only the number of orphaned entities.
+ *   FALSE if the function should return the list of orphned entities.
+ * @param integer $offset
+ *   For paging of entities set this to the offset within the total count.
+ * @param integer $limit
+ *   For paging of entities set this to the total number to return.
+ *   
+ * @return array|bool
+ *  If $count == FALSE then an array of all entity IDs that are orphaned. If
+ *  $count == TRUE then a single integer count value is returned.
+ */
+function hook_bundle_find_orphans(TripalBundle $bundle, bool $count = FALSE, 
+  int $offset = 0, int $limit = 10) {
+  
+    // See the tripal_chado_bundle_find_orphans() function for an example.  
+  
+}
+

+ 110 - 0
tripal/includes/TripalBundleController.inc

@@ -117,4 +117,114 @@ class TripalBundleController extends EntityAPIControllerExportable {
       return FALSE;
     }
   }
+  
+  /**
+   * Finds any orphaned entities associated with this bundle.
+   * 
+   * An orphaned entity can occur if the module that created the entity 
+   * unknowingly lost its underlying record in its data store.  Such a case 
+   * could happen if someone directly removed the record from the data store 
+   * outside of the module's control. This function allows each module
+   * to report if any orphans are missing for a given bundle type.
+   * 
+   * @param integer $id
+   *   The ID of the bundle.
+   * @param bool $count
+   *   Set to TRUE to return only a count of orphans.
+   * @param integer $offset
+   *   For paging of entities set this to the offset within the total count.
+   * @param integer $limit
+   *   For paging of entities set this to the total number to return.
+   * 
+   * @return array|integer
+   *  If $count == FALSE then an array of all entity IDs that are orphaned. If
+   *  $count == TRUE then a single integer count value is returned.
+   */
+  public function findOrphans(int $id, bool $count = FALSE, int $offset = 0, int $limit = 0) {
+    
+    // Call the hook for modules to find their orphans
+    $bundle = tripal_load_bundle_entity(['id' => $id]);
+
+    
+    // If a count is desired, we need to sum up the values returned by all.
+    if ($count) {
+      $response = module_invoke_all('bundle_find_orphans', $bundle, TRUE);
+      $sum = 0;
+      foreach ($response as $key => $value) {
+        $sum += $value;
+      }
+      return $sum;
+    }
+    // Otherwise just return the entity ID.s
+    else {
+      $response = module_invoke_all('bundle_find_orphans', $bundle, FALSE, $offset, $limit);
+      return $response;
+    }
+  }
+  
+  
+  /**
+   * Deletes orphaned entities.
+   *
+   * An orphaned entity can occur if the module that created the entity
+   * unknowingly lost its underlying record in its data store.  Such a case
+   * could happen if someone directly removed the record from the data store
+   * outside of the module's control. This function allows each module
+   * to report if any orphans are missing for a given bundle type.
+   *
+   * 
+   * @param integer $id
+   *   The ID of the bundle.
+   * @param TripalJob $job
+   *   An optional Tripal Job object. This is provided when this function is
+   *   called using the Tripal Jobs system.  
+   * @return integer
+   *   The number of entitites that were cleaned up.
+   */
+  public function deleteOrphans(int $id, TripalJob $job = NULL) {
+    
+    $num_deleted = 0;
+    $transaction = db_transaction();
+    try {
+            
+      // Get the list of entities that need cleanup.
+      $eids = $this->findOrphans($id, FALSE, 0, 0);    
+      $num_entities = count($eids);
+  
+      // Initialize the job count.
+      if ($job) {
+        $job->logMessage('Found !num orphaned entities.', ['!num' => $num_entities]);
+        $job->setInterval(1);
+        $job->setTotalItems($num_entities);
+      }    
+      
+      // If there are no entities then just return.
+      if ($num_entities == 0) {
+        return 0;
+      }    
+      
+      // Allow the modules to cleanup their records.
+      $bundle = tripal_load_bundle_entity(['id' => $id]);
+      $response = module_invoke_all('bundle_delete_orphans', $bundle, $eids, $job);
+      
+      // Now remove the entities. 
+      $num_deleted = db_delete('tripal_entity')
+        ->condition('id', $eids, 'IN')
+        ->execute();
+    }
+    catch(Exception $e) {
+      $transaction->rollback();
+      $err_msg =  "Failed to remove orphans: " . $e->getMessage();
+      if ($job) {
+        $job->logMessage($err_msg, [], 'error');
+      }
+      else {
+        drupal_set_message($erro_msg, 'error');
+      }
+      return 0;
+    }
+    
+    return $num_deleted;
+    
+  }
 }

+ 1 - 1
tripal/includes/TripalEntityController.inc

@@ -101,7 +101,7 @@ class TripalEntityController extends EntityAPIController {
 
     return TRUE;
   }
-
+  
   /**
    * Sets the title for an entity.
    *

+ 164 - 0
tripal/includes/tripal.unpublish_orphans.inc

@@ -0,0 +1,164 @@
+<?php
+
+/**
+ * Unpublish orphaned entities form.
+ *
+ * @param $form
+ * @param $form_state
+ */
+function tripal_unpublish_orphans_form($form, &$form_state) {
+
+  // Get the list of bundles.
+  $bundles = [];
+  $query = '
+    SELECT bundle_id, data_table, label
+    FROM {chado_bundle} CB
+      INNER JOIN {tripal_bundle} TB ON TB.id = CB.bundle_id
+    ORDER BY label
+  ';
+  $results = db_select('tripal_bundle', 'tb')
+    ->fields('tb')
+    ->orderBy('label')
+    ->execute();
+  while ($bundle = $results->fetchObject()) {
+    $bundles[$bundle->id] = $bundle->label;
+  }
+  drupal_set_title('Unpublish Orphaned Content');
+
+  $form['description'] = [
+    '#type' => 'markup',
+    '#markup' => t('Sometimes published content can become orphaned. This can
+      occur if someone deletes records directly from the underlying data store
+      yet Tripal is not aware of it.  Here, you can unpublish orphaned content
+      '),
+  ];
+
+  if (empty($bundles)) {
+    $form['message'] = [
+      '#type' => 'markup',
+      '#markup' => t("No orphaned content detected."),
+    ];
+
+    return $form;
+  }
+
+  $form['bundles'] = [
+    '#title' => t('Content type'),
+    '#type' => 'select',
+    '#options' => $bundles,
+    '#empty_option' => t('-- Select a Content Type --'),
+    '#description' => t('Select a Tripal content type to find orphaned content.'),
+    '#ajax' => [
+      'callback' => 'tripal_unpublish_orphans_form_callback',
+      'wrapper' => 'bundle_info_fieldset_wrapper',
+    ],
+  ];
+
+  $form['bundle_info_fieldset'] = [
+    '#type' => 'fieldset',
+    '#title' => 'Search Results',
+    '#states' => [
+      'invisible' => [
+        'select[name="bundles"]' => ['value' => ''],
+      ],
+    ],
+    '#collapsible' => FALSE,
+    '#prefix' => '<div id="bundle_info_fieldset_wrapper">',
+    '#suffix' => '</div>',
+  ];
+
+  $selected_bundle_id = isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : NULL;
+  if ($selected_bundle_id) {
+    $bundlec = entity_get_controller('TripalBundle');
+    $count = $bundlec->findOrphans($selected_bundle_id, TRUE);
+    $name = $bundles[$selected_bundle_id];
+    $form['bundle_info_fieldset']['message'] = [
+      '#type' => 'markup',
+      '#markup' => t('<p><strong>There are ' . $count . ' orphaned entities in the ' . $name . ' bundle.</strong></p>'),
+    ];
+
+    if ($count > 0) {
+      $form['bundle_info_fieldset']['example_table'] = [
+        '#type' => 'markup',
+        '#prefix' => t('The following is a subset of the records that will be unpublished. Only a maximum of 10 records are shown.'),
+        '#markup' => tripal_chado_missing_records_table($bundlec, $selected_bundle_id),
+      ];
+
+      $form['bundle_info_fieldset']['submit'] = [
+        '#type' => 'submit',
+        '#value' => 'Unpublish Orphaned Entities',
+      ];
+    }
+  }
+
+  return $form;
+}
+
+/**
+ * Validate form entries.
+ *
+ * @param array $form
+ * @param array $form_state
+ */
+function tripal_unpublish_orphans_form_validate($form, &$form_state) {
+  $bundle_id = isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : NULL;
+
+   if (empty($bundle_id) || !is_numeric($bundle_id)) {
+     form_set_error('bundles', t('Please select a valid bundle.'));
+   }
+}
+
+/**
+ * Process unpublish form.
+ *
+ * @param array $form
+ * @param array $form_state
+ */
+function tripal_unpublish_orphans_form_submit($form, &$form_state) {
+  global $user;
+
+  $bundle_id = isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : NULL;
+
+  tripal_add_job('Delete Orphaned Entities', 'tripal_chado',
+    'tripal_unpublish_orphans', [$bundle_id], $user->uid, 10, []);
+
+  drupal_set_message('Job submitted');
+}
+
+
+/**
+ * Ajax callback for this form.
+ *
+ * @param array $form
+ *
+ * @return array
+ */
+function tripal_unpublish_orphans_form_callback($form) {
+  return $form['bundle_info_fieldset'];
+}
+
+/**
+ * Create a table populated with examples of records that would get deleted.
+ *
+ * @param TripalBundleController $bundlec
+ * @param int $selected_bundle_id
+ *
+ * @return string
+ */
+function tripal_chado_missing_records_table(TripalBundleController $bundlec, int $selected_bundle_id) {
+  $ids = $bundlec->findOrphans($selected_bundle_id, FALSE, 0, 10);
+  $entities = tripal_load_entity('TripalEntity', $ids);
+
+  return theme('table', [
+    'header' => [
+      'Entity ID',
+      'Title'
+    ],
+    'rows' => array_map(function ($entity) {
+      return [
+        $entity->id,
+        l($entity->title, 'bio_data/' . $entity->id),
+      ];
+    }, $entities),
+  ]);
+}

+ 12 - 0
tripal/tripal.module

@@ -109,6 +109,18 @@ function tripal_menu() {
     'file path' => drupal_get_path('module', 'system'),
   );
 
+  $items['admin/content/bio_data/unpublish'] = [
+    'title' => 'Unpublish Orphaned Content',
+    'description' => t('Unpublish content that has no associated records in the data store.'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => ['tripal_unpublish_orphans_form'],
+    'access arguments' => ['administer tripal'],
+    'file' => 'includes/tripal.unpublish_orphans.inc',
+    'file path' => drupal_get_path('module', 'tripal'),
+    'type' => MENU_LOCAL_ACTION,
+    'weight' => 3
+  ];
+  
   /**
    * Tripal Extensions
    */

+ 5 - 1
tripal_chado/api/tripal_chado.query.api.inc

@@ -1627,7 +1627,11 @@ function chado_select_record_check_value_type(&$op, &$value, $type) {
  * Will use a chado persistent connection if it already exists.
  *
  * @param $sql
- *   The sql statement to execute
+ *   The sql statement to execute. When referencing tables in chado, table names
+ *   should be surrounded by curly brackets (e.g. { and }). If Drupal tables
+ *   need to be included in the query, surround those by sqaure brackets
+ *   (e.g. [ and ]).  This follows Drupal conventions for resolving table names.
+ *   It also supports a multi-chado installation.
  *
  * @param $args
  *   The array of arguments, with the same structure as passed to

+ 60 - 0
tripal_chado/includes/tripal_chado.bundle.inc

@@ -138,3 +138,63 @@ function tripal_chado_bundle_delete($bundle) {
     ->execute();
 }
 
+/**
+ * Implements hook_bundle_find_orphans(). 
+ *
+ */
+function tripal_chado_bundle_find_orphans($bundle, $count = FALSE, $offset = 0, $limit = 10) {
+  $chado_bundle_table = chado_get_bundle_entity_table($bundle);
+  $schema = chado_get_schema($bundle->data_table);
+  $primary_key = $schema['primary key'][0];
+  
+  $select = "CT.entity_id";
+  if ($count) {
+    $select = "count(*) as count";
+  }
+  $qlimit = '';
+  if (!$count and $limit) {
+    $qlimit = "LIMIT $limit OFFSET $offset ";
+  }
+  // Get the count 
+  $query = "
+    SELECT $select  
+    FROM [$chado_bundle_table] CT
+      LEFT JOIN {" . $bundle->data_table . "} BT ON CT.record_id = BT.$primary_key
+    WHERE BT.$primary_key IS NULL
+    $qlimit
+  ";
+
+  $results = chado_query($query);
+  if ($count) {
+   $num_orphans = (int) $results->fetchField();
+   return $num_orphans;
+  }
+  else {
+    $ids = [];
+    while ($entity_id = $results->fetchField()) {
+      $ids[] = $entity_id;
+    }
+    return $ids;
+  }
+}
+
+/**
+ * Implements hook_bundle_delete_orphans(). 
+ */
+function tripal_chado_bundle_delete_orphans(TripalBundle $bundle, array $ids, TripalJob $job = NULL) {
+  
+  $chado_bundle_table = chado_get_bundle_entity_table($bundle);
+  $schema = chado_get_schema($bundle->data_table);
+  $primary_key = $schema['primary key'][0];
+  
+  $num_deleted = db_delete($chado_bundle_table)
+    ->condition('entity_id', $ids, 'IN')
+    ->execute();
+  
+  if ($job) {
+    $job->addItemsHandled($num_deleted);
+    $job->logMessage("Removed !num orphaned entities", ['!num' => $num_deleted]);
+  }    
+   
+  return $num_deleted;
+}

+ 67 - 62
tripal_chado/includes/tripal_chado.entity.inc

@@ -1,6 +1,5 @@
 <?php
 
-
 /**
  * Implements hook_entity_create().
  *
@@ -20,7 +19,7 @@ function tripal_chado_entity_create(&$entity, $type, $bundle = NULL) {
 
     // Set some defaults on vars needed by this module.
     if (!property_exists($entity, 'chado_table')) {
-      $entity->chado_table =  NULL;
+      $entity->chado_table = NULL;
       $entity->chado_column = NULL;
       $entity->chado_linker = NULL;
       $entity->chado_type_id = NULL;
@@ -29,7 +28,7 @@ function tripal_chado_entity_create(&$entity, $type, $bundle = NULL) {
 
       // Add in the Chado table information for this entity type.
       if (!$bundle) {
-        $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
+        $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
       }
       if ($bundle->data_table) {
         $entity->chado_table = $bundle->data_table;
@@ -46,6 +45,7 @@ function tripal_chado_entity_create(&$entity, $type, $bundle = NULL) {
     }
   }
 }
+
 /**
  * Implements hook_entity_presave().
  */
@@ -97,7 +97,7 @@ function tripal_chado_entity_load($entities, $type) {
         $entity->chado_record_id = NULL;
 
         // Add in the Chado table information for this entity type.
-        $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
+        $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
         if (!$bundle) {
           continue;
         }
@@ -112,7 +112,8 @@ function tripal_chado_entity_load($entities, $type) {
           ->fetchObject();
         if ($chado_entity) {
           $schema = chado_get_schema($entity->chado_table);
-          $record = chado_generate_var($entity->chado_table, array($schema['primary key'][0] => $chado_entity->record_id));
+          $record = chado_generate_var($entity->chado_table,
+            [$schema['primary key'][0] => $chado_entity->record_id]);
           $entity->chado_record = $record;
           $entity->chado_record_id = $chado_entity->record_id;
         }
@@ -157,107 +158,107 @@ function tripal_chado_entity_delete($entity, $type) {
  * Overrides the default titles.
  */
 function tripal_chado_tripal_default_title_format($bundle, $available_tokens) {
-  $format = array();
+  $format = [];
 
   $table = $bundle->data_table;
 
   if ($table == 'organism') {
     if (chado_get_version() <= '1.2') {
-      $format[] = array(
+      $format[] = [
         'format' => '[taxrank__genus] [taxrank__species]',
-        'weight' => -5
-      );
+        'weight' => -5,
+      ];
     }
     else {
-      $format[] = array(
+      $format[] = [
         'format' => '[taxrank__genus] [taxrank__species] [taxrank__infraspecific_taxon,TAXRANK:0000045]',
-        'weight' => -5
-      );
+        'weight' => -5,
+      ];
     }
   }
   if ($table == 'arraydesign') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
-      'weight' => -5
-    );
+      'weight' => -5,
+    ];
   }
   if ($table == 'assay') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
-      'weight' => -5
-    );
+      'weight' => -5,
+    ];
   }
   if ($table == 'biomaterial') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
-      'weight' => -5
-    );
+      'weight' => -5,
+    ];
   }
   if ($table == 'analysis') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
-      'weight' => -5
-    );
+      'weight' => -5,
+    ];
   }
   if ($table == 'feature') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
-      'weight' => -5
-    );
+      'weight' => -5,
+    ];
   }
   if ($table == 'featuremap') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
-      'weight' => -5
-    );
+      'weight' => -5,
+    ];
   }
   if ($table == 'stock') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
-      'weight' => -5
-    );
+      'weight' => -5,
+    ];
   }
   if ($table == 'pub') {
-    $format[] = array(
+    $format[] = [
       'format' => '[tpub__title]',
       'weight' => -5,
-    );
+    ];
   }
   if ($table == 'cvterm') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
       'weight' => -5,
-    );
+    ];
   }
   if ($table == 'project') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
       'weight' => -5,
-    );
+    ];
   }
   if ($table == 'contact') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
       'weight' => -5,
-    );
+    ];
   }
   if ($table == 'phylotree') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
       'weight' => -5,
-    );
+    ];
   }
   if ($table == 'library') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
       'weight' => -5,
-    );
+    ];
   }
   if ($table == 'protocol') {
-    $format[] = array(
+    $format[] = [
       'format' => '[schema__name]',
       'weight' => -5,
-    );
+    ];
   }
   return $format;
 }
@@ -270,36 +271,37 @@ function tripal_chado_entity_view($entity, $type, $view_mode, $langcode) {
   // If this entity is a TripalEntity and is a full view, then
   // we want to support the legacy view, but only if the legacy
   // module is enabled (the functions exist).
-  if ($type =='TripalEntity') {
+  if ($type == 'TripalEntity') {
     // Use the generic template to render the fields
     if ($view_mode == 'full') {
 
       // Get the Chado table for this data type.
-      $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
+      $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
       $chado_table = $bundle->data_table;
       $chado_field = $bundle->type_column;
 
       // Get the list of templates that should be used for entities and generatte
       // the key in the array for this entity type (using the chado table the
       // entity maps to).
-      $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
+      $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates',
+        []);
       $legacy_template = 'legacy_template--chado_' . $chado_table;
 
       // If the site admin has indicated that this entity type should use
       // a legacy tmplate then prepare the entity and content to fake a
       // node.
-      if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
+      if (key_exists($legacy_template,
+          $enabled_templates) && $enabled_templates[$legacy_template]) {
         // Remove the fields added by the chado_field_storage.
         $fields = field_info_fields();
-        foreach($fields as $field) {
-          if ($field['storage']['type'] == 'field_chado_storage' or
-              $field['storage']['type'] == 'tripal_no_storage') {
-                $field_name = $field['field_name'];
-                if (property_exists($entity, $field_name)) {
-                  $entity->$field_name = NULL;
-                  unset($entity->content[$field_name]);
-                }
-              }
+        foreach ($fields as $field) {
+          if ($field['storage']['type'] == 'field_chado_storage' or $field['storage']['type'] == 'tripal_no_storage') {
+            $field_name = $field['field_name'];
+            if (property_exists($entity, $field_name)) {
+              $entity->$field_name = NULL;
+              unset($entity->content[$field_name]);
+            }
+          }
         }
 
         // Make the entity look like a node.
@@ -341,10 +343,12 @@ function tripal_chado_entity_view_alter(&$build) {
   // For the legacy support, we need to make sure the TOC
   // is built.
   if ($build['#entity_type'] == 'TripalEntity') {
-    $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
+    $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates',
+      []);
     $entity = $build['#entity'];
     $legacy_template = 'legacy_template--' . $entity->type;
-    if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
+    if (key_exists($legacy_template,
+        $enabled_templates) && $enabled_templates[$legacy_template]) {
       $build['#entity']->nid = NULL;
       $build['#node'] = $build['#entity'];
       $modules = module_list();
@@ -357,3 +361,4 @@ function tripal_chado_entity_view_alter(&$build) {
     }
   }
 }
+