Skip to content

Commit

Permalink
Merge pull request #20144 from eileenmcnaughton/never
Browse files Browse the repository at this point in the history
hook_managed - do not try to disable managed entities if is_active is not available to api3.create
  • Loading branch information
eileenmcnaughton authored May 7, 2021
2 parents 638fdb5 + 3cf0255 commit c96bdd5
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions CRM/Core/ManagedEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ public function updateExistingEntity($dao, $todo) {
$doUpdate = ($policy == 'always');

if ($doUpdate) {
$defaults = ['id' => $dao->entity_id, 'is_active' => 1];
$defaults = ['id' => $dao->entity_id];
if ($this->isActivationSupported($dao->entity_type)) {
$defaults['is_active'] = 1;
}
$params = array_merge($defaults, $todo['params']);

$manager = CRM_Extension_System::singleton()->getManager();
Expand Down Expand Up @@ -301,10 +304,12 @@ public function updateExistingEntity($dao, $todo) {
* inactive.
*
* @param CRM_Core_DAO_Managed $dao
*
* @throws \CiviCRM_API3_Exception
*/
public function disableEntity($dao) {
// FIXME: if ($dao->entity_type supports is_active) {
if (TRUE) {
public function disableEntity($dao): void {
$entity_type = $dao->entity_type;
if ($this->isActivationSupported($entity_type)) {
// FIXME cascading for payproc types?
$params = [
'version' => 3,
Expand Down Expand Up @@ -479,4 +484,23 @@ protected function onApiError($entity, $action, $params, $result) {
throw new Exception('API error: ' . $result['error_message'] . ' on ' . $entity . '.' . $action);
}

/**
* Determine if an entity supports APIv3-based activation/de-activation.
* @param string $entity_type
*
* @return bool
* @throws \CiviCRM_API3_Exception
*/
private function isActivationSupported(string $entity_type): bool {
if (!isset(Civi::$statics[__CLASS__][__FUNCTION__][$entity_type])) {
$actions = civicrm_api3($entity_type, 'getactions', [])['values'];
Civi::$statics[__CLASS__][__FUNCTION__][$entity_type] = FALSE;
if (in_array('create', $actions, TRUE) && in_array('getfields', $actions)) {
$fields = civicrm_api3($entity_type, 'getfields', ['action' => 'create'])['values'];
Civi::$statics[__CLASS__][__FUNCTION__][$entity_type] = array_key_exists('is_active', $fields);
}
}
return Civi::$statics[__CLASS__][__FUNCTION__][$entity_type];
}

}

0 comments on commit c96bdd5

Please sign in to comment.