Skip to content

Commit

Permalink
Remove use of nullArray in delete hooks
Browse files Browse the repository at this point in the history
Looking at civicrm#18995 I was struck by the delete hook being non-standard. In general we don't load from the DB just to pass
to the hook as the hook can do that itself. However, when I looked at other hooks
I found that they were passing out nullArray - this is a legacy method that precedes
being on a php version that supported default params when passing by reference.

It's further known to introduce intermittent hard to debug issues. This adds the new hook
and also adds standardisation to the other hooks for pre+delete.

I've left the create one for now but GroupContact is a good example of something
close to what we want to standardise on there
  • Loading branch information
eileenmcnaughton committed Nov 27, 2020
1 parent 9219cf5 commit be12df5
Show file tree
Hide file tree
Showing 19 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CRM/Batch/BAO/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function generateBatchName() {
*/
public static function deleteBatch($batchId) {
// delete entry from batch table
CRM_Utils_Hook::pre('delete', 'Batch', $batchId, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Batch', $batchId);
$batch = new CRM_Batch_DAO_Batch();
$batch->id = $batchId;
$batch->delete();
Expand Down
2 changes: 1 addition & 1 deletion CRM/Campaign/BAO/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static function del($id) {
return FALSE;
}

CRM_Utils_Hook::pre('delete', 'Campaign', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Campaign', $id);

$dao = new CRM_Campaign_DAO_Campaign();
$dao->id = $id;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static function getCaseType($caseId, $colName = 'title') {
* is successful
*/
public static function deleteCase($caseId, $moveToTrash = FALSE) {
CRM_Utils_Hook::pre('delete', 'Case', $caseId, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Case', $caseId);

//delete activities
$activities = self::getCaseActivityDates($caseId);
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3473,9 +3473,9 @@ public static function deleteObjectWithPrimary($type, $id) {
$obj = new $daoName();
$obj->id = $id;
$obj->find();
$hookParams = [];

if ($obj->fetch()) {
CRM_Utils_Hook::pre('delete', $type, $id, $hookParams);
CRM_Utils_Hook::pre('delete', $type, $id);
$contactId = $obj->contact_id;
$obj->delete();
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/BAO/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function discard($id) {
if (!$id || !is_numeric($id)) {
throw new CRM_Core_Exception('Invalid group request attempted');
}
CRM_Utils_Hook::pre('delete', 'Group', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Group', $id);

$transaction = new CRM_Core_Transaction();

Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/BAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ public static function clearCurrentEmployer($id, $action) {
*/
public static function del($id) {
// delete from relationship table
CRM_Utils_Hook::pre('delete', 'Relationship', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Relationship', $id);

$relationship = self::clearCurrentEmployer($id, CRM_Core_Action::DELETE);
$relationship->delete();
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ public static function getTotalAmountAndCount($status = NULL, $startDate = NULL,
* $results no of deleted Contribution on success, false otherwise
*/
public static function deleteContribution($id) {
CRM_Utils_Hook::pre('delete', 'Contribution', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Contribution', $id);

$transaction = new CRM_Core_Transaction();

Expand Down
4 changes: 4 additions & 0 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,8 @@ public static function usedByModule($id) {
*
*/
public static function del($id) {
CRM_Utils_Hook::pre('delete', 'UFGroup', $id);

//check whether this group contains any profile fields
$profileField = new CRM_Core_DAO_UFField();
$profileField->uf_group_id = $id;
Expand All @@ -1416,6 +1418,8 @@ public static function del($id) {
$group = new CRM_Core_DAO_UFGroup();
$group->id = $id;
$group->delete();

CRM_Utils_Hook::post('delete', 'UFGroup', $id, $group);
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static function del($id) {
return NULL;
}

CRM_Utils_Hook::pre('delete', 'Event', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Event', $id);

$extends = ['event'];
$groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends);
Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/BAO/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ public static function deleteParticipant($id) {
if (!$participant->find()) {
return FALSE;
}
CRM_Utils_Hook::pre('delete', 'Participant', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Participant', $id);

$transaction = new CRM_Core_Transaction();

Expand Down
2 changes: 1 addition & 1 deletion CRM/Grant/BAO/Grant.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public static function deleteContact($id) {
* @return bool|mixed
*/
public static function del($id) {
CRM_Utils_Hook::pre('delete', 'Grant', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Grant', $id);

$grant = new CRM_Grant_DAO_Grant();
$grant->id = $id;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Mailing/BAO/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2463,7 +2463,7 @@ public static function del($id) {
throw new CRM_Core_Exception(ts('No id passed to mailing del function'));
}

CRM_Utils_Hook::pre('delete', 'Mailing', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Mailing', $id);

// delete all file attachments
CRM_Core_BAO_File::deleteEntityFile('civicrm_mailing',
Expand Down
2 changes: 1 addition & 1 deletion CRM/Mailing/BAO/MailingAB.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function del($id) {
throw new CRM_Core_Exception(ts('No id passed to MailingAB del function'));
}
CRM_Core_Transaction::create()->run(function () use ($id) {
CRM_Utils_Hook::pre('delete', 'MailingAB', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'MailingAB', $id);

$dao = new CRM_Mailing_DAO_MailingAB();
$dao->id = $id;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Mailing/BAO/MailingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ public static function findPendingTasks($jobId, $medium) {
* @return mixed
*/
public static function del($id) {
CRM_Utils_Hook::pre('delete', 'MailingJob', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'MailingJob', $id);

$jobDAO = new CRM_Mailing_BAO_MailingJob();
$jobDAO->id = $id;
Expand Down
2 changes: 1 addition & 1 deletion CRM/PCP/BAO/PCP.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public static function &pcpLinks($pcpId = NULL) {
* Campaign page id.
*/
public static function deleteById($id) {
CRM_Utils_Hook::pre('delete', 'Campaign', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Campaign', $id);

$transaction = new CRM_Core_Transaction();

Expand Down
2 changes: 1 addition & 1 deletion CRM/Pledge/BAO/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ protected static function isPaymentsRequireRecalculation($params) {
* @return mixed
*/
public static function deletePledge($id) {
CRM_Utils_Hook::pre('delete', 'Pledge', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'Pledge', $id);

$transaction = new CRM_Core_Transaction();

Expand Down
2 changes: 1 addition & 1 deletion CRM/Pledge/BAO/PledgeBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function add($params) {
* @return mixed|null
*/
public static function deletePledgeBlock($id) {
CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO::$_nullArray);
CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id);

$transaction = new CRM_Core_Transaction();

Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function requireCiviModules(&$moduleList) {
* @return null
* the return value is ignored
*/
public static function pre($op, $objectName, $id, &$params) {
public static function pre($op, $objectName, $id, &$params = []) {
$event = new \Civi\Core\Event\PreEvent($op, $objectName, $id, $params);
\Civi::dispatcher()->dispatch('hook_civicrm_pre', $event);
return $event->getReturnValues();
Expand Down
2 changes: 1 addition & 1 deletion Civi/Api4/Generic/Traits/CustomValueActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function deleteObjects($items) {
$customTable = CoreUtil::getTableName($this->getEntityName());
$ids = [];
foreach ($items as $item) {
\CRM_Utils_Hook::pre('delete', $this->getEntityName(), $item['id'], \CRM_Core_DAO::$_nullArray);
\CRM_Utils_Hook::pre('delete', $this->getEntityName(), $item['id']);
\CRM_Utils_SQL_Delete::from($customTable)
->where('id = #value')
->param('#value', $item['id'])
Expand Down

0 comments on commit be12df5

Please sign in to comment.