You are here

function _hook_post_action_post_save in Hook Post Action 8

Same name and namespace in other branches
  1. 7 hook_post_action.module \_hook_post_action_post_save()

Post save shutdown callback.

Parameters

EntityInterface $entity: Updated entity.

string $op: Operation name.

3 string references to '_hook_post_action_post_save'
hook_post_action_entity_delete in ./hook_post_action.module
Implements hook_entity_delete
hook_post_action_entity_insert in ./hook_post_action.module
Implements hook_entity_insert().
hook_post_action_entity_update in ./hook_post_action.module
Implements hook_entity_update().

File

./hook_post_action.module, line 39
Contains post hooks declaration.

Code

function _hook_post_action_post_save(EntityInterface $entity, $op) {
  $entity_is_saved = FALSE;
  $new_entity = NULL;
  if ($op == 'insert' || $op == 'update') {
    $entity_is_saved = TRUE;
  }
  if ($op == 'delete') {
    try {
      $new_entity = \Drupal::entityTypeManager()
        ->getStorage($entity
        ->getEntityTypeId())
        ->load($entity
        ->id());
      if (!$new_entity) {
        $entity_is_saved = TRUE;
      }
    } catch (\Drupal\Component\Plugin\Exception\PluginNotFoundException $e) {

      // If Entity Type is deleted, allow hooks to run; implementing modules
      // beware.
      $entity_is_saved = TRUE;
    }
  }
  if ($entity_is_saved) {
    $type = $entity
      ->getEntityTypeId();
    $module_handler = Drupal::moduleHandler();

    // Invokes hook_ENTITY_TYPE_post<operation name>.
    $module_handler
      ->invokeAll($type . '_post' . $op, [
      $entity,
    ]);
    $module_handler
      ->invokeAll($type . '_postsave', [
      $entity,
      $op,
    ]);

    // Invokes hook_entity_post<operation name>.
    $module_handler
      ->invokeAll('entity_post' . $op, [
      $entity,
    ]);
    $module_handler
      ->invokeAll('entity_postsave', [
      $entity,
      $op,
    ]);
  }
}