3 * Test Generated example of using contact GetFields API
4 * Demonstrate retrieving metadata with custom field options *
6 function contact_getfields_example(){
9 'get_options' => 'custom_1',
15 $result = civicrm_api3('contact', 'GetFields', $params);
17 catch (CiviCRM_API3_Exception
$e) {
19 $errorMessage = $e->getMessage();
20 $errorCode = $e->getErrorCode();
21 $errorData = $e->getExtraParams();
22 return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
29 * Function returns array of result expected from previous function
31 function contact_getfields_expectedresult(){
33 $expectedResult = array(
41 'title' => 'Internal Contact ID',
44 'where' => 'civicrm_contact.id',
45 'headerPattern' => '/internal|contact?|id$/i',
47 'api.aliases' => array(
51 'contact_type' => array(
52 'name' => 'contact_type',
54 'title' => 'Contact Type',
58 'where' => 'civicrm_contact.contact_type',
62 'pseudoconstant' => array(
63 'table' => 'civicrm_contact_type',
64 'keyColumn' => 'name',
65 'labelColumn' => 'label',
66 'condition' => 'parent_id IS NULL',
70 'contact_sub_type' => array(
71 'name' => 'contact_sub_type',
73 'title' => 'Contact Subtype',
77 'where' => 'civicrm_contact.contact_sub_type',
78 'headerPattern' => '/C(ontact )?(subtype|sub-type|sub type)/i',
81 'type' => 'Multi-Select',
83 'pseudoconstant' => array(
84 'table' => 'civicrm_contact_type',
85 'keyColumn' => 'name',
86 'labelColumn' => 'label',
87 'condition' => 'parent_id IS NOT NULL',
90 'do_not_email' => array(
91 'name' => 'do_not_email',
93 'title' => 'Do Not Email',
95 'where' => 'civicrm_contact.do_not_email',
96 'headerPattern' => '/d(o )?(not )?(email)/i',
97 'dataPattern' => '/^\\d{1,}$/',
100 'type' => 'CheckBox',
103 'do_not_phone' => array(
104 'name' => 'do_not_phone',
106 'title' => 'Do Not Phone',
108 'where' => 'civicrm_contact.do_not_phone',
109 'headerPattern' => '/d(o )?(not )?(call|phone)/i',
110 'dataPattern' => '/^\\d{1,}$/',
113 'type' => 'CheckBox',
116 'do_not_mail' => array(
117 'name' => 'do_not_mail',
119 'title' => 'Do Not Mail',
121 'where' => 'civicrm_contact.do_not_mail',
122 'headerPattern' => '/^(d(o\\s)?n(ot\\s)?mail)|(\\w*)?bulk\\s?(\\w*)$/i',
123 'dataPattern' => '/^\\d{1,}$/',
126 'type' => 'CheckBox',
129 'do_not_sms' => array(
130 'name' => 'do_not_sms',
132 'title' => 'Do Not Sms',
134 'where' => 'civicrm_contact.do_not_sms',
135 'headerPattern' => '/d(o )?(not )?(sms)/i',
136 'dataPattern' => '/^\\d{1,}$/',
139 'type' => 'CheckBox',
142 'do_not_trade' => array(
143 'name' => 'do_not_trade',
145 'title' => 'Do Not Trade',
147 'where' => 'civicrm_contact.do_not_trade',
148 'headerPattern' => '/d(o )?(not )?(trade)/i',
149 'dataPattern' => '/^\\d{1,}$/',
152 'type' => 'CheckBox',
155 'is_opt_out' => array(
156 'name' => 'is_opt_out',
158 'title' => 'No Bulk Emails (User Opt Out)',
161 'where' => 'civicrm_contact.is_opt_out',
164 'type' => 'CheckBox',
167 'legal_identifier' => array(
168 'name' => 'legal_identifier',
170 'title' => 'Legal Identifier',
174 'where' => 'civicrm_contact.legal_identifier',
175 'headerPattern' => '/legal\\s?id/i',
176 'dataPattern' => '/\\w+?\\d{5,}/',
182 'external_identifier' => array(
183 'name' => 'external_identifier',
185 'title' => 'External Identifier',
189 'where' => 'civicrm_contact.external_identifier',
190 'headerPattern' => '/external\\s?id/i',
191 'dataPattern' => '/^\\d{11,}$/',
197 'sort_name' => array(
198 'name' => 'sort_name',
200 'title' => 'Sort Name',
204 'where' => 'civicrm_contact.sort_name',
209 'display_name' => array(
210 'name' => 'display_name',
212 'title' => 'Display Name',
216 'where' => 'civicrm_contact.display_name',
221 'nick_name' => array(
222 'name' => 'nick_name',
224 'title' => 'Nickname',
228 'where' => 'civicrm_contact.nick_name',
229 'headerPattern' => '/n(ick\\s)name|nick$/i',
230 'dataPattern' => '/^\\w+$/',
236 'legal_name' => array(
237 'name' => 'legal_name',
239 'title' => 'Legal Name',
243 'where' => 'civicrm_contact.legal_name',
244 'headerPattern' => '/^legal|(l(egal\\s)?name)$/i',
250 'image_URL' => array(
251 'name' => 'image_URL',
253 'title' => 'Image Url',
257 'where' => 'civicrm_contact.image_URL',
263 'preferred_communication_method' => array(
264 'name' => 'preferred_communication_method',
266 'title' => 'Preferred Communication Method',
270 'where' => 'civicrm_contact.preferred_communication_method',
271 'headerPattern' => '/^p(ref\\w*\\s)?c(omm\\w*)|( meth\\w*)$/i',
272 'dataPattern' => '/^\\w+$/',
277 'pseudoconstant' => array(
278 'optionGroupName' => 'preferred_communication_method',
281 'preferred_language' => array(
282 'name' => 'preferred_language',
284 'title' => 'Preferred Language',
288 'where' => 'civicrm_contact.preferred_language',
289 'headerPattern' => '/^lang/i',
294 'pseudoconstant' => array(
295 'optionGroupName' => 'languages',
296 'keyColumn' => 'name',
299 'preferred_mail_format' => array(
300 'name' => 'preferred_mail_format',
302 'title' => 'Preferred Mail Format',
306 'where' => 'civicrm_contact.preferred_mail_format',
307 'headerPattern' => '/^p(ref\\w*\\s)?m(ail\\s)?f(orm\\w*)$/i',
313 'pseudoconstant' => array(
314 'callback' => 'CRM_Core_SelectValues::pmf',
320 'title' => 'Contact Hash',
324 'where' => 'civicrm_contact.hash',
329 'title' => 'Api Key',
333 'first_name' => array(
334 'name' => 'first_name',
336 'title' => 'First Name',
340 'where' => 'civicrm_contact.first_name',
341 'headerPattern' => '/^first|(f(irst\\s)?name)$/i',
342 'dataPattern' => '/^\\w+$/',
348 'middle_name' => array(
349 'name' => 'middle_name',
351 'title' => 'Middle Name',
355 'where' => 'civicrm_contact.middle_name',
356 'headerPattern' => '/^middle|(m(iddle\\s)?name)$/i',
357 'dataPattern' => '/^\\w+$/',
363 'last_name' => array(
364 'name' => 'last_name',
366 'title' => 'Last Name',
370 'where' => 'civicrm_contact.last_name',
371 'headerPattern' => '/^last|(l(ast\\s)?name)$/i',
372 'dataPattern' => '/^\\w+(\\s\\w+)?+$/',
378 'prefix_id' => array(
379 'name' => 'prefix_id',
381 'title' => 'Individual Prefix',
383 'where' => 'civicrm_contact.prefix_id',
384 'headerPattern' => '/^(prefix|title)/i',
385 'dataPattern' => '/^(mr|ms|mrs|sir|dr)\\.?$/i',
390 'pseudoconstant' => array(
391 'optionGroupName' => 'individual_prefix',
393 'api.aliases' => array(
394 '0' => 'individual_prefix',
395 '1' => 'individual_prefix_id',
398 'suffix_id' => array(
399 'name' => 'suffix_id',
401 'title' => 'Individual Suffix',
403 'where' => 'civicrm_contact.suffix_id',
404 'headerPattern' => '/^suffix$/i',
405 'dataPattern' => '/^(sr|jr)\\.?|i{2,}$/',
410 'pseudoconstant' => array(
411 'optionGroupName' => 'individual_suffix',
413 'api.aliases' => array(
414 '0' => 'individual_suffix',
415 '1' => 'individual_suffix_id',
418 'formal_title' => array(
419 'name' => 'formal_title',
421 'title' => 'Formal Title',
425 'where' => 'civicrm_contact.formal_title',
426 'headerPattern' => '/^title/i',
429 'communication_style_id' => array(
430 'name' => 'communication_style_id',
432 'title' => 'Communication Style',
434 'where' => 'civicrm_contact.communication_style_id',
435 'pseudoconstant' => array(
436 'optionGroupName' => 'communication_style',
439 'email_greeting_id' => array(
440 'name' => 'email_greeting_id',
442 'title' => 'Email Greeting ID',
444 'email_greeting_custom' => array(
445 'name' => 'email_greeting_custom',
447 'title' => 'Email Greeting Custom',
451 'where' => 'civicrm_contact.email_greeting_custom',
456 'email_greeting_display' => array(
457 'name' => 'email_greeting_display',
459 'title' => 'Email Greeting',
466 'postal_greeting_id' => array(
467 'name' => 'postal_greeting_id',
469 'title' => 'Postal Greeting ID',
474 'postal_greeting_custom' => array(
475 'name' => 'postal_greeting_custom',
477 'title' => 'Postal Greeting Custom',
481 'where' => 'civicrm_contact.postal_greeting_custom',
486 'postal_greeting_display' => array(
487 'name' => 'postal_greeting_display',
489 'title' => 'Postal Greeting',
496 'addressee_id' => array(
497 'name' => 'addressee_id',
499 'title' => 'Addressee ID',
501 'addressee_custom' => array(
502 'name' => 'addressee_custom',
504 'title' => 'Addressee Custom',
508 'where' => 'civicrm_contact.addressee_custom',
513 'addressee_display' => array(
514 'name' => 'addressee_display',
516 'title' => 'Addressee',
523 'job_title' => array(
524 'name' => 'job_title',
526 'title' => 'Job Title',
530 'where' => 'civicrm_contact.job_title',
531 'headerPattern' => '/^job|(j(ob\\s)?title)$/i',
532 'dataPattern' => '//',
538 'gender_id' => array(
539 'name' => 'gender_id',
543 'where' => 'civicrm_contact.gender_id',
544 'headerPattern' => '/^gender$/i',
549 'pseudoconstant' => array(
550 'optionGroupName' => 'gender',
553 'birth_date' => array(
554 'name' => 'birth_date',
556 'title' => 'Birth Date',
558 'where' => 'civicrm_contact.birth_date',
559 'headerPattern' => '/^birth|(b(irth\\s)?date)|D(\\W*)O(\\W*)B(\\W*)$/i',
560 'dataPattern' => '/\\d{4}-?\\d{2}-?\\d{2}/',
563 'type' => 'Select Date',
566 'is_deceased' => array(
567 'name' => 'is_deceased',
569 'title' => 'Is Deceased',
571 'where' => 'civicrm_contact.is_deceased',
572 'headerPattern' => '/i(s\\s)?d(eceased)$/i',
575 'type' => 'CheckBox',
578 'deceased_date' => array(
579 'name' => 'deceased_date',
581 'title' => 'Deceased Date',
583 'where' => 'civicrm_contact.deceased_date',
584 'headerPattern' => '/^deceased|(d(eceased\\s)?date)$/i',
587 'type' => 'Select Date',
590 'household_name' => array(
591 'name' => 'household_name',
593 'title' => 'Household Name',
597 'where' => 'civicrm_contact.household_name',
598 'headerPattern' => '/^household|(h(ousehold\\s)?name)$/i',
599 'dataPattern' => '/^\\w+$/',
605 'primary_contact_id' => array(
606 'name' => 'primary_contact_id',
608 'title' => 'Household Primary Contact ID',
609 'FKClassName' => 'CRM_Contact_DAO_Contact',
614 'organization_name' => array(
615 'name' => 'organization_name',
617 'title' => 'Organization Name',
621 'where' => 'civicrm_contact.organization_name',
622 'headerPattern' => '/^organization|(o(rganization\\s)?name)$/i',
623 'dataPattern' => '/^\\w+$/',
630 'name' => 'sic_code',
632 'title' => 'Sic Code',
636 'where' => 'civicrm_contact.sic_code',
637 'headerPattern' => '/^sic|(s(ic\\s)?code)$/i',
643 'user_unique_id' => array(
644 'name' => 'user_unique_id',
646 'title' => 'Unique ID (OpenID)',
650 'where' => 'civicrm_contact.user_unique_id',
651 'headerPattern' => '/^Open\\s?ID|u(niq\\w*)?\\s?ID/i',
652 'dataPattern' => '/^[\\w\\/\\:\\.]+$/',
659 'created_date' => array(
660 'name' => 'created_date',
662 'title' => 'Created Date',
665 'where' => 'civicrm_contact.created_date',
668 'modified_date' => array(
669 'name' => 'modified_date',
671 'title' => 'Modified Date',
674 'where' => 'civicrm_contact.modified_date',
675 'default' => 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
680 'title' => 'Source of Contact Data',
684 'where' => 'civicrm_contact.source',
685 'headerPattern' => '/(S(ource\\s)?o(f\\s)?C(ontact\\s)?Data)$/i',
690 'uniqueName' => 'contact_source',
692 'employer_id' => array(
693 'name' => 'employer_id',
695 'title' => 'Current Employer ID',
697 'where' => 'civicrm_contact.employer_id',
698 'FKClassName' => 'CRM_Contact_DAO_Contact',
700 'type' => 'Autocomplete-Select',
702 'uniqueName' => 'current_employer_id',
704 'is_deleted' => array(
705 'name' => 'is_deleted',
707 'title' => 'Contact is in Trash',
710 'where' => 'civicrm_contact.is_deleted',
712 'type' => 'CheckBox',
714 'uniqueName' => 'contact_is_deleted',
717 'label' => 'Our special field',
718 'groupTitle' => 'select_test_g',
719 'data_type' => 'String',
720 'html_type' => 'Select',
721 'default_value' => '',
723 'options_per_line' => '',
724 'custom_group_id' => '1',
725 'extends' => 'Contact',
726 'is_search_range' => 0,
727 'extends_entity_column_value' => '',
728 'extends_entity_column_id' => '',
731 'option_group_id' => '102',
734 'is_required' => '1',
735 'name' => 'custom_1',
736 'title' => 'Our special field',
743 'current_employer' => array(
744 'title' => 'Current Employer',
745 'description' => 'Name of Current Employer',
748 'dupe_check' => array(
749 'title' => 'Check for Duplicates',
750 'description' => 'Throw error if contact create matches dedupe rule',
755 return $expectedResult;
760 * This example has been generated from the API test suite. The test that created it is called
762 * testCustomFieldCreateWithOptionValues and can be found in
763 * https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContactTest.php
765 * You can see the outcome of the API tests at
766 * https://test.civicrm.org/job/CiviCRM-master-git/
768 * To Learn about the API read
769 * http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
771 * Browse the api on your own site with the api explorer
772 * http://MYSITE.ORG/path/to/civicrm/api/explorer
774 * Read more about testing here
775 * http://wiki.civicrm.org/confluence/display/CRM/Testing
777 * API Standards documentation:
778 * http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards