You are here

public function MetatagViewsCacheWrapper::doDeferredCacheSet in Metatag 8

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.

File

metatag_views/src/MetatagViewsCacheWrapper.php, line 67

Class

MetatagViewsCacheWrapper
This class wraps a Views cache plugin.

Namespace

Drupal\metatag_views

Code

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());
}