You are here

function entity_delete_log_entity_delete in Entity Delete Log 7

Implements hook_entity_delete().

File

./entity_delete_log.module, line 63
Saves custom log entry to database when an entity is deleted.

Code

function entity_delete_log_entity_delete($entity, $type) {

  // Grab the entity types chosen for logging.
  $entity_types = array_values(variable_get('entity_delete_log_entity_types', array()));

  // If the incoming entity type is enabled for logging, let's log it.
  if (in_array($type, $entity_types, true)) {

    // Let's set up the variables and their values to insert.
    $variables = array();

    // The user id.
    global $user;
    $variables['uid'] = $user->uid;

    // Grab the entity id and bundle (we don't care about the vid).
    list($entity_id, $vid, $entity_bundle) = entity_extract_ids($type, $entity);
    $variables['entity_id'] = $entity_id;
    $variables['entity_bundle'] = $entity_bundle;

    // The entity type is the same as the incoming type.
    $variables['entity_type'] = $type;

    // If there is an entity user id, we'll use that as the author, otherwise
    // we'll just use user #1. We don't want to use the deleted user's entity id
    // as the author, since they will lo longer exist, so we'll just use #1.
    $variables['author'] = 1;
    if (isset($entity->uid) && $type != 'user') {
      $variables['author'] = $entity->uid;
    }

    // If there is an entity created date, we'll use it, otherwise we'll use null.
    $variables['created'] = null;
    if (isset($entity->created)) {
      $variables['created'] = $entity->created;
    }

    // We'll set the deleted time to right now.
    $variables['deleted'] = time();

    // For some entities, we need to dynamically set some of the log entry data,
    // let's do that...
    // Define the remaining log variables we need to determine.
    $variables['entity_title'] = null;
    $variables['revisions'] = null;

    // Take a snapshot of the entity.
    $variables['entity'] = $entity;

    // Depending on the entity type, let's fill in the remaining log variables.
    switch ($type) {

      // For comments, use the comment subject for the title.
      case 'comment':
        $variables['entity_title'] = $entity->subject;
        break;

      // For files, use the file name for the title and the timestamp for the
      // created date.
      case 'file':
        $variables['entity_title'] = $entity->filename;
        $variables['created'] = $entity->timestamp;
        break;

      // For nodes, use the node title for the title and grab a revisions count.
      case 'node':
        $variables['entity_title'] = $entity->title;
        $variables['revisions'] = db_select('node_revision', 'nr')
          ->fields('nr', array(
          'nid',
        ))
          ->condition('nid', $variables['entity_id'])
          ->countQuery()
          ->execute()
          ->fetchField();
        break;

      // For vocabularies, terms and users, use the name for the title.
      case 'taxonomy_vocabulary':
      case 'taxonomy_term':
      case 'user':
        $variables['entity_title'] = $entity->name;
        break;
    }

    // Give other modules an opportunity via hook_entity_delete_log_alter() to
    // make any changes to the log data variables before insertion.
    // @deprecated because we use entity_save().
    if (sizeof(module_implements('entity_delete_log_alter')) > 0) {
      $variables = module_invoke_all('entity_delete_log_alter', $entity, $type, $variables);
    }

    // If nobody could determine a title, let's just use 'N/A'.
    if (!isset($variables['entity_title'])) {
      $variables['entity_title'] = t('N/A');
    }

    // Make sure the required variables have been provided, otherwise don't
    // insert anything and throw a warning. We don't need to check the
    // entity_title since it will have been set above.
    $proceed = true;
    $msgs = array();
    if (!isset($variables['entity_id'])) {
      $proceed = false;
      $msgs[] = t('Missing Entity ID');
    }
    if (!isset($variables['entity_type'])) {
      $proceed = false;
      $msgs[] = t('Missing Entity Type');
    }
    if (!isset($variables['author'])) {
      $proceed = false;
      $msgs[] = t('Missing Entity Author');
    }
    if (!isset($variables['deleted'])) {
      $proceed = false;
      $msgs[] = t('Missing Entity Deleted Time');
    }
    if (!isset($variables['uid'])) {
      $proceed = false;
      $msgs[] = t('Missing Acting User ID');
    }

    // If we are ready to proceed, insert the log entry data, otherwise show
    // the warning(s) in a message.
    if (!$proceed) {
      drupal_set_message(t('Entity Delete Log Failed!') . theme('item_list', array(
        'items' => $msgs,
      )), 'warning');
    }
    else {
      $entity_delete_log = (object) $variables;
      entity_save('entity_delete_log', $entity_delete_log);

      // Now that we've logged this entity's deletion, let's let others do any
      // extra stuff they need with hook_entity_delete_log_post_process().
      // @deprecated because we used entity_save().
      module_invoke_all('entity_delete_log_post_process', $entity, $type, $entity_delete_log);
    }
  }
}