Selaa lähdekoodia

The results of a day of debugging an error that is no longer repeatable? Anyway, I fixed TripalEntityController::delete to match the parent version of this function so it wasn't all for naught.

Lacey Sanderson 9 vuotta sitten
vanhempi
commit
bda778a5ea

+ 30 - 10
tripal_entities/includes/TripalEntityController.inc

@@ -50,20 +50,39 @@ class TripalEntityController extends EntityAPIController {
   }
 
   /**
-   * Delete a single entity.
+   * Overrides EntityAPIController::delete().
    *
-   * Really a convenience function for deleteMultiple().
+   * @param array $ids
+   *    An array of the ids denoting which entities to delete.
+   * @param DatabaseTransaction $transaction
+   *    Optionally a DatabaseTransaction object to use. Allows overrides to pass in 
+   *    their transaction object.
    */
-  public function delete($entity) {
-    $transaction = db_transaction();
+  public function delete($ids, $transaction = NULL) {
+
+    if (!$transaction) {
+      $transaction = db_transaction();
+    }
+    
     try {
-      // Invoke hook_entity_delete().
-      module_invoke_all('entity_delete', $entity, $entity->type);
-      field_attach_delete('TripalEntity', $entity);
+      
+      // First load the entity.
+      $entities = entity_load('TripalEntity', $ids);
+      
+      // Then properly delete each one.
+      foreach ($entities as $entity) {
+      
+        // Invoke hook_entity_delete().
+        module_invoke_all('entity_delete', $entity, $entity->type);
+        
+        // Delete any field data for this entity.
+        field_attach_delete('TripalEntity', $entity);
 
-      db_delete('tripal_entity')
-        ->condition('id', $entity->id)
-        ->execute();
+        // Finally delete the entity record from our base table.
+        db_delete('tripal_entity')
+          ->condition('id', $entity->id)
+          ->execute();
+      }
     }
     catch (Exception $e) {
       $transaction->rollback();
@@ -71,6 +90,7 @@ class TripalEntityController extends EntityAPIController {
       throw $e;
       return FALSE;
     }
+
     return TRUE;
   }
 

+ 5 - 3
tripal_entities/includes/TripalEntityUIController.inc

@@ -410,11 +410,13 @@ function tripal_entities_view_entity($entity, $view_mode = 'full') {
    $entity = $form_state['entity'];
 
    $entity_controller = new TripalEntityController($entity->type);
-   if ($entity_controller->delete($entity)) {
+
+   if ($entity_controller->delete(array($entity->id))) {
      drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
-     $form_state['redirect'] = 'admin/content/tripal_entitys';
+     $form_state['redirect'] = 'admin/content/bio-data';
    }
    else {
      drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
    }
- }
+ }
+ 

+ 37 - 1
tripal_entities/tripal_entities.install

@@ -16,6 +16,43 @@ function tripal_entities_install() {
 
 }
 
+/**
+ * Implements hook_uninstall().
+ */
+function tripal_entities_uninstall() {
+
+/*
+  // So somehow I was able to uninstall this module without deleting the bundles. This
+  // caused aweful errors because fields weren't deleted so when I re-installed, the code
+  // tried to create fields that were inactive (despite checking if the field exists 
+  // before creating). The following code was meant to ensure that all content was deleted
+  // before uninstall so these errors would not occur. Unfortunatly I am now unable to 
+  // test this because the Field API module is disabling uninstall of Tripal Chado until 
+  // all the content is deleted. Thus ensuring the errors described above don't occur.
+  // But I'm Sure I was able to uninstall with content before... 
+  // **I am slowly going crazy; Crazy going slowly am I**
+  // Anyway, I'll leaving the solution code here in case I am able to repeat it in
+  // the future.
+  // @see https://www.drupal.org/node/1262092
+  // @see https://www.drupal.org/node/1861710
+  
+  // First delete all TripalEntities.
+  $entity_ids = (new EntityFieldQuery)->entityCondition("entity_type", "TripalEntity")->execute(); 
+  $entity_ids = reset($entity_ids);
+  entity_delete_multiple("TripalEntity", array_keys($entity_ids));
+
+  // Then delete all TripalBundles.
+  $bundle_ids = (new EntityFieldQuery)->entityCondition("entity_type", "TripalBundle")->execute(); 
+  $bundle_ids = reset($bundle_ids);
+  entity_delete_multiple("TripalBundle", array_keys($bundle_ids));
+  
+  // @TODO: Should we delete all TripalVocabularies and TripalTerms?
+  
+  // Finally purge all fields that are no longer used.
+  field_purge_batch(100);
+  */
+}
+
 /**
  * Implements hook_schema().
  */
@@ -30,7 +67,6 @@ function tripal_entities_schema() {
   // Adds a table for additional information related to bundles.
   $schema['tripal_bundle_variables'] = tripal_entities_tripal_bundle_variables_schema();
 
-
   return $schema;
 }