ComputedFormatterBase.php in Computed Field 8.2        
                          
                  
                        
  
  
  
  
File
  src/Plugin/Field/FieldFormatter/ComputedFormatterBase.php
  
    View source  
  <?php
namespace Drupal\computed_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
abstract class ComputedFormatterBase extends FormatterBase {
  
  use ComputedCacheFormatterTrait;
  
  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($item->value),
        ];
      }
      else {
        $elements[$delta] = [
          '#markup' => $this
            ->prepareValue($item
            ->executeCode()),
          '#cache' => [
            'keys' => [
              $items
                ->getEntity()
                ->getEntityTypeId(),
              $items
                ->getEntity()
                ->bundle(),
              $this->viewMode,
            ],
            'max-age' => $cache_duration * $cache_unit,
          ],
        ];
      }
    }
    return $elements;
  }
  
  protected function prepareValue($value) {
    return $value;
  }
}