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) »
  • Inconsistent representation of multi-select custom field values.
Pages: [1]

Author Topic: Inconsistent representation of multi-select custom field values.  (Read 693 times)

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Inconsistent representation of multi-select custom field values.
February 05, 2015, 02:40:38 am
I created a multi-select custom field for contacts, and a multi-select custom field for relationship of the employee-type. The possible values are 'A', 'B' and 'C'. (just as an example)

A assigned value 'B' to a contact, and to a relationship. (In fact, I had to create a new relationship with the custom value, because CiviCRM wouldn't update an existing one, but that's another issue.)

When I use the API to retrieve the custom value 'B' of the contact

Code: [Select]
    $result = civicrm_api3('Contact', 'get', array(
    'sequential' => 1,
    'return' => "custom_7",
    'id' => 1,
    ));

I get what I expect: the selected values as an array.

Code: [Select]
    {
    "is_error":0,
    "version":3,
    "count":1,
    "id":1,
    "values":[{
    "contact_id":"1",
    "civicrm_value_mycustomset_4_id":"1",
    "custom_7":["B"],
    "id":"1"
    }]
    }

When I did the same for the relationship:

Code: [Select]
    $result = civicrm_api3('Relationship', 'get', array(
    'sequential' => 1,
    'id' => 217,
    ));

(I didn't use a result field, because that didn't work out, but that is also another issue.)

Now I get the selected values as key-value pairs:
Code: [Select]
    {
    "is_error":0,
    "version":3,
    "count":1,
    "id":217,
    "values":[{
    "id":"217",
    "contact_id_a":"202",
    "contact_id_b":"2",
    "relationship_type_id":"5",
    "is_active":"1",
    "description":"",
    "is_permission_a_b":"0",
    "is_permission_b_a":"0",
    "custom_8":{
    "B":"B"
    },
    "custom_8_1":{
    "B":"B"
    }
    }]
    }

Except when a relationship doesn't have a custom value selected for the custom field, then it is an empty array again:

Code: [Select]
    {
    "is_error":0,
    "version":3,
    "count":1,
    "id":218,
    "values":[{
    "id":"218",
    "contact_id_a":"106",
    "contact_id_b":"2",
    "relationship_type_id":"5",
    "is_active":"1",
    "description":"",
    "is_permission_a_b":"0",
    "is_permission_b_a":"0",
    "custom_8":[],
    "custom_8_2":[]
    }]
    }

So I guess the key-value result I get for custom multi-select values on relationships is a bug?

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Inconsistent representation of multi-select custom field values.
February 05, 2015, 06:12:42 am
Sounds like a bug to me.
Try asking your question on the new CiviCRM help site.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Inconsistent representation of multi-select custom field values.
February 05, 2015, 06:40:23 am
Quote from: johanv on February 05, 2015, 02:40:38 am
So I guess the key-value result I get for custom multi-select values on relationships is a bug?

As the wise Eileen said, "if it ain't tested, it isn't a bug".

But yes, having a different format for the same stuff based on where you call it is not logical. Custom fields are complex and we had lots of discussion about the format (eg. a custom group can have multiple record, a custom field can have multiple values...).

Can you detail the format of your custom group/field? Not sure where your case fits in the various options possible, see what is the best solution (right now as you said, it isn't, so let's call it a bug, even if we didn't break any test)
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Inconsistent representation of multi-select custom field values.
February 05, 2015, 07:18:41 am
In both cases, I had a custom group with one custom field. The custom field was alphanumerical and multi-select. I had three possible values: 'A','B' and 'C'.

I created an issue in the issue tracker:CRM-15915. I will create a couple of unit tests today or tomorrow to make it clear.

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Inconsistent representation of multi-select custom field values.
February 05, 2015, 11:47:04 pm
Unit tests can be found here: https://github.com/civicrm/civicrm-core/pull/5104

There are two tests in this pull request.

The first test creates a multi-select custom field for contacts. It then creates a contact with a certain value as the custom field. Then it gets the contact, and it accesses the custom value (as an array element). This test passes.

The second test does exactly the same, except that the custom field is for relationships. This test fails, because the custom value is not returned as an array of selected items, but as key-value pairs instead.

johanv

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 5
  • #chiro #geek #linux #beer
    • my homepage
  • CiviCRM version: 4.7.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Inconsistent representation of multi-select custom field values.
February 06, 2015, 05:11:13 am
I fixed the issue by changing a line in CRM/Core/BAO/CustomGroup.php. But I am not sure whether what I did is correct. To be honest: I am kind of clueless :-)

The change doesn't seem to cause new unit test failures. And the user interface also seems to be still working.

So if someone could look at the change, it would certainly be appreciated :-)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Inconsistent representation of multi-select custom field values.

This forum was archived on 2017-11-26.