Browse Source

Entity delete now seems to work

Stephen Ficklin 9 years ago
parent
commit
b566111e6f

+ 15 - 33
tripal_entities/includes/TripalEntityController.inc

@@ -41,41 +41,23 @@ class TripalEntityController extends EntityAPIController {
    * Really a convenience function for deleteMultiple().
    */
   public function delete($entity) {
-    $this->deleteMultiple(array($entity));
-  }
+    $transaction = db_transaction();
+    try {
+      // Invoke hook_entity_delete().
+      module_invoke_all('entity_delete', $entity, $entity->type);
+      field_attach_delete($entity->type, $entity);
 
-  /**
-   * Delete one or more tripal_entities entities.
-   *
-   * Deletion is unfortunately not supported in the base
-   * DrupalDefaultEntityController class.
-   *
-   * @param array $entities
-   *   An array of entity IDs or a single numeric ID.
-   */
-  public function deleteMultiple($entities) {
-
-    $ids = array();
-    if (!empty($entities)) {
-      $transaction = db_transaction();
-      try {
-        foreach ($entities as $entity_id) {
-          // Invoke hook_entity_delete().
-          $entity = entity_load($entity_id);
-          module_invoke_all('entity_delete', $entity, $entity->type);
-          field_attach_delete($entity->type, $entity);
-          $ids[] = $entity->id;
-        }
-        db_delete('tripal_entity')
-          ->condition('id', $ids, 'IN')
-          ->execute();
-      }
-      catch (Exception $e) {
-        $transaction->rollback();
-        watchdog_exception('entity_example', $e);
-        throw $e;
-      }
+      db_delete('tripal_entity')
+        ->condition('id', $entity->id)
+        ->execute();
+    }
+    catch (Exception $e) {
+      $transaction->rollback();
+      watchdog_exception('tripal_entities', $e);
+      throw $e;
+      return FALSE;
     }
+    return TRUE;
   }
 
   /**

+ 28 - 22
tripal_entities/includes/TripalEntityUIController.inc

@@ -92,7 +92,6 @@ class TripalEntityUIController extends EntityDefaultUIController {
       'access callback' => 'tripal_entity_access',
       'access arguments' => array('edit', 1),
       'type' => MENU_CALLBACK,
-
       'weight' => 10,
     );
     return $items;
@@ -241,7 +240,7 @@ function tripal_entity_form($form, &$form_state, $entity = NULL) {
       $entity = entity_get_controller($cvterm->dbxref_id->db_id->name)->create(array('bundle' => $bundle_id));
       field_attach_form($cvterm->dbxref_id->db_id->name, $entity, $form, $form_state);
 
-      $form['submit'] = array(
+      $form['add_button'] = array(
         '#type' => 'submit',
         '#value' => t('Add a new ' . $cvterm->name),
         '#name' => 'add_data',
@@ -250,13 +249,22 @@ function tripal_entity_form($form, &$form_state, $entity = NULL) {
     }
     else {
       field_attach_form($cvterm->dbxref_id->db_id->name, $entity, $form, $form_state);
-
-      $form['submit'] = array(
+      $form['entity_id'] = array(
+        '#type'  => 'hidden',
+        '#value' => $entity->id,
+      );
+      $form['update_button'] = array(
         '#type' => 'submit',
         '#value' => t('Update'),
         '#name' => 'update_data',
         '#weight' => 1000
       );
+      $form['delete_button'] = array(
+        '#type' => 'submit',
+        '#value' => t('Delete'),
+        '#name' => 'delete_data',
+        '#weight' => 1000,
+      );
     }
 
     // The entity object must be added to the $form_state in order for
@@ -295,8 +303,8 @@ function tripal_entity_form_validate($form, &$form_state) {
 function tripal_entity_form_submit($form, &$form_state) {
   if ($form_state['clicked_button']['#name'] == 'cancel') {
     if (array_key_exists('id', $form_state['values'])){
-      $entity = $form_state['values']['entity'];
-      $form_state['redirect'] = "data/$entity->id";
+      $entity_id = $form_state['values']['entity_id'];
+      $form_state['redirect'] = "data/$entity_id";
     }
     else {
       $form_state['redirect'] = "admin/structure/tripal_entity";
@@ -316,14 +324,10 @@ function tripal_entity_form_submit($form, &$form_state) {
     $entity->save();
     $form_state['redirect'] = "data/$entity->id";
   }
-}
-/**
- * Form API submit callback for the delete button.
- *
- * @todo Remove hard-coded path
- */
-function tripal_entity_form_submit_delete(&$form, &$form_state) {
-  $form_state['redirect'] = 'admin/content/tripal_entitys/tripal_entity/' . $form_state['tripal_entity']->tripal_entity_id . '/delete';
+  if ($form_state['clicked_button']['#name'] == 'delete_data') {
+    $entity_id = $form_state['values']['entity_id'];
+    $form_state['redirect'] = 'data/' . $entity_id . '/delete';
+  }
 }
 
 
@@ -337,11 +341,11 @@ function tripal_entity_form_submit_delete(&$form, &$form_state) {
  */
 function tripal_entity_delete_form($form, &$form_state, $entity) {
   $form_state['entity'] = $entity;
-
+dpm($entity);
   $form['#submit'][] = 'tripal_entity_delete_form_submit';
 
   $form = confirm_form($form,
-    t('Click the delete button below to confirm deltion of the record titled: %title', array('%title' => $entity->title)),
+    t('Click the delete button below to confirm deletion of the record titled: %title', array('%title' => $entity->title)),
     'admin/content/tripal_entity',
     '<p>' . t('This action cannot be undone.') . '</p>',
     t('Delete'),
@@ -358,12 +362,14 @@ function tripal_entity_delete_form($form, &$form_state, $entity) {
 function tripal_entity_delete_form_submit($form, &$form_state) {
   $entity = $form_state['entity'];
 
-  tripal_entity_delete($entity);
-
-  drupal_set_message(t('The tripal_entity %name has been deleted.', array('%name' => $tripal_entity->name)));
-  watchdog('tripal_entity', 'Deleted tripal_entity %name.', array('%name' => $tripal_entity->name));
-
-  $form_state['redirect'] = 'admin/content/tripal_entitys';
+  $entity_controller = new TripalEntityController($entity->type);
+  if($entity_controller->delete($entity)) {
+    drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
+    $form_state['redirect'] = 'admin/content/tripal_entitys';
+  }
+  else {
+    drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
+  }
 }
 
 /**

+ 2 - 1
tripal_entities/includes/tripal_entities.chado_entity.inc

@@ -47,5 +47,6 @@ function tripal_entities_entity_update($entity, $type) {
  * Implements hook_entity_delete().
  */
 function tripal_entities_entity_delete($entity, $type) {
-
+  // TODO: delete the record in the public.chado_entity table
+  // and delete the corresponding record in Chado.
 }