You are here

function civicrm_case_activity_reference_field_civicrm_post in CiviCRM Entity 7.2

Implements hook_civicrm_post().

Clear specific entity field cache for entities that have fields of type civicase_activity_reference and target new or deleted entity

Parameters

$op:

$objectName:

$objectId:

$objectRef:

File

modules/civicrm_case_activity_reference_field/civicrm_case_activity_reference_field.module, line 494
Provide CiviCRM entity reference field type

Code

function civicrm_case_activity_reference_field_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  if ($op == 'create' || $op == 'delete' || $op == 'edit') {
    $civicrm_case_activity_reference_fields = db_select('field_config', 'fc')
      ->fields('fc', array(
      'field_name',
      'id',
    ))
      ->condition('type', 'civicase_activity_reference')
      ->execute();
    while ($record = $civicrm_case_activity_reference_fields
      ->fetchAssoc()) {
      $field = field_info_field_by_id($record['id']);
      $instances = db_select('field_config_instance', 'fi')
        ->fields('fi', array(
        'id',
        'entity_type',
      ))
        ->condition('field_id', $record['id'])
        ->execute();
      if ($objectName == 'Case') {
        while ($instance = $instances
          ->fetchAssoc()) {

          //clear the cache of entity
          cache_clear_all('field:' . $instance['entity_type'] . ':' . $objectId, 'cache_field');
        }
      }
      if ($objectName == 'Activity') {

        // look to see if this activity is associated with a case, if so we clear the field cache for the case
        while ($instance = $instances
          ->fetchAssoc()) {
          if (!empty($objectRef->case_id)) {
            cache_clear_all('field:' . $instance['entity_type'] . ':' . $objectRef->case_id, 'cache_field');
          }
        }
      }
    }
  }
}