Skip to content
Snippets Groups Projects

Implement regex validation for excluding cache tags.

1 file
+ 53
4
Compare changes
  • Side-by-side
  • Inline
@@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface;
@@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Link;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableDependencyInterface;
 
use Drupal\views\ResultRow;
use Drupal\views\Views;
use Drupal\views\Views;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -343,6 +344,15 @@ class AdvancedViewsCache extends CachePluginBase {
@@ -343,6 +344,15 @@ class AdvancedViewsCache extends CachePluginBase {
parent::submitOptionsForm($form, $form_state);
parent::submitOptionsForm($form, $form_state);
}
}
 
/**
 
* {@inheritdoc}
 
*/
 
public function getRowCacheTags(ResultRow $row) {
 
$cache_tags = parent::getRowCacheTags($row);
 
$this->excludeCacheTags($cache_tags);
 
return $cache_tags;
 
}
 
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
@@ -372,14 +382,53 @@ class AdvancedViewsCache extends CachePluginBase {
@@ -372,14 +382,53 @@ class AdvancedViewsCache extends CachePluginBase {
}
}
// Remove cache tags marked for exclusion.
// Remove cache tags marked for exclusion.
if (!empty($this->options['cache_tags_exclude'])) {
$this->excludeCacheTags($cache_tags);
$cache_tags_exclude = $this->options['cache_tags_exclude'];
$cache_tags = array_diff($cache_tags, $cache_tags_exclude);
}
return $cache_tags;
return $cache_tags;
}
}
 
/**
 
* Compare the array of cache tags with the list of cache tags to exclude.
 
*
 
* If a cache tag matches the exclude pattern, it is removed from the array.
 
*
 
* @param array $cache_tags
 
* The array of cache tags to be filtered.
 
*/
 
protected function excludeCacheTags(array &$cache_tags): void {
 
if (empty($this->options['cache_tags_exclude'])) {
 
return;
 
}
 
 
$cache_exclude = $this->options['cache_tags_exclude'];
 
 
// Filters the array of cache tags to exclude the tags that match the
 
// exclude pattern.
 
$cache_tags = array_filter($cache_tags, function ($tag) use ($cache_exclude) {
 
foreach ($cache_exclude as $exclude) {
 
 
// Try to match the exclude with the tag, if the regex throws any error
 
// the match value will be false, in this case we will check if the tag
 
// is equal to the exclude value. If the tag is not equal to the exclude
 
// value we will continue to the next exclude value.
 
$match = @preg_match($exclude, $tag);
 
if ($match === FALSE) {
 
if ($tag === $exclude) {
 
return FALSE;
 
}
 
continue;
 
}
 
 
// Only returns when the match is valid to avoid skipping the next
 
// exclude value.
 
if ($match) {
 
return FALSE;
 
}
 
}
 
return TRUE;
 
});
 
}
 
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
Loading