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) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Can a module read and write profile fields
Pages: [1]

Author Topic: Can a module read and write profile fields  (Read 1619 times)

davo

  • Guest
Can a module read and write profile fields
January 24, 2008, 01:51:50 am
On my Drupal/CiviCRM site, I've exposed a few CiviCRM data fields, some pre-defined, some me-defined, as profile fields. Thus they show up in a user's Drupal account form where the user can view and edit them. This is all good.

What I want to know is, if I write a Drupal module, can I access the profile fields within my module? Typically a module can access a user's Drupal attributes, but what about a user's CiviCRM attributes? If this can be done, how do I do it? I need to be able to read the fields, but I'd also like to be able to change them. Any thoughts?

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Can a module read and write profile fields
January 24, 2008, 01:40:06 pm
Modules can perform CRUD operations (create, read, update, delete) on most CiviCRM data elements using our Public APIs. Start with the documentation here:

http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+Public+APIs

... and then search for modules which interface with CiviCRM on Drupal.org etc.
Protect your investment in CiviCRM by  becoming a Member!

davo

  • Guest
Re: Can a module read and write profile fields
January 27, 2008, 05:34:55 pm
OK, I'm off to a good start. I've managed to write a trivial module that can read the details for a user and update one of the user's attributes.

So far I've used the following functions:
  • crm_uf_get_match_id: to convert a Drupal user id into a CiviCRM contact id
  • crm_get_contact: to retrieve the data for a contact id
  • crm_update_contact: to update the user's nick name
  • crm_uf_get_profile_fields: to get the data about my custom profile - haven't really made use of this data though

I have a few follow-up questions.
*) It was quite easy to update a user's nickname. I simply set params to array('nick_name', <new nick name>) and called crm_update_contact and voila. However, how do I achieve the same result for a more complicated field (eg. first name) or a custom value (eg. custom_values[0]['value'])? What do insert into my params array?
*) I've got a custom data field that I'm using in a profile I've defined. It's of the 'select' type, ie. a drop-down list. Is there a simple way to display this data field in my own form? That is, how do I get the field to appear as a drop-down list in my form without having to build the select HTML myself.

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Can a module read and write profile fields
January 27, 2008, 06:01:00 pm
On question 1) - your best bet is to review the api unit tests which you can find in the svn repository. One example which should help is: http://svn.civicrm.org/civicrm/branches/v1.9/test/CRM/api/CreateContactWithCustomValues.php - but check out others for more and different examples.

On question 2) -  Not sure about the scope of what you're trying to do. Have you looked at the "Standalone form" feature for profiles? If that feature doesn't do it for you, I don't think there's a way to do have the api automagically create the HTML for the select field for you. I'm pretty sure you can query for the option values - but I think you'll need to build the form yourself.
Protect your investment in CiviCRM by  becoming a Member!

davo

  • Guest
Re: Can a module read and write profile fields
January 27, 2008, 07:22:54 pm
Here's the code I've written, based on the example you gave for Q1. It correctly updates the nick name for the user, but not their custom field (which is a select, so I'm using the int value for a label). Can you suggest why it doesn't work for the custom field?

  $params = array('contact_id' => $cid);
  $contact = crm_get_contact($params);
  $cf1 = crm_get_custom_field(array('id' => 1)); // I know it's field 1
  $cfid = 'custom_' . $cf1->id;
  $uparams = array(
      'nick_name' => 'Nick #1',
      $cfid => 5,
  );
  $contact = crm_update_contact($contact, $uparams);

davo

  • Guest
Re: Can a module read and write profile fields
January 27, 2008, 07:54:45 pm
Hmmmm. It seems to be working now and I'm not sure what I did to fix things. I did change the type of the value passed to the custom field from int to string. But initially this didn't seem to make any difference. Weird. Oh well, I think I'm good for Q1.

New code excerpt:
  $uparams = array(
    'nick_name' => 'Nick #1',
    $cfid => '5',
  );


As for Q2, I can get the custom field values using <code>crm_get_option_values()</code> and build the HTML if I need to.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Can a module read and write profile fields

This forum was archived on 2017-11-26.