Commit d934f7eb authored by Alexandre Dias's avatar Alexandre Dias Committed by Damien McKenna
Browse files

Issue #3261473 by DamienMcKenna, saidatom, claudiu.cristea, heddn, Evaldas...

Issue #3261473 by DamienMcKenna, saidatom, claudiu.cristea, heddn, Evaldas Užkuras, Eugene Bocharov: Views cache wrapper overrides other modules cache logic.
parent a073ce48
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -41,6 +41,8 @@ Metatag 8.x-1.x-dev, 2022-xx-xx
#3311891 by DamienMcKenna, GiorgosK: MaskIcon::setValue() should not set a NULL
  value.
#3311911 by DamienMcKenna: TypedData logic has an error.
#3261473 by DamienMcKenna, saidatom, claudiu.cristea, heddn, Evaldas Užkuras,
  Eugene Bocharov: Views cache wrapper overrides other modules cache logic.


Metatag 8.x-1.21, 2022-07-16
+0 −4
Original line number Diff line number Diff line
@@ -103,9 +103,6 @@ function metatag_views_metatag_route_entity(RouteMatchInterface $route_match) {
 * Implements hook_views_post_render().
 */
function metatag_views_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) {
  if ($cache instanceof MetatagViewsCacheWrapper) {
    $cache->doDeferredCacheSet();
  }
  $extenders = $view->getDisplay()->getExtenders();
  if (isset($extenders['metatag_display_extender'])) {
    $first_row_tokens = MetatagDisplayExtender::getFirstRowTokensFromStylePlugin($view);
@@ -113,5 +110,4 @@ function metatag_views_views_post_render(ViewExecutable $view, &$output, CachePl
    $extender = $extenders['metatag_display_extender'];
    $extender->setFirstRowTokens($first_row_tokens);
  }

}
+0 −6
Original line number Diff line number Diff line
services:
  metatag_views.plugin.manager.views.cache:
    decorates: plugin.manager.views.cache
    class: Drupal\metatag_views\MetatagViewsCachePluginManager
    arguments:
      - '@metatag_views.plugin.manager.views.cache.inner'
+35 −52
Original line number Diff line number Diff line
@@ -31,13 +31,6 @@ class MetatagViewsCacheWrapper extends CachePluginBase {
   */
  protected $plugin;

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

  /**
   * MetatagViewsCacheWrapper constructor.
   *
@@ -53,23 +46,6 @@ class MetatagViewsCacheWrapper extends CachePluginBase {
   */
  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 = [
@@ -78,40 +54,47 @@ class MetatagViewsCacheWrapper extends CachePluginBase {
        'current_page' => $view->getCurrentPage(),
        'first_row_tokens' => MetatagDisplayExtender::getFirstRowTokensFromStylePlugin($view),
      ];
    $cache_set_max_age = $plugin->cacheSetMaxAge(self::RESULTS);
      $cache_set_max_age = $this->cacheSetMaxAge('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());
      \Drupal::cache($plugin->resultsBin)
        ->set($plugin->generateResultsKey(), $data, $expire, $plugin->getCacheTags());
    }
    else {
      $this->plugin->cacheSet($type);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function cacheGet($type) {
    if ($type === self::RESULTS) {
    switch ($type) {
      case 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) {
          /** @var \Drupal\views\ViewExecutable $view */
            $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']);
            $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'] ?? []);
              $extenders['metatag_display_extender']->setFirstRowTokens($cache->data['first_row_tokens']);
            }
            return TRUE;
          }
        }
        return FALSE;
    }

      default:
        return $this->plugin->cacheGet($type);
    }
  }

  /**
   * {@inheritdoc}