Answers.Body: <p>I followed @xavier's answer, but I'm posting what I did in case it's useful to others.</p>
  
  <p>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).</p>
  
  <pre><code>function xxxxxx_civicrm_post( $op, $objectName, $id, &amp;$params )
  {
    if ($objectName == 'Participant') {
      switch ($op) {
        case 'create':
          $custom_values = civicrm_api3('CustomValue', 'get', array(
              'sequential' =&gt; 1,
              'entity_id' =&gt; $params-&gt;event_id,
              'entity_table' =&gt; 'civicrm_event',
              'id' =&gt; 25,
          ));
  
          $id = null;
          foreach ($custom_values['values'] as $custom_value) {
            if (isset($custom_value['latest'])
                &amp;&amp; isset($custom_value['id'])
                &amp;&amp; $custom_value['id'] = '25') {
              $id = (int) $custom_value['latest'];
              break;
            }
          }
  
          if ( ! isset($id) ) {
            break;
          }
  
          /* Use the $id */
          break;
      }
    } 
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T22:29:04.970Z
Answers.LastActivityDate: 2015-04-14T22:29:04.970Z
Answers.UserDisplayName: Ken West
Answers.Body: <p>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?</p>
  
  <p><a href="http://wiki.civicrm.org/confluence/display/CRMDOC/API+Security" rel="nofollow">http://wiki.civicrm.org/confluence/display/CRMDOC/API+Security</a></p>
  
  <p>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</p>
  
  <p><a href="http://wiki.civicrm.org/confluence/display/CRMDOC/Using+Custom+Data+with+the+API" rel="nofollow">http://wiki.civicrm.org/confluence/display/CRMDOC/Using+Custom+Data+with+the+API</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-13T08:26:47.980Z
Answers.LastActivityDate: 2015-04-13T12:11:46.387Z
Answers.LastEditDate: 2015-04-13T12:11:46.387Z
Answers.UserDisplayName: Xavier
Body: <p>I have custom data for events that is not visible to anonymous users (it's for internal use only).</p>
  
  <p>I want to access that data from my implementation of hook_civicrm_post().</p>
  
  <p>When I call the API "get" from within the hook, the API doesn't return this custom data, since it's protected.</p>
  
  <p>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?</p>
  
  <p>Here's a cut-down sample of my code, where 'custom_25' is the custom field which is not returned as part of $event ...</p>
  
  <pre><code>function xxxxxx_civicrm_post( $op, $objectName, $id, &amp;$params )
  {
    if ($objectName == 'Participant') {
      switch ($op) {
        case 'create':
          $event = civicrm_api3('Event', 'get', array(
              'sequential' =&gt; 1,
              'return' =&gt; 'custom_25',
              'id' =&gt; $params-&gt;event_id,
          ));
          break;
      }
    } 
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-13T09:37:00.223Z
Comments.Text: @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;
      }
    }
  }`
Comments.UserDisplayName: Ken West
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-13T09:43:47.167Z
Comments.Text: @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.
Comments.UserDisplayName: Ken West
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-13T09:45:57.183Z
Comments.Text: @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.
Comments.UserDisplayName: Ken West
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-13T12:22:33.737Z
Comments.Text: Can you please put your code in the question so it is more readable?  What CMS are you in?
Comments.UserDisplayName: Elin
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-14T02:00:30.453Z
Comments.Text: @elin, I've added the code to the question as requested. I'm using Drupal.
Comments.UserDisplayName: Ken West
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-14T13:04:45.563Z
Comments.Text: I've updated my answer, I think you need to use CustomValue api to fetch the custom data, it should work better)
Comments.UserDisplayName: Xavier
Tags: <api><hooks><permissions>
Title: How do I use the API to access protected custom data in a hook?
ThreadText: # How do I use the API to access protected custom data in a hook?
  
  (Asked by: username_0 on 2015-04-13)
  
  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;
      }
    } 
  }
  
  **Tags:** api, hooks, permissions
  
  > **Comments:**
  > **username_0**: @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;     }   } }`  
  > **username_0**: @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.  
  > **username_0**: @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.  
  > **username_1**: Can you please put your code in the question so it is more readable?  What CMS are you in?  
  > **username_0**: @elin, I've added the code to the question as requested. I'm using Drupal.  
  > **username_2**: I've updated my answer, I think you need to use CustomValue api to fetch the custom data, it should work better)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-04-14. Score: 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;
      }
    } 
  }
  
  
  ---
  
  ### Answer by username_2 on 2015-04-13. Score: 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>
  
  
  ---
  
  