ComputedPhpFormatterBase.php in Computed Field 8.2
File
src/Plugin/Field/FieldFormatter/ComputedPhpFormatterBase.php
View source
<?php
namespace Drupal\computed_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
abstract class ComputedPhpFormatterBase extends ComputedStringFormatter {
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;
}
public function formatItem($value, FieldItemInterface $item, $delta = 0, $langcode = NULL) {
return $value;
}
}