You are here

function civicrm_entity_civicrm_pre in CiviCRM Entity 8.3

Same name and namespace in other branches
  1. 7.2 civicrm_entity.module \civicrm_entity_civicrm_pre()

Implements hook_civicrm_pre().

File

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

Code

function civicrm_entity_civicrm_pre($op, $objectName, $id, &$params) {
  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);
  if ($op == 'create') {
    $entity = $storage
      ->create($params);
  }
  elseif (empty($id)) {

    // Sometimes 'delete' is called with an $id of NULL, but we can't really do
    // anything with that in this context, so return.
    return;
  }
  else {

    // Special handling for EntityTag objects.
    if ($objectName == 'EntityTag') {
      $id = $storage
        ->getEntityTagEntityId($params[0][0], $params[1]);
    }
    $entity = $storage
      ->load($id);
  }
  if (!$entity) {
    return;
  }
  if ($entity
    ->id()) {
    $entity->original = $storage
      ->loadUnchanged($entity
      ->id());
  }
  switch ($op) {
    case 'create':
      $storage
        ->civiPreSave($entity);
      break;
    case 'delete':
      $storage
        ->civiPreDelete($entity);
      _civicrm_entity_stash($objectName, $id, $entity);
      break;
    case 'restore':
      $storage
        ->civiPreSave($entity);
      break;
    case 'edit':
      $storage
        ->civiPreSave($entity);
      break;
  }
}