public function ComputedPhpFormatterBase::viewElements in Computed Field 8.2
Same name and namespace in other branches
- 3.x src/Plugin/Field/FieldFormatter/ComputedPhpFormatterBase.php \Drupal\computed_field\Plugin\Field\FieldFormatter\ComputedPhpFormatterBase::viewElements()
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 ComputedFormatterBase::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ ComputedPhpFormatterBase.php, line 16
Class
- ComputedPhpFormatterBase
- Base class for own php-formatter
Namespace
Drupal\computed_field\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode = NULL) {
$cache_duration = $this
->getSetting('cache_duration');
$cache_unit = $this
->getSetting('cache_unit');
$elements = [];
foreach ($items as $delta => $item) {
if ($cache_unit < 0) {
$elements[$delta] = [
'#markup' => $this
->prepareValue($this
->formatItem($item->value, $item, $delta, $langcode)),
];
}
else {
$value = $this
->prepareValue($item
->executeCode());
$elements[$delta] = [
'#markup' => $this
->prepareValue($this
->formatItem($item
->executeCode(), $item, $delta, $langcode)),
'#cache' => [
'keys' => [
$items
->getEntity()
->getEntityTypeId(),
$items
->getEntity()
->bundle(),
$this->viewMode,
],
'max-age' => $cache_duration * $cache_unit,
],
];
}
}
return $elements;
}