You are here

function civicrm_entity_civicrm_pre in CiviCRM Entity 7.2

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

Implement the pre hook and delete Drupal field data when civicrm deletes an entity.

Parameters

$op:

$object_name:

$id:

$params:

File

./civicrm_entity.module, line 3461

Code

function civicrm_entity_civicrm_pre($op, $objectName, $id, &$params) {
  if ($op == 'delete') {
    $entity_type = '';
    $valid_entities = _civicrm_entity_enabled_entities();
    switch ($objectName) {
      case 'Individual':
      case 'Household':
      case 'Organization':
        $entity_type = 'civicrm_contact';
        break;
      default:
        $entity_type = 'civicrm_' . _civicrm_entity_get_entity_name_from_camel($objectName);
        break;
    }
    if (isset($valid_entities[$entity_type])) {
      if ($objectName == 'EntityTag') {
        $api_params = [
          'sequential' => 1,
          'entity_id' => $params[0][0],
          'entity_table' => $params[1],
        ];
        $api_results = civicrm_api3('EntityTag', 'get', $api_params);
        if (!empty($api_results['values'])) {
          foreach ($api_results as $delta => $result) {
            if ($result['tag_id'] == $id) {
              $entity = entity_load_single($entity_type, $result['id']);
              break;
            }
          }
        }
      }
      else {
        $entity = entity_load_single($entity_type, $id);
      }
      if (!empty($entity)) {
        field_attach_delete($entity_type, $entity);
      }
    }
  }
}