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 (Moderator: Donald Lobo) »
  • Useful function for custom field sets
Pages: [1]

Author Topic: Useful function for custom field sets  (Read 747 times)

man4mac

  • I’m new here
  • *
  • Posts: 9
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Druapl 7
  • MySQL version: 5.5
  • PHP version: 5+
Useful function for custom field sets
August 28, 2012, 09:50:23 am
Mods: *I'm trying to post a useful piece of code in here, and it keeps telling me im not allowed to post external links...any chance we can fix this so I can put my post up?

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Useful function for custom field sets
August 28, 2012, 10:12:12 am
(Posting on behalf of man4mac)
----------
I spent quite a while slaving over the way the CiviCRM API works to return a useful result set of custom content regardless of how the field was configured (multiple value field set...or not). This is piece of a bigger drupal module I'm creating for a more seamless developer experience with CiviCRM. This is my first post on the CiviForum, and frankly there is probably a better way to do this, but this snippet may be useful to someone :) Enjoy!

Code: [Select]
<?php
function civi_unify_custom_values($contact_id){
$data = $groups = $fields = $values = array();

$custom_values = civicrm_api("CustomValue","get", array("version"=>3,'sequential' =>'1',"entity_id"=>$contact_id));

if(!$custom_values['is_error']){
$custom_fields = civicrm_api("CustomField","get", array("version"=>3,'sequential' =>'1'));
$custom_groups = civicrm_api("CustomGroup","get", array("version"=>3,'sequential' =>'1'));

foreach($custom_values['values'] as $key => $value_set){
$values[$value_set['id']] = numeric_set($value_set);
}
foreach($custom_groups['values'] as $key => $group){
$groups[$group['id']] = $group['name'];
}
foreach($custom_fields['values'] as $key => $field){
$data[$groups[$field['custom_group_id']]][$field['name']] = $values[$field['id']];
}
return $data;
}else{
return false;
}
}

function 
numeric_set($array){
foreach($array as $key=>$value){
if(!is_numeric($key)){
unset($array[$key]);
}
}
return $array;
}
?>


Ideally you would use it in this manner:

Code: [Select]
<?php 
function get_profile($contact_id){
$contact_id = 1; //for simplicity sake
$profile = civicrm_api("Contact","get", array('version'=>3,'sequential'=>1,"contact_id"=>$contact_id));
if($custom = civi_unify_custom_values($contact_id)){
$profile['values'][0] = $profile['values'][0]+$custom;
}
return $profile['values'][0];
}
?>

Protect your investment in CiviCRM by  becoming a Member!

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Useful function for custom field sets
August 28, 2012, 10:54:40 am
Hi,

Custom fields are a bit too close to the underlying BAOs to be really easy to use.

you can already fetch custom fields using return=custom_<id>, could you paste what you get with that vs. what your solution generates? Kind of hard to see right now just looking at the code;)

And it would be more than welcome to get your solution part of the api (hint ;)

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

man4mac

  • I’m new here
  • *
  • Posts: 9
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Druapl 7
  • MySQL version: 5.5
  • PHP version: 5+
Re: Useful function for custom field sets
August 28, 2012, 11:16:01 am
I probably should have posted a sample result set. Attached is a sample output, this is 2 different field sets of custom content. Profile_Additions is the name of the first set, it only has 1 field named "Shipping_Address" with a text entry. As an example, the second field set is a multi value set with 2 fields "Social_Media" and "URL". You can see the data is returned in a fairly contextual result. Nothing too spiffy, but definitely helpful for custom development in drupal.

Code: [Select]
[Profile_Additions] => Array
        (
            [Shipping_Address] => Array
                (
                    [0] => home
                )

        )

    [Social_Media] => Array
        (
            [Service] => Array
                (
                    [1] => facebook
                    [2] => twitter
                    [3] => dribble
                )

            [URL] => Array
                (
                    [1] => http://www.facebook.com/justinpschroeder
                    [2] => http://www.twitter.com/jpschroeder
                    [3] =>
                )

        )


xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Useful function for custom field sets
August 28, 2012, 10:35:03 pm
Could you add return=array (custom_x,custom_y) (with x and y being the shipping address and the social media fields id) and paste the diff?

About social media, have you seen that the website standard field have now a type (facebook/linkedin...)?

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Useful function for custom field sets

This forum was archived on 2017-11-26.