View source
<?php
namespace Drupal\metatag_views;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\metatag_views\Plugin\views\display_extender\MetatagDisplayExtender;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MetatagViewsCacheWrapper extends CachePluginBase {
const RESULTS = 'results';
protected $plugin;
protected $called = FALSE;
public function __construct(CachePluginBase $plugin) {
$this->plugin = $plugin;
}
public function cacheSet($type) {
if ($type === self::RESULTS) {
$this->called = TRUE;
}
else {
$this->plugin
->cacheSet($type);
}
}
public function doDeferredCacheSet() {
if (!$this->called) {
return;
}
$plugin = $this->plugin;
$view = $plugin->view;
$data = [
'result' => $plugin
->prepareViewResult($view->result),
'total_rows' => isset($view->total_rows) ? $view->total_rows : 0,
'current_page' => $view
->getCurrentPage(),
'first_row_tokens' => MetatagDisplayExtender::getFirstRowTokensFromStylePlugin($view),
];
$cache_set_max_age = $plugin
->cacheSetMaxAge(self::RESULTS);
$expire = $cache_set_max_age === Cache::PERMANENT ? Cache::PERMANENT : (int) $view
->getRequest()->server
->get('REQUEST_TIME') + $cache_set_max_age;
\Drupal::cache($plugin->resultsBin)
->set($plugin
->generateResultsKey(), $data, $expire, $plugin
->getCacheTags());
}
public function cacheGet($type) {
if ($type === self::RESULTS) {
$cutoff = $this->plugin
->cacheExpire($type);
if ($cache = \Drupal::cache($this->plugin->resultsBin)
->get($this->plugin
->generateResultsKey())) {
if (!$cutoff || $cache->created > $cutoff) {
$view = $this->plugin->view;
$view->result = $cache->data['result'];
$view->query
->loadEntities($view->result);
$view->total_rows = $cache->data['total_rows'];
$view
->setCurrentPage($cache->data['current_page'], TRUE);
$view->execute_time = 0;
$extenders = $view
->getDisplay()
->getExtenders();
if (isset($extenders['metatag_display_extender'])) {
$extenders['metatag_display_extender']
->setFirstRowTokens($cache->data['first_row_tokens']);
}
return TRUE;
}
}
return FALSE;
}
return $this->plugin
->cacheGet($type);
}
public function getResultsKey() {
return $this->plugin
->getResultsKey();
}
public function summaryTitle() {
return $this->plugin
->summaryTitle();
}
public function cacheFlush() {
$this->plugin
->cacheFlush();
}
public function postRender(&$output) {
$this->plugin
->postRender($output);
}
public function generateResultsKey() {
return $this->plugin
->generateResultsKey();
}
public function getCacheTags() {
return $this->plugin
->getCacheTags();
}
public function getCacheMaxAge() {
return $this->plugin
->getCacheMaxAge();
}
public function alterCacheMetadata(CacheableMetadata $cache_metadata) {
$this->plugin
->alterCacheMetadata($cache_metadata);
}
public function getRowCacheTags(ResultRow $row) {
return $this->plugin
->getRowCacheTags($row);
}
public function getRowCacheKeys(ResultRow $row) {
return $this->plugin
->getRowCacheKeys($row);
}
public function getRowId(ResultRow $row) {
return $this->plugin
->getRowId($row);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
CachePluginBase::create($container, $configuration, $plugin_id, $plugin_definition);
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
$this->plugin
->init($view, $display, $options);
}
public function filterByDefinedOptions(array &$storage) {
$this->plugin
->filterByDefinedOptions($storage);
}
public function unpackOptions(&$storage, $options, $definition = NULL, $all = TRUE, $check = TRUE) {
$this->plugin
->unpackOptions($storage, $options, $definition, $all, $check);
}
public function destroy() {
$this->plugin
->destroy();
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$this->plugin
->buildOptionsForm($form, $form_state);
}
public static function trustedCallbacks() {
CachePluginBase::trustedCallbacks();
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$this->plugin
->validateOptionsForm($form, $form_state);
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$this->plugin
->submitOptionsForm($form, $form_state);
}
public function query() {
$this->plugin
->query();
}
public function themeFunctions() {
return $this->plugin
->themeFunctions();
}
public function validate() {
return $this->plugin
->validate();
}
public function pluginTitle() {
return $this->plugin
->pluginTitle();
}
public function usesOptions() {
return $this->plugin
->usesOptions();
}
public function globalTokenReplace($string = '', array $options = []) {
return $this->plugin
->globalTokenReplace($string, $options);
}
public function getAvailableGlobalTokens($prepared = FALSE, array $types = []) {
return $this->plugin
->getAvailableGlobalTokens($prepared, $types);
}
public function globalTokenForm(&$form, FormStateInterface $form_state) {
$this->plugin
->globalTokenForm($form, $form_state);
}
public static function preRenderAddFieldsetMarkup(array $form) {
CachePluginBase::preRenderAddFieldsetMarkup($form);
}
public static function preRenderFlattenData($form) {
CachePluginBase::preRenderFlattenData($form);
}
public function calculateDependencies() {
return $this->plugin
->calculateDependencies();
}
public function getProvider() {
return $this->plugin
->getProvider();
}
public static function queryLanguageSubstitutions() {
CachePluginBase::queryLanguageSubstitutions();
}
public function getPluginId() {
return $this->plugin
->getPluginId();
}
public function getBaseId() {
return $this->plugin
->getBaseId();
}
public function getDerivativeId() {
return $this->plugin
->getDerivativeId();
}
public function getPluginDefinition() {
return $this->plugin
->getPluginDefinition();
}
public function isConfigurable() {
return $this->plugin
->isConfigurable();
}
public function setStringTranslation(TranslationInterface $translation) {
return $this->plugin
->setStringTranslation($translation);
}
public function __sleep() {
return $this->plugin
->__sleep();
}
public function __wakeup() {
$this->plugin
->__wakeup();
}
public function setMessenger(MessengerInterface $messenger) {
$this->plugin
->setMessenger($messenger);
}
public function messenger() {
$this->plugin
->messenger();
}
public function __get($name) {
return $this->plugin->{$name};
}
public function __set($name, $value) {
$this->plugin->{$name} = $value;
}
}