You are here

function civicrm_entity_civicrm_post in CiviCRM Entity 8.3

Same name and namespace in other branches
  1. 7.2 civicrm_entity.module \civicrm_entity_civicrm_post()
  2. 7 civicrm_entity.module \civicrm_entity_civicrm_post()

Implements hook_civicrm_post().

File

./civicrm_entity.module, line 402
Module file for the CiviCRM Entity module.

Code

function civicrm_entity_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  if (\Drupal::config('civicrm_entity.settings')
    ->get('disable_hooks')) {
    return;
  }
  $operations = [
    'create',
    'edit',
    'delete',
    'restore',
  ];
  if (!in_array($op, $operations)) {
    return;
  }
  $entityType = SupportedEntities::getEntityType($objectName);

  // Check valid entity type.
  if (!$entityType) {
    return;
  }

  /** @var \Drupal\civicrm_entity\CiviEntityStorage $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage($entityType);

  // Fix because $objectId is not set for participant payments, possibly other
  // entity types.
  if (!$objectId) {

    // If we cannot determine the id, bail.
    if (empty($objectRef->id)) {
      return;
    }
    $objectId = $objectRef->id;
  }
  if ($op == 'delete') {
    $entity = _civicrm_entity_stash($objectName, $objectId);
  }
  else {

    // Special handling for EntityTag objects.
    if ($entityType == 'civicrm_entity_tag') {
      foreach ($objectRef[0] as $entityTag) {
        $object = new CRM_Core_BAO_EntityTag();
        $object->entity_id = $entityTag;
        $object->entity_table = 'civicrm_contact';
        $object->tag_id = $objectId;
        if ($object
          ->find(TRUE)) {
          $entity = $storage
            ->load($object->id);
          $entity->original = $storage
            ->loadUnchanged($entity
            ->id());
          _civicrm_entity_post_invoke($op, $storage, $entity);
        }
      }
      return;
    }
    if ($entity = $storage
      ->load($objectId)) {
      $entity->original = $storage
        ->loadUnchanged($entity
        ->id());
    }
  }
  if ($entity) {
    _civicrm_entity_post_invoke($op, $storage, $entity);
  }
  _civicrm_entity_stash($objectName, $objectId, 'clear');
}