public function CustomMultiValue::viewElements in CiviCRM Entity 8.3
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides OptionsDefaultFormatter::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ CustomMultiValue.php, line 93
Class
- CustomMultiValue
- Plugin implementation of the 'civicrm_entity_custom_multi_value' formatter.
Namespace
Drupal\civicrm_entity\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
try {
$entity = $items
->getEntity();
$result = $this->civicrmApi
->get('CustomValue', [
'sequential' => 1,
'return' => [
$this->fieldDefinition
->getName(),
],
'entity_id' => $entity
->id(),
'entity_table' => $entity
->getEntityTypeId(),
]);
if (!empty($result)) {
$result = reset($result);
$field_id = $result['id'];
$result = array_filter($result, function ($key) {
return is_int($key);
}, ARRAY_FILTER_USE_KEY);
foreach ($result as &$value) {
$value = \CRM_Core_BAO_CustomField::displayValue($value, $field_id, $entity
->id());
$value = is_array($value) ? implode($this
->getSetting('separator'), $value) : $value;
}
// @todo Provide a workaround for element values for now until we can
// have another way to handle multi-group, multi-value CiviCRM fields.
foreach ($result as $delta => $item) {
$elements[$delta] = [
'#markup' => $item,
'#allowed_tags' => FieldFilteredMarkup::allowedTags(),
];
}
}
} catch (\CiviCRM_API3_Exception $e) {
// Don't do anything.
}
return $elements;
}