You are here

entity_delete_log.module in Entity Delete Log 7

Saves custom log entry to database when an entity is deleted.

File

entity_delete_log.module
View source
<?php

/**
 * @file
 * Saves custom log entry to database when an entity is deleted.
 */

/**
 * Implements hook_menu(). See the entity_delete_log view for the default local
 * task page display.
 */
function entity_delete_log_menu() {
  $items = array();

  // Module setup form.
  $items['admin/config/system/entity-delete-log'] = array(
    'title' => t('Entity Delete Log'),
    'description' => t('Entity Delete Log Settings'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'entity_delete_log_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );

  // Place an empty default local task so our default view(s) local tasks will
  // work properly.
  $items['admin/reports/entity-delete-log/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  return $items;
}

/**
 * The module's settings form.
 */
function entity_delete_log_settings() {

  // Build the settings form.
  $form = array();

  // Add some helpful links.
  $items = array(
    l('View the Entity Delete Log README', drupal_get_path('module', 'entity_delete_log') . '/README.txt'),
    l('View the Entity Delete Logs', 'admin/reports/entity-delete-log'),
  );
  $form['#prefix'] = theme('item_list', array(
    'items' => $items,
  ));

  // Provide a list of checkboxes for the user to choose which entity types
  // will have delete logging enabled.
  $entity_keys = array_keys(entity_get_info());
  $form['entity_delete_log_entity_types'] = array(
    '#title' => t('Entity Types to Log'),
    '#type' => 'checkboxes',
    '#options' => drupal_map_assoc($entity_keys),
    '#default_value' => variable_get('entity_delete_log_entity_types', array()),
    '#description' => t('Select which entity types will be logged when deleted.'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_entity_delete().
 */
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);
    }
  }
}

/**
 * Implements hook_views_api().
 */
function entity_delete_log_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'entity_delete_log'),
    'template path' => drupal_get_path('module', 'entity_delete_log'),
  );
}

/**
 * Implements hook_entity_info().
 */
function entity_delete_log_entity_info() {
  return array(
    'entity_delete_log' => array(
      'label' => t('Deleted entity'),
      'entity class' => 'Entity',
      'controller class' => 'EntityAPIController',
      'base table' => 'entity_delete_log',
      'fieldable' => TRUE,
      'entity keys' => array(
        'id' => 'entity_delete_log_id',
      ),
      'views controller class' => 'EntityDefaultViewsController',
    ),
  );
}

/**
 * Implements hook_entity_property_info_alter().
 */
function entity_delete_log_entity_property_info_alter(&$info) {
  $info['entity_delete_log']['properties']['uid']['type'] = 'user';
  $info['entity_delete_log']['properties']['uid']['description'] = 'The user who deleted this entity.';
  $info['entity_delete_log']['properties']['author']['type'] = 'user';
  $info['entity_delete_log']['properties']['author']['description'] = 'The original user of this deleted entity.';
  $info['entity_delete_log']['properties']['created']['type'] = 'date';
  $info['entity_delete_log']['properties']['deleted']['type'] = 'date';
}

Functions

Namesort descending Description
entity_delete_log_entity_delete Implements hook_entity_delete().
entity_delete_log_entity_info Implements hook_entity_info().
entity_delete_log_entity_property_info_alter Implements hook_entity_property_info_alter().
entity_delete_log_menu Implements hook_menu(). See the entity_delete_log view for the default local task page display.
entity_delete_log_settings The module's settings form.
entity_delete_log_views_api Implements hook_views_api().