array field output handler
Adds a new field output handler to return multi-value fields as an array.
My use case: I have an API that sends event data, but I have a multi-value custom field ("Category") which I used with the "OptionFieldOutputHandler". This would return comma-separated values if multiple categories were selected, but I needed them to be a JSON array.
This handler adds the ability to return arrays of multi-value fields (thanks for the isMultiValue
property - seems unused elsewhere but very helpful to me!). You can specify in the configuration if you want to return option values or labels.
Merge request reports
Activity
69 public function formatField($rawRecord, $formattedRecord) { 70 $formattedValue = $rawRecord[$this->inputFieldSpec->alias]; 71 // Convert from a value-separated string to an array. 72 if (strpos($formattedValue, \CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) { 73 $formattedValue = explode(\CRM_Core_DAO::VALUE_SEPARATOR, substr($formattedValue, 1, -1)); 74 } 75 // Convert to option labels if applicable. 76 if ($this->label_as_value) { 77 $options = $this->inputFieldSpec->getOptions(); 78 foreach ($formattedValue as $k => $v) { 79 if (isset($options[$v])) { 80 $formattedValue[$k] = $options[$v]; 81 } 82 } 83 } 84 $output = new FieldOutput($rawRecord[$this->inputFieldSpec->alias]); changed this line in version 2 of the diff
Thanks @JonGold I have added a comment in the code can you have a look at that?
@jaapjansma ah - I saw that class but it seemed unimplemented so I thought it might be deprecated. I've just modified the PR to use that class.
mentioned in commit 91b38077