View source
<?php
namespace Drupal\webform\Plugin\Field\FieldFormatter;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\webform\Plugin\Field\FieldType\WebformEntityReferenceItem;
use Drupal\webform\WebformInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WebformEntityReferenceFormatterBase extends EntityReferenceFormatterBase implements ContainerFactoryPluginInterface {
protected $renderer;
protected $configFactory;
protected $time;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, RendererInterface $renderer, ConfigFactoryInterface $config_factory) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->configFactory = $config_factory;
$this->renderer = $renderer;
$this->time = \Drupal::service('datetime.time');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('renderer'), $container
->get('config.factory'));
}
protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
$entities = parent::getEntitiesToView($items, $langcode);
foreach ($entities as $entity) {
$item = $entity->_referringItem;
if ($entity
->isOpen()) {
if (isset($item->open)) {
$entity
->set('open', $item->open);
}
if (isset($item->close)) {
$entity
->set('close', $item->close);
}
if (isset($item->status)) {
$entity
->setStatus($item->status);
}
if (isset($item->open) || isset($item->close) || isset($item->status)) {
$entity
->setOverride();
}
}
}
return $entities;
}
protected function setCacheContext(array &$elements, WebformInterface $webform, WebformEntityReferenceItem $item) {
$config = $this->configFactory
->get('webform.settings');
$this->renderer
->addCacheableDependency($elements, $config);
$this->renderer
->addCacheableDependency($elements, $webform);
$max_age = 0;
$states = [
'open',
'close',
];
foreach ($states as $state) {
if ($item->status === WebformInterface::STATUS_SCHEDULED) {
$item_state = $item->{$state};
if ($item_state && strtotime($item_state) > $this->time
->getRequestTime()) {
$item_seconds = strtotime($item_state) - $this->time
->getRequestTime();
if (!$max_age && $item_seconds > $max_age) {
$max_age = $item_seconds;
}
}
}
if ($webform
->status() === WebformInterface::STATUS_SCHEDULED) {
$webform_state = $webform
->get($state);
if ($webform_state && strtotime($webform_state) > $this->time
->getRequestTime()) {
$webform_seconds = strtotime($webform_state) - $this->time
->getRequestTime();
if (!$max_age && $webform_seconds > $max_age) {
$max_age = $webform_seconds;
}
}
}
}
if ($max_age) {
$elements['#cache']['max-age'] = $max_age;
}
}
}