6

I have custom data for events that is not visible to anonymous users (it's for internal use only).

I want to access that data from my implementation of hook_civicrm_post().

When I call the API "get" from within the hook, the API doesn't return this custom data, since it's protected.

I guess I want my implementation of the hook to run as if it's a logged in user. How do I do that within the hook, but not in a way that opens up visibility once the hook completes running?

Here's a cut-down sample of my code, where 'custom_25' is the custom field which is not returned as part of $event ...

function xxxxxx_civicrm_post( $op, $objectName, $id, &$params )
{
  if ($objectName == 'Participant') {
    switch ($op) {
      case 'create':
        $event = civicrm_api3('Event', 'get', array(
            'sequential' => 1,
            'return' => 'custom_25',
            'id' => $params->event_id,
        ));
        break;
    }
  } 
}
6
  • @xavier, my code is function xxxxxx_civicrm_post( $op, $objectName, $id, &$params ) { if ($objectName == 'Participant') { switch ($op) { case 'create': $event = civicrm_api3('Event', 'get', array( 'sequential' => 1, 'return' => 'custom_25', 'id' => $params->event_id, )); break; } } }
    – Ken West
    Apr 13, 2015 at 9:37
  • @xavier, 'custom_25' is a custom field and the permission 'access all custom data' is not given to anonymous users. The code that limits access is at CRM/Core/BAO/CustomGroup.php on line 480 in v4.5.8.
    – Ken West
    Apr 13, 2015 at 9:43
  • @xavier, my code is invoked when a Participant is created and the intent is to do some special processing if a custom field is set. It's not appropriate to make this field visible to end users.
    – Ken West
    Apr 13, 2015 at 9:45
  • Can you please put your code in the question so it is more readable? What CMS are you in?
    – Elin
    Apr 13, 2015 at 12:22
  • @elin, I've added the code to the question as requested. I'm using Drupal.
    – Ken West
    Apr 14, 2015 at 2:00

2 Answers 2

4

I followed @xavier's answer, but I'm posting what I did in case it's useful to others.

See the code below. The '25' is a magic number for this Custom Field, and can be found using the CiviCRM API Explorer. The foreach loop finds the right result among those returned (even though I specify Custom Field '25' I get results for other fields).

function xxxxxx_civicrm_post( $op, $objectName, $id, &$params )
{
  if ($objectName == 'Participant') {
    switch ($op) {
      case 'create':
        $custom_values = civicrm_api3('CustomValue', 'get', array(
            'sequential' => 1,
            'entity_id' => $params->event_id,
            'entity_table' => 'civicrm_event',
            'id' => 25,
        ));

        $id = null;
        foreach ($custom_values['values'] as $custom_value) {
          if (isset($custom_value['latest'])
              && isset($custom_value['id'])
              && $custom_value['id'] = '25') {
            $id = (int) $custom_value['latest'];
            break;
          }
        }

        if ( ! isset($id) ) {
          break;
        }

        /* Use the $id */
        break;
    }
  } 
}
2

By default, any api call in php doesn't take into account the permission (, so not being able to access that custom data might be another issue. Can you publish the code you are using? You mention the post_hook, so I'm assuming it's in php?

http://wiki.civicrm.org/confluence/display/CRMDOC/API+Security

You should use the CustomValue API to fetch the custom field, it might be an issue with the Event api that doesn't work with custom_xxx format

http://wiki.civicrm.org/confluence/display/CRMDOC/Using+Custom+Data+with+the+API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.