You are here

hook_post_action.module in Hook Post Action 7

Same filename and directory in other branches
  1. 8 hook_post_action.module

File

hook_post_action.module
View source
<?php

/**
 * @param $entity
 * @param $type
 */
function hook_post_action_entity_insert($entity, $type) {
  $entity_info = array(
    'entity' => $entity,
    'type' => $type,
    'operation' => 'insert',
  );
  drupal_register_shutdown_function('_hook_post_action_post_save', $entity_info);
}

/**
 * @param $entity
 * @param $type
 */
function hook_post_action_entity_update($entity, $type) {

  //hook_node_update unsets the original property after calling the hook functions.

  //To work around this we save a reference to the original object here,

  //and pass it in to _hook_post_action_post_save as an optional fourth argument.
  $original = isset($entity->original) ? $entity->original : NULL;
  $entity_info = array(
    'entity' => $entity,
    'type' => $type,
    'operation' => 'update',
    'original' => $original,
  );
  drupal_register_shutdown_function('_hook_post_action_post_save', $entity_info);
}

/**
 * @param $entity
 * @param $type
 */
function hook_post_action_entity_delete($entity, $type) {
  $entity_info = array(
    'entity' => $entity,
    'type' => $type,
    'operation' => 'delete',
  );
  drupal_register_shutdown_function('_hook_post_action_post_save', $entity_info);
}

/**
 * @param $entity
 * @param $type
 * @param $op
 */
function _hook_post_action_post_save($entity_info) {
  $entity = $entity_info['entity'];
  $type = $entity_info['type'];
  $op = $entity_info['operation'];
  $original = array_key_exists('original', $entity_info) ? $entity_info['original'] : NULL;
  $entity_is_saved = FALSE;
  $new_entity = NULL;

  //If $original is not null then restore it to the original property in the entity.
  if ($original != NULL) {
    $entity->original = $original;
  }
  if ($op == 'insert' || $op == 'update') {
    $entity_is_saved = TRUE;
  }
  if ($op == 'delete') {
    list($id) = entity_extract_ids($type, $entity);
    $new_entity = entity_load($type, array(
      $id,
    ));
    if (!$new_entity) {
      $entity_is_saved = TRUE;
    }
  }
  if ($entity_is_saved) {
    module_invoke_all('entity_post' . $op, $entity, $type);
    module_invoke_all('entity_postsave', $entity, $type, $op);
    if ($type == 'node') {
      module_invoke_all('node_post' . $op, $entity);
      module_invoke_all('node_postsave', $entity, $op);
    }
  }
}