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) »
  • Get the custom field of a Group - which API call is the best?
Pages: [1]

Author Topic: Get the custom field of a Group - which API call is the best?  (Read 2903 times)

stephang

  • I’m new here
  • *
  • Posts: 4
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 7
  • MySQL version: 5.2
  • PHP version: 5.1
Get the custom field of a Group - which API call is the best?
April 06, 2012, 08:15:20 am
Hi,

I am struggling with the CiviCRM API and I'm not sure whether I'm using the right functions.

I have a group which has a custom field (id: custom_5, name: Ownership). What I want is to get the custom fields for one group. I use the following code so far:

$results=civicrm_api(
  "CustomValue", "get",
  array ('version' => '3', 'entity_id' => $group_id, 'entity_type' => 'group', 'entity_table' => 'civicrm_group')
  );


The result is quite what I need, however, CustomValue returns a warning.

Notice: Undefined offset: 2 in civicrm_api3_custom_value_get() (Zeile 171 von DRUPALROOT/sites/all/modules/civicrm/api/v3/CustomValue.php).

I also tried the API function Group.get, but this function does not return custom fields.

Thus, my questions are:
  • Do I use the right API call and the right parameters? How do I get rid of the warning?
  • Is there a better alternative? One that for instance returns all groups and their custom fields?

Does anyone have an idea?

Thanks for your help
Stephan

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Get the custom field of a Group - which API call is the best?
April 07, 2012, 03:11:10 am
Yes, you use the righy API and parameters but it looks like there is still a little coding to be done.....I would ignore the warning if it gives you the right result. I do not think the group API returns custom fields, but I am not sure. Give it a try?

Here is the latest documentation on the API : http://civicrm.org/blogs/eileen/api-civicrm-41-under-hood

Let me know how you get on! You can also check if xavier_d, eileen, totten or me (erik_h or ErikHommel) are online on our IRC channel if you need help.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

stephang

  • I’m new here
  • *
  • Posts: 4
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 7
  • MySQL version: 5.2
  • PHP version: 5.1
Re: Get the custom field of a Group - which API call is the best?
April 10, 2012, 01:49:08 am
Erik, thanks for your reply. I think the group API doesn't return any custom fields (a pity though). I tried Group.get with custom_5 as a return value, but no success. So I'll pursue my approach and let you know.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Get the custom field of a Group - which API call is the best?
April 10, 2012, 01:56:53 am
Would be great if you could add the feature to retrieve custom fields (probably to handle the return param) on the api.group.get

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Get the custom field of a Group - which API call is the best?
April 10, 2012, 04:20:39 am
Agree with X, would be great if you could add the ability to retrieve custom fields to the group API!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Get the custom field of a Group - which API call is the best?
April 12, 2012, 12:45:22 pm
So,

if you  look at some of the other API that DO return custom fields you'll see this line

      _civicrm_api3_custom_data_get($activities[$activityId], 'Activity', $activityId, NULL, $values['activity_type_id']);
 
- so putting the right variant of that function in the right place in the group function would be the place to start & then you can raise a ticket & submit a patch (if any of those things sound tricky jump on our IRC channel for help)
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

stephang

  • I’m new here
  • *
  • Posts: 4
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 7
  • MySQL version: 5.2
  • PHP version: 5.1
Re: Get the custom field of a Group - which API call is the best?
April 16, 2012, 03:40:55 am
I submitted a patch here: http://issues.civicrm.org/jira/browse/CRM-10035 
The patch works for me. I'll be glad to get your feedback on the jira issue.

With the patch, I can use a much nicer API call

$results=civicrm_api(
  "Group",
  "get",
   array ('version' =>'3', 'id' =>$group_id)
);
« Last Edit: April 16, 2012, 03:50:46 am by stephang »

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Get the custom field of a Group - which API call is the best?
April 16, 2012, 03:51:41 am
Thanks for submitting!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Get the custom field of a Group - which API call is the best?
April 16, 2012, 04:04:44 am
I'd really like avoiding that *by default* the request is more complex than needed. Ie if I just want the name of the groups and their id, that'd be a shame to fetch as well to do an extra sql (or several) to fetch all the custom data, waste memory/bandwidth to store stuff I won't use anyway.

We have started using a param return (for contact.get for instance), where you can be specific about what you want.

$results=civicrm_api(
  "Group",
  "get",
   array ('version' =>'3', 'id' =>$group_id, return=array ('id', 'name', 'custom_42'))
);


So by default it doesn't fetch the custom fields, and if you need them, that's still a clean call.

@eileen,

_civicrm_api3_custom_data_get doesn't see the param so we have to check on each calling api if it's needed to get the custom data (if in return).

Should we add a _civicrm_api3_return_custom ($params that checks if there is a custom_X in $params[return] and if (_civicrm_api3_return_custom($params))  {
_civicrm_api3_custom_data_get (...)}
?

-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

stephang

  • I’m new here
  • *
  • Posts: 4
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 7
  • MySQL version: 5.2
  • PHP version: 5.1
Re: Get the custom field of a Group - which API call is the best?
April 16, 2012, 05:53:55 am
Quote from: xavier on April 16, 2012, 04:04:44 am
$results=civicrm_api(
  "Group",
  "get",
   array ('version' =>'3', 'id' =>$group_id, return=array ('id', 'name', 'custom_42'))
);

So by default it doesn't fetch the custom fields, and if you need them, that's still a clean call.

@xavier: I agree that is makes sense to query custom fields only with explicit return parameters. However, the call you suggest doesn't work. I get an Exception ("DB Error: syntax error"). It is thrown in civicrm_api3_group_get(), line 110: $groupObjects = CRM_Contact_BAO_Group::getGroups( $params, $returnProperties );

So it seems to me that Group.get does not accept return parameters as an array?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Get the custom field of a Group - which API call is the best?
April 16, 2012, 01:09:04 pm
stephang - The syntax Xavier describes should work in 4.2 (some work has been done) but is inconsistent in 4.1 & earlier. You can probably use 'return.custom_42' => 1 in 4.2 & earlier.

Xavier - just the custom field get could do with work - ideally we'd find a way of including it in the get query - but then we are starting to duplicate the query object so not sure that's the right path.

But, yes, passing in the params & reducing the get would help
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) »
  • Get the custom field of a Group - which API call is the best?

This forum was archived on 2017-11-26.