CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Cannot access objectRef data in civicrm drupal hooks
Pages: [1]

Author Topic: Cannot access objectRef data in civicrm drupal hooks  (Read 809 times)

karlis

  • I’m new here
  • *
  • Posts: 5
  • Karma: 0
  • CiviCRM version: 4.1.1
  • CMS version: drupal 7.12
  • MySQL version: 5.1.63
  • PHP version: 5.2.17
Cannot access objectRef data in civicrm drupal hooks
September 24, 2012, 08:01:44 pm
I'm having huge trouble with using hook_civicrm_post with drupal.  Like the examples in civicrm.module.sample, I'm accessing data in $objectRef using assocative array syntax, and php shows $objectRef to be an array with print_r and gettype.  When I run the code it gives an error:

Fatal error: Cannot use object of type CRM_Contact_DAO_Contact as array in sensiblebc.module on line 26


but it is not an object, does not identify as an object with gettype, and if I try to access the variable with object syntax ($objectRef->postal_code) it does not have any value.  Anyone have any idea what's going on here?


function sensiblebc_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {

  $riding_id = 'custom_7';
  require_once 'CRM/Core/BAO/CustomValueTable.php';

  if (($objectName == 'Individual' || $objectName == 'Address') && ($op == 'create' || $op == 'edit')) {         

    $postal = $objectRef['postal_code'];  //this is line 26

    $riding = sensiblebc_civicrm_postal_lookup($postal);
    # assign to the custom field         
    $customParams = array("entityID" => $objectId, $riding_id => $riding);         
 
    CRM_Core_BAO_CustomValueTable::setValues( $customParams );     
  }
  return;
}


ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: Cannot access objectRef data in civicrm drupal hooks
September 24, 2012, 11:55:04 pm
@karlis,

The documentation at http://wiki.civicrm.org/confluence/display/CRMDOC42/Hook+Reference#HookReference-hookcivicrmpost suggests the -> syntax is what you need.

Could it be that a Contact doesn't have a postal_code attribute? Your code looks for Contacts or Addresses: don't you really want an Address so you can gain access to the postal code? Perhaps if you have a Contact you might want to use the CiviCRM API to get hold of the Addresses for that contact. Also, Is the custom data attached to the Contact or the Address record?

Finally, rather than requiring CiviCRM code, I think you should be using the CiviCRM API.

Ken

karlis

  • I’m new here
  • *
  • Posts: 5
  • Karma: 0
  • CiviCRM version: 4.1.1
  • CMS version: drupal 7.12
  • MySQL version: 5.1.63
  • PHP version: 5.2.17
Re: Cannot access objectRef data in civicrm drupal hooks
September 25, 2012, 11:53:42 am
Thanks for the light, Ken.  I'm not exactly sure how I ended up looking at the Address object instead of the Individual.  it's surely not meant to be adressed that way, because then objectref refers to an Address associative array, which isn't happy after the hook finishes. so now I access the  postal code thus

if ($objectName == 'Individual' && ($op == 'create' || $op == 'edit')) { 
    $postal = $objectRef->address[0]->postal_code;
    $riding = sensiblebc_civicrm_postal_lookup($postal);

and everything seems to be happy.  I thought I was using the API.  I will look into the (other?) API as an alternative.

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: Cannot access objectRef data in civicrm drupal hooks
September 25, 2012, 10:54:10 pm
Another thought would be to see whether hook_civicrm_pre is a better place to do this, because that's called before the Contact is edited or created. Perhaps changing the params in that place might work. (Never tried it, so I don't know if that's going to work.)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Cannot access objectRef data in civicrm drupal hooks
September 25, 2012, 11:52:11 pm
The address hook goes out as an array not an object  - I think this is fixed in 4.2
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Cannot access objectRef data in civicrm drupal hooks

This forum was archived on 2017-11-26.