You are here

class MetatagViewsCacheWrapper in Metatag 8

This class wraps a Views cache plugin.

Hierarchy

Expanded class hierarchy of MetatagViewsCacheWrapper

1 file declares its use of MetatagViewsCacheWrapper
metatag_views.module in metatag_views/metatag_views.module
Contains hook implementations for the metatag_views module.

File

metatag_views/src/MetatagViewsCacheWrapper.php, line 20

Namespace

Drupal\metatag_views
View source
class MetatagViewsCacheWrapper extends CachePluginBase {

  /**
   * The cache type we are interested in.
   */
  const RESULTS = 'results';

  /**
   * @var \Drupal\views\Plugin\views\cache\CachePluginBase
   */
  protected $plugin;

  /**
   * Whether cacheSet was called with $type 'result'.
   *
   * @var bool
   */
  protected $called = FALSE;

  /**
   * MetatagViewsCacheWrapper constructor.
   *
   * @param \Drupal\views\Plugin\views\cache\CachePluginBase $plugin
   *   The cache plugin being wrapped.
   */
  public function __construct(CachePluginBase $plugin) {
    $this->plugin = $plugin;
  }

  /**
   * {@inheritdoc}
   */
  public function cacheSet($type) {
    if ($type === self::RESULTS) {
      $this->called = TRUE;
    }
    else {
      $this->plugin
        ->cacheSet($type);
    }
  }

  /**
   * Actually run cacheSet for type results.
   *
   * It needs to be deferred past the render phase so the row tokens in
   * the style plugin are populated.
   */
  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());
  }

  /**
   * {@inheritdoc}
   */
  public function cacheGet($type) {
    if ($type === self::RESULTS) {
      $cutoff = $this->plugin
        ->cacheExpire($type);

      // Values to set: $view->result, $view->total_rows, $view->execute_time,
      // $view->current_page and pass row tokens to metatag display extender.
      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'];

          // Load entities for each 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);
  }

  /**
   * {@inheritdoc}
   */
  public function getResultsKey() {
    return $this->plugin
      ->getResultsKey();
  }

  /**
   * {@inheritdoc}
   */
  public function summaryTitle() {
    return $this->plugin
      ->summaryTitle();
  }

  /**
   * {@inheritdoc}
   */
  public function cacheFlush() {
    $this->plugin
      ->cacheFlush();
  }

  /**
   * {@inheritdoc}
   */
  public function postRender(&$output) {
    $this->plugin
      ->postRender($output);
  }

  /**
   * {@inheritdoc}
   */
  public function generateResultsKey() {
    return $this->plugin
      ->generateResultsKey();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return $this->plugin
      ->getCacheTags();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return $this->plugin
      ->getCacheMaxAge();
  }

  /**
   * {@inheritdoc}
   */
  public function alterCacheMetadata(CacheableMetadata $cache_metadata) {
    $this->plugin
      ->alterCacheMetadata($cache_metadata);
  }

  /**
   * {@inheritdoc}
   */
  public function getRowCacheTags(ResultRow $row) {
    return $this->plugin
      ->getRowCacheTags($row);
  }

  /**
   * {@inheritdoc}
   */
  public function getRowCacheKeys(ResultRow $row) {
    return $this->plugin
      ->getRowCacheKeys($row);
  }

  /**
   * {@inheritdoc}
   */
  public function getRowId(ResultRow $row) {
    return $this->plugin
      ->getRowId($row);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    CachePluginBase::create($container, $configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    $this->plugin
      ->init($view, $display, $options);
  }

  /**
   * {@inheritdoc}
   */
  public function filterByDefinedOptions(array &$storage) {
    $this->plugin
      ->filterByDefinedOptions($storage);
  }

  /**
   * {@inheritdoc}
   */
  public function unpackOptions(&$storage, $options, $definition = NULL, $all = TRUE, $check = TRUE) {
    $this->plugin
      ->unpackOptions($storage, $options, $definition, $all, $check);
  }

  /**
   * {@inheritdoc}
   */
  public function destroy() {
    $this->plugin
      ->destroy();
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $this->plugin
      ->buildOptionsForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    CachePluginBase::trustedCallbacks();
  }

  /**
   * {@inheritdoc}
   */
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    $this->plugin
      ->validateOptionsForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitOptionsForm(&$form, FormStateInterface $form_state) {
    $this->plugin
      ->submitOptionsForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    $this->plugin
      ->query();
  }

  /**
   * {@inheritdoc}
   */
  public function themeFunctions() {
    return $this->plugin
      ->themeFunctions();
  }

  /**
   * {@inheritdoc}
   */
  public function validate() {
    return $this->plugin
      ->validate();
  }

  /**
   * {@inheritdoc}
   */
  public function pluginTitle() {
    return $this->plugin
      ->pluginTitle();
  }

  /**
   * {@inheritdoc}
   */
  public function usesOptions() {
    return $this->plugin
      ->usesOptions();
  }

  /**
   * {@inheritdoc}
   */
  public function globalTokenReplace($string = '', array $options = []) {
    return $this->plugin
      ->globalTokenReplace($string, $options);
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableGlobalTokens($prepared = FALSE, array $types = []) {
    return $this->plugin
      ->getAvailableGlobalTokens($prepared, $types);
  }

  /**
   * {@inheritdoc}
   */
  public function globalTokenForm(&$form, FormStateInterface $form_state) {
    $this->plugin
      ->globalTokenForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public static function preRenderAddFieldsetMarkup(array $form) {
    CachePluginBase::preRenderAddFieldsetMarkup($form);
  }

  /**
   * {@inheritdoc}
   */
  public static function preRenderFlattenData($form) {
    CachePluginBase::preRenderFlattenData($form);
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return $this->plugin
      ->calculateDependencies();
  }

  /**
   * {@inheritdoc}
   */
  public function getProvider() {
    return $this->plugin
      ->getProvider();
  }

  /**
   * {@inheritdoc}
   */
  public static function queryLanguageSubstitutions() {
    CachePluginBase::queryLanguageSubstitutions();
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginId() {
    return $this->plugin
      ->getPluginId();
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseId() {
    return $this->plugin
      ->getBaseId();
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeId() {
    return $this->plugin
      ->getDerivativeId();
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginDefinition() {
    return $this->plugin
      ->getPluginDefinition();
  }

  /**
   * {@inheritdoc}
   */
  public function isConfigurable() {
    return $this->plugin
      ->isConfigurable();
  }

  /**
   * {@inheritdoc}
   */
  public function setStringTranslation(TranslationInterface $translation) {
    return $this->plugin
      ->setStringTranslation($translation);
  }

  /**
   * {@inheritdoc}
   */
  public function __sleep() {
    return $this->plugin
      ->__sleep();
  }

  /**
   * {@inheritdoc}
   */
  public function __wakeup() {
    $this->plugin
      ->__wakeup();
  }

  /**
   * {@inheritdoc}
   */
  public function setMessenger(MessengerInterface $messenger) {
    $this->plugin
      ->setMessenger($messenger);
  }

  /**
   * {@inheritdoc}
   */
  public function messenger() {
    $this->plugin
      ->messenger();
  }
  public function __get($name) {
    return $this->plugin->{$name};
  }
  public function __set($name, $value) {
    $this->plugin->{$name} = $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CachePluginBase::$resultsBin protected property Which cache bin to store query results in.
CachePluginBase::$resultsKey protected property Stores the cache ID used for the results cache.
CachePluginBase::$storage public property Contains all data that should be written/read from cache.
CachePluginBase::cacheExpire protected function Determine the expiration time of the cache type, or NULL if no expire. 2
CachePluginBase::cacheSetMaxAge protected function Determine expiration time in the cache table of the cache type or CACHE_PERMANENT if item shouldn't be removed automatically from cache. 1
CachePluginBase::getDefaultCacheMaxAge protected function Returns the default cache max age. 2
CachePluginBase::prepareViewResult protected function Prepares the view result before putting it into cache.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
MessengerTrait::$messenger protected property The messenger. 29
MetatagViewsCacheWrapper::$called protected property Whether cacheSet was called with $type 'result'.
MetatagViewsCacheWrapper::$plugin protected property
MetatagViewsCacheWrapper::alterCacheMetadata public function Alters the cache metadata of a display upon saving a view. Overrides CachePluginBase::alterCacheMetadata
MetatagViewsCacheWrapper::buildOptionsForm public function Provide a form to edit options for this plugin. Overrides PluginBase::buildOptionsForm
MetatagViewsCacheWrapper::cacheFlush public function Clear out cached data for a view. Overrides CachePluginBase::cacheFlush
MetatagViewsCacheWrapper::cacheGet public function Retrieve data from the cache. Overrides CachePluginBase::cacheGet
MetatagViewsCacheWrapper::cacheSet public function Save data to the cache. Overrides CachePluginBase::cacheSet
MetatagViewsCacheWrapper::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides PluginBase::calculateDependencies
MetatagViewsCacheWrapper::create public static function Creates an instance of the plugin. Overrides PluginBase::create
MetatagViewsCacheWrapper::destroy public function Clears a plugin. Overrides PluginBase::destroy
MetatagViewsCacheWrapper::doDeferredCacheSet public function Actually run cacheSet for type results.
MetatagViewsCacheWrapper::filterByDefinedOptions public function Filter out stored options depending on the defined options. Overrides PluginBase::filterByDefinedOptions
MetatagViewsCacheWrapper::generateResultsKey public function Calculates and sets a cache ID used for the result cache. Overrides CachePluginBase::generateResultsKey
MetatagViewsCacheWrapper::getAvailableGlobalTokens public function Returns an array of available token replacements. Overrides PluginBase::getAvailableGlobalTokens
MetatagViewsCacheWrapper::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides PluginBase::getBaseId
MetatagViewsCacheWrapper::getCacheMaxAge public function Gets the max age for the current view. Overrides CachePluginBase::getCacheMaxAge
MetatagViewsCacheWrapper::getCacheTags public function Gets an array of cache tags for the current view. Overrides CachePluginBase::getCacheTags
MetatagViewsCacheWrapper::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides PluginBase::getDerivativeId
MetatagViewsCacheWrapper::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginBase::getPluginDefinition
MetatagViewsCacheWrapper::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginBase::getPluginId
MetatagViewsCacheWrapper::getProvider public function Returns the plugin provider. Overrides PluginBase::getProvider
MetatagViewsCacheWrapper::getResultsKey public function Returns the resultsKey property. Overrides CachePluginBase::getResultsKey
MetatagViewsCacheWrapper::getRowCacheKeys public function Returns the row cache keys. Overrides CachePluginBase::getRowCacheKeys
MetatagViewsCacheWrapper::getRowCacheTags public function Returns the row cache tags. Overrides CachePluginBase::getRowCacheTags
MetatagViewsCacheWrapper::getRowId public function Returns a unique identifier for the specified row. Overrides CachePluginBase::getRowId
MetatagViewsCacheWrapper::globalTokenForm public function Adds elements for available core tokens to a form. Overrides PluginBase::globalTokenForm
MetatagViewsCacheWrapper::globalTokenReplace public function Returns a string with any core tokens replaced. Overrides PluginBase::globalTokenReplace
MetatagViewsCacheWrapper::init public function Initialize the plugin. Overrides PluginBase::init
MetatagViewsCacheWrapper::isConfigurable public function Determines if the plugin is configurable. Overrides PluginBase::isConfigurable
MetatagViewsCacheWrapper::messenger public function Gets the messenger. Overrides MessengerTrait::messenger
MetatagViewsCacheWrapper::pluginTitle public function Return the human readable name of the display. Overrides PluginBase::pluginTitle
MetatagViewsCacheWrapper::postRender public function Post process any rendered data. Overrides CachePluginBase::postRender
MetatagViewsCacheWrapper::preRenderAddFieldsetMarkup public static function Moves form elements into fieldsets for presentation purposes. Overrides PluginBase::preRenderAddFieldsetMarkup
MetatagViewsCacheWrapper::preRenderFlattenData public static function Flattens the structure of form elements. Overrides PluginBase::preRenderFlattenData
MetatagViewsCacheWrapper::query public function Add anything to the query that we might need to. Overrides PluginBase::query
MetatagViewsCacheWrapper::queryLanguageSubstitutions public static function Returns substitutions for Views queries for languages. Overrides PluginBase::queryLanguageSubstitutions
MetatagViewsCacheWrapper::RESULTS constant The cache type we are interested in.
MetatagViewsCacheWrapper::setMessenger public function Sets the messenger. Overrides MessengerTrait::setMessenger
MetatagViewsCacheWrapper::setStringTranslation public function Sets the string translation service to use. Overrides StringTranslationTrait::setStringTranslation
MetatagViewsCacheWrapper::submitOptionsForm public function Handle any special handling on the validate form. Overrides PluginBase::submitOptionsForm
MetatagViewsCacheWrapper::summaryTitle public function Return a string to display as the clickable title for the access control. Overrides CachePluginBase::summaryTitle
MetatagViewsCacheWrapper::themeFunctions public function Provide a full list of possible theme templates used by this style. Overrides PluginBase::themeFunctions
MetatagViewsCacheWrapper::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides PluginBase::trustedCallbacks
MetatagViewsCacheWrapper::unpackOptions public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away. Overrides PluginBase::unpackOptions
MetatagViewsCacheWrapper::usesOptions public function Returns the usesOptions property. Overrides PluginBase::usesOptions
MetatagViewsCacheWrapper::validate public function Validate that the plugin is correct and can be saved. Overrides PluginBase::validate
MetatagViewsCacheWrapper::validateOptionsForm public function Validate the options form. Overrides PluginBase::validateOptionsForm
MetatagViewsCacheWrapper::__construct public function MetatagViewsCacheWrapper constructor. Overrides PluginBase::__construct
MetatagViewsCacheWrapper::__get public function
MetatagViewsCacheWrapper::__set public function
MetatagViewsCacheWrapper::__sleep public function Overrides DependencySerializationTrait::__sleep
MetatagViewsCacheWrapper::__wakeup public function Overrides DependencySerializationTrait::__wakeup
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$definition public property Plugins's definition
PluginBase::$displayHandler public property The display object this plugin is for.
PluginBase::$options public property Options for this plugin will be held here.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::$renderer protected property Stores the render API renderer. 3
PluginBase::$usesOptions protected property Denotes whether the plugin has an additional options form. 8
PluginBase::$view public property The top object of a view. 1
PluginBase::defineOptions protected function Information about options for all kinds of purposes will be held here. 18
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::doFilterByDefinedOptions protected function Do the work to filter out stored options depending on the defined options.
PluginBase::getRenderer protected function Returns the render API renderer. 1
PluginBase::INCLUDE_ENTITY constant Include entity row languages when listing languages.
PluginBase::INCLUDE_NEGOTIATED constant Include negotiated languages when listing languages.
PluginBase::listLanguages protected function Makes an array of languages, optionally including special languages.
PluginBase::setOptionDefaults protected function Fills up the options of the plugin with defaults.
PluginBase::viewsTokenReplace protected function Replaces Views' tokens in a given string. The resulting string will be sanitized with Xss::filterAdmin. 1
PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT constant Query string to indicate the site default language.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.