Skip to main content
added 1892 characters in body
Source Link
artfulrobot
  • 4k
  • 16
  • 33

EDIT please see edit below

Used to work, before CiviCRM v5.18

Interesting observations

Interesting observations

EDIT

Why this no longer works

Works, if you don't need to use phpunit (so, doesn't really work!)

Then myfixercallback would do the same work. (Note:However, if you are using this then beware phpunit tests that use transaction interface: your code won't run until the test ends - which is too late!)

However! it seems that CiviCRM - at least from 5.18, possibly earlier, now properly creates default custom data. So this hack is simply not needed any more. Sorry, certainly 5.24.2 does not do this.

API Wrapper solution (tested with 5.24.2)

You can implement an API wrapper, but you need to implement it for both API3 and API4.


function giftaid_civicrm_apiWrappers(&$wrappers, $apiRequest) {
  if ($apiRequest['entity'] === 'Contribution' && $apiRequest['action'] === 'create') {
    $wrappers[] = new CRM_MyApiWrapperContributionCreate();
  }
}

// ... Implement the class ...

class CRM_MyApiWrapperContributionCreate implements API_Wrapper {

  public function fromApiInput($apiRequest) {

    if ($apiRequest instanceof Civi\Api4\Generic\AbstractAction) {
      // APIv4
      $params = $apiRequest->getParams();
      if (empty($params['id']) && noneOfTheCustomFieldsAreDefinedApi4($apiRequest)) {
        $apiRequest->addValue('my_fieldset_name.my_field_name', '');
      }
    }
    if (empty($apiRequest['params']['id']) && noneOfTheCustomFieldsAreDefinedApi3($apiRequest)) {
        $apiRequest['params']['custom_1234'] = '';
      }
    }
    return $apiRequest;
  }

  public function toApiOutput($apiRequest, $result) {
    return $result;
  }
}

In this example

  • my_fieldset_name is the custom fieldset, in which my_field_name is a field that has '' as a default value, and an internal ID of 1234 which explains where custom_1234 comes from.

  • Rather than relying on setting a value to '' you could look up the default value for a field, but if you're doing this for a specific solution you can do it this way to be more efficient.

  • noneOfTheCustomFieldsAreDefinedApi3 needs to scan the params for fields named custom_N where N is the known ID value of each of your fields. And noneOfTheCustomFieldsAreDefinedApi4 needs to scan the params for keys beginning with my_fieldset_name.

EDIT please see edit below

Interesting observations

EDIT

Then myfixercallback would do the same work. (Note: if you are using this then beware phpunit tests that use transaction interface: your code won't run until the test ends - which is too late!)

However! it seems that CiviCRM - at least from 5.18, possibly earlier, now properly creates default custom data. So this hack is simply not needed any more. Sorry, certainly 5.24.2 does not do this.

Used to work, before CiviCRM v5.18

Interesting observations

Why this no longer works

Works, if you don't need to use phpunit (so, doesn't really work!)

Then myfixercallback would do the same work. However, if you are using this then beware phpunit tests that use transaction interface: your code won't run until the test ends - which is too late!

However! it seems that CiviCRM - at least from 5.18, possibly earlier, now properly creates default custom data. So this hack is simply not needed any more. Sorry, certainly 5.24.2 does not do this.

API Wrapper solution (tested with 5.24.2)

You can implement an API wrapper, but you need to implement it for both API3 and API4.


function giftaid_civicrm_apiWrappers(&$wrappers, $apiRequest) {
  if ($apiRequest['entity'] === 'Contribution' && $apiRequest['action'] === 'create') {
    $wrappers[] = new CRM_MyApiWrapperContributionCreate();
  }
}

// ... Implement the class ...

class CRM_MyApiWrapperContributionCreate implements API_Wrapper {

  public function fromApiInput($apiRequest) {

    if ($apiRequest instanceof Civi\Api4\Generic\AbstractAction) {
      // APIv4
      $params = $apiRequest->getParams();
      if (empty($params['id']) && noneOfTheCustomFieldsAreDefinedApi4($apiRequest)) {
        $apiRequest->addValue('my_fieldset_name.my_field_name', '');
      }
    }
    if (empty($apiRequest['params']['id']) && noneOfTheCustomFieldsAreDefinedApi3($apiRequest)) {
        $apiRequest['params']['custom_1234'] = '';
      }
    }
    return $apiRequest;
  }

  public function toApiOutput($apiRequest, $result) {
    return $result;
  }
}

In this example

  • my_fieldset_name is the custom fieldset, in which my_field_name is a field that has '' as a default value, and an internal ID of 1234 which explains where custom_1234 comes from.

  • Rather than relying on setting a value to '' you could look up the default value for a field, but if you're doing this for a specific solution you can do it this way to be more efficient.

  • noneOfTheCustomFieldsAreDefinedApi3 needs to scan the params for fields named custom_N where N is the known ID value of each of your fields. And noneOfTheCustomFieldsAreDefinedApi4 needs to scan the params for keys beginning with my_fieldset_name.

added 53 characters in body
Source Link
artfulrobot
  • 4k
  • 16
  • 33

However! it seems that CiviCRM - at least fromit seems that CiviCRM - at least from 5.18, possibly earlier, now properly creates default custom data. So this hack is simply not needed any more. Sorry, certainly 5.18, possibly earlier, now properly creates default custom data24. So this hack is simply2 does not needed any moredo this.

However! it seems that CiviCRM - at least from 5.18, possibly earlier, now properly creates default custom data. So this hack is simply not needed any more.

However! it seems that CiviCRM - at least from 5.18, possibly earlier, now properly creates default custom data. So this hack is simply not needed any more. Sorry, certainly 5.24.2 does not do this.

added 149 characters in body
Source Link
artfulrobot
  • 4k
  • 16
  • 33

Then myfixercallback would do the same work. (Note: if you are using this then beware phpunit tests that use transaction interface: your code won't run until the test ends - which is too late!)

Then myfixercallback would do the same work.

Then myfixercallback would do the same work. (Note: if you are using this then beware phpunit tests that use transaction interface: your code won't run until the test ends - which is too late!)

added 14 characters in body
Source Link
artfulrobot
  • 4k
  • 16
  • 33
Loading
edited body
Source Link
artfulrobot
  • 4k
  • 16
  • 33
Loading
update answer for more recent versions
Source Link
artfulrobot
  • 4k
  • 16
  • 33
Loading
Source Link
artfulrobot
  • 4k
  • 16
  • 33
Loading