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) »
  • Standard on Custom Fields
Pages: [1]

Author Topic: Standard on Custom Fields  (Read 1469 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Standard on Custom Fields
May 12, 2011, 06:39:23 pm
Hi guys,

We are still getting pretty random behaviour on custom fields (some may have been introduced in 3.3 series not quite sure as ... the tests weren't adequate).

Anyway currently a mixture of 'custom_fieldno_valueID' and 'custom_fieldno' are returned. For example participant_get returns

'custom_1' = "I'm a barbie"

But membership returns

'custom_1_34' = "I'm a barbie"

'custom_1' always seems to work for setting the custom fields with the caution that if it's a multi field then it will add an entry whereas 'custom_1_34' will change an existing entry

I suggest that since we don't know whether people are relying on the anomolies & we should be stable we return both on the GET requests. Otherwise we should settle on just 'custom_1'
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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Standard on Custom Fields
May 12, 2011, 06:48:56 pm
NB - contribution follows the same std as participant ('custom_1')
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Standard on Custom Fields
May 12, 2011, 11:07:08 pm
Hi,

dlobo did mention at one point introducing a name/label to custom fields and use it in the code. I'd personally prefer working with custom_whatever than custom_123. That's one of the issues I have making re-usable modules.

As for the number, definitely having the same convention on all entities is needed. I prefer custom_x.

Side note: when you add/test custom fields, can you check if api.xxx.getfields returns the custom fields ? You probably have to implement the function and return the exportable fields instead of the dao->getfield (check the contact api for example).

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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Standard on Custom Fields
May 12, 2011, 11:31:38 pm
Well, I think 'custom_123' has been our standard to-date & anything else should be as well. Don't suppose you want to take a look at membership get - I felt like I was drowning in that code.... I can submit the tes.
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

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Standard on Custom Fields
May 13, 2011, 07:15:30 am
Quote from: xavier on May 12, 2011, 11:07:08 pm
dlobo did mention at one point introducing a name/label to custom fields and use it in the code. I'd personally prefer working with custom_whatever than custom_123. That's one of the issues I have making re-usable modules.

yes its on the list, but fairly low down on my list (primarily due to time constraints). ideally we should use something like custom_groupName:fieldName and things are a lot more portable and reusable. Its also a fairly big change across a lot of the codebase, so doing it early in the release cycle would help (so for folks interested in contributing this code, now is an ideal time to do this for 4.1

However we still need to figure out how to modify the protocol to include the custom value id (especially for multiple record values) and make it part of the form. it curently is encoded in the element name as custom_FIELDID_VALUEID

lobo



lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Standard on Custom Fields
May 15, 2011, 04:51:34 pm
membership & event now use the same function (in utils) & return both & are tested for the one that was missing
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

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Standard on Custom Fields
May 16, 2011, 05:35:43 pm
Along these lines, we need to think about how to handle multi-valued fields (checkboxes, multi-select, and the like). The most obvious way to input/output them is in arrays. In creating the customValue API, I added the following code to the get function:
    //Convert multi-value strings to arrays
    //TODO: Is this, or should this be, a utility function?
    $sp = CRM_Core_DAO::VALUE_SEPARATOR;
    foreach ($values as &$value) {
      if (strpos($value, $sp) !== FALSE) {
        $value = explode($sp, trim($value, $sp));
      }
    }
Which handily converts all multi-valued results strings into arrays. Since the VALUE_SEPARATOR is added around even single value items (i.e. user only checked one checkbox), the results are nice and consistent (the single checked item will be in a single-item array).  It seems to me that this code should just be added to civicrm_api3_create_success() and then we wouldn't have to worry about individual APIs handling it differently.

Then there's the question of handling input. The inverse of the above code looks like this:
  $sp = CRM_Core_DAO::VALUE_SEPARATOR;
  foreach ($params as &$param) {
    if (is_array($param)) {
      $param = $sp . implode($sp, $param) . $sp;
    }
  }
However, with input it's not quite so simple. The trouble is that API users may assume that they don't need to provide data in arrays if there's only one item. So in the case of a single checkbox being checked, the API is going to need to do a little extra work to ensure that the string gets wrapped in VALUE_SEPARATORs. Again, it would be nice to just add this feature to Api.php so that we are nice and consistent.
Try asking your question on the new CiviCRM help site.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Standard on Custom Fields
May 16, 2011, 05:38:08 pm
Just a note _civicrm_apiv3_custom_data_get is the function I added to utils.php so that membership & event could call the same function & return the std field name - so this would be a possible place to add more handling - or we can remove the guts of this & make it call something different. I put in a couple of tests so if that breaks it we should see
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Standard on Custom Fields
May 17, 2011, 12:15:36 am
Hi,

Yeap, as array in output is fine, as input fine as well but for the json.

for api.contact.get I did do a return=sort_name,email,phone   I am assuming that given that we don't know the content of the multivalue, we are a bit stuck, or can we safely know it won't contain "," ?

Don't know, but potentially an issue for ajax/rest for the input

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

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Standard on Custom Fields
May 17, 2011, 12:44:50 pm
I have supplied a patch that will explode all strings containing VALUE_SEPARATOR into arrays during create_success(). Works quite nicely. http://issues.civicrm.org/jira/browse/CRM-8131

That takes care of the output, now we need to do something about input. It seems that some APIs (contact, for example) already accept input for checkbox/select fields as an array. But they have the strange quirk of requiring the values to be in the array keys. Again, let's standardize this (and take values from array VALUES, for god's sake), preferably inside the civicrm_api() function so that it doesn't need to be done in each separate civicrm_api3_entity_create() function.
Again, the issue is that we need to wrap the values in VALUE_SEPARATOR. Easily done if the user passed in an array of values. But if they were lazy and only passed a string, it still needs to get wrapped. Which means for each custom value we need to look it up in civicrm_custom_field table and see if its html_type was Checkbox or Select and wrap appropriately. It means a lot of extra queries, but I don't see any way around it.
Try asking your question on the new CiviCRM help site.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Standard on Custom Fields

This forum was archived on 2017-11-26.