From fb49143fab43dc0e5fb76084aa73016ae3ff150a Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 12 Mar 2024 10:20:08 +0000
Subject: [PATCH] Issue #3421009 by sorlov, mohit_aghera, pradhumanjain2311,
 larowlan, smustgrave, alexpott: Convert ViewsCache plugin discovery to
 attributes

---
 .../views/src/Attribute/ViewsCache.php        | 53 +++++++++++++++++++
 .../views/src/Plugin/views/cache/None.php     | 14 ++---
 .../views/src/Plugin/views/cache/Tag.php      | 13 ++---
 .../views/src/Plugin/views/cache/Time.php     | 13 ++---
 4 files changed, 75 insertions(+), 18 deletions(-)
 create mode 100644 core/modules/views/src/Attribute/ViewsCache.php

diff --git a/core/modules/views/src/Attribute/ViewsCache.php b/core/modules/views/src/Attribute/ViewsCache.php
new file mode 100644
index 000000000000..286eb6ebeadc
--- /dev/null
+++ b/core/modules/views/src/Attribute/ViewsCache.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace Drupal\views\Attribute;
+
+use Drupal\Component\Plugin\Attribute\Plugin;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
+
+/**
+ * Defines a views cache plugins type attribute for plugin discovery.
+ *
+ * @see \Drupal\views\Plugin\views\cache\CachePluginBase
+ *
+ * @ingroup views_cache_plugins
+ */
+#[\Attribute(\Attribute::TARGET_CLASS)]
+class ViewsCache extends Plugin {
+
+  /**
+   * Constructs a ViewsCache attribute.
+   *
+   * @param string $id
+   *   The plugin ID.
+   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $title
+   *   The plugin title used in the views UI.
+   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $short_title
+   *   (optional) The short title used in the views UI.
+   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $help
+   *   (optional) A short help string; this is displayed in the views UI.
+   * @param string[]|null $display_types
+   *   (optional) The types of the display this plugin can be used with.
+   *   For example the Feed display defines the type 'feed', so only rss style
+   *   and row plugins can be used in the views UI.
+   * @param string[] $base
+   *   The base tables on which this cache plugin can be used.
+   *   If no base table is specified the plugin can be used with all tables.
+   * @param bool $no_ui
+   *   Whether the plugin should be not selectable in the UI.
+   *   If set to TRUE, you can still use it via the API in config files.
+   * @param class-string|null $deriver
+   *   (optional) The deriver class.
+   */
+  public function __construct(
+    public readonly string $id,
+    public readonly ?TranslatableMarkup $title = NULL,
+    public readonly ?TranslatableMarkup $short_title = NULL,
+    public readonly ?TranslatableMarkup $help = NULL,
+    public readonly ?array $display_types = NULL,
+    public readonly array $base = [],
+    public readonly ?bool $no_ui = NULL,
+    public readonly ?string $deriver = NULL
+  ) {}
+
+}
diff --git a/core/modules/views/src/Plugin/views/cache/None.php b/core/modules/views/src/Plugin/views/cache/None.php
index fc9b58b238bb..5f0533f4aa22 100644
--- a/core/modules/views/src/Plugin/views/cache/None.php
+++ b/core/modules/views/src/Plugin/views/cache/None.php
@@ -2,17 +2,19 @@
 
 namespace Drupal\views\Plugin\views\cache;
 
+use Drupal\Core\StringTranslation\TranslatableMarkup;
+use Drupal\views\Attribute\ViewsCache;
+
 /**
  * Caching plugin that provides no caching at all.
  *
  * @ingroup views_cache_plugins
- *
- * @ViewsCache(
- *   id = "none",
- *   title = @Translation("None"),
- *   help = @Translation("No caching of Views data.")
- * )
  */
+#[ViewsCache(
+  id: 'none',
+  title: new TranslatableMarkup('None'),
+  help: new TranslatableMarkup('No caching of Views data.'),
+)]
 class None extends CachePluginBase {
 
   public function summaryTitle() {
diff --git a/core/modules/views/src/Plugin/views/cache/Tag.php b/core/modules/views/src/Plugin/views/cache/Tag.php
index cc542c251940..64608acf5ac7 100644
--- a/core/modules/views/src/Plugin/views/cache/Tag.php
+++ b/core/modules/views/src/Plugin/views/cache/Tag.php
@@ -3,18 +3,19 @@
 namespace Drupal\views\Plugin\views\cache;
 
 use Drupal\Core\Cache\CacheBackendInterface;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
+use Drupal\views\Attribute\ViewsCache;
 
 /**
  * Simple caching of query results for Views displays.
  *
  * @ingroup views_cache_plugins
- *
- * @ViewsCache(
- *   id = "tag",
- *   title = @Translation("Tag based"),
- *   help = @Translation("Tag based caching of data. Caches will persist until any related cache tags are invalidated.")
- * )
  */
+#[ViewsCache(
+  id: 'tag',
+  title: new TranslatableMarkup('Tag based'),
+  help: new TranslatableMarkup('Tag based caching of data. Caches will persist until any related cache tags are invalidated.'),
+)]
 class Tag extends CachePluginBase {
 
   /**
diff --git a/core/modules/views/src/Plugin/views/cache/Time.php b/core/modules/views/src/Plugin/views/cache/Time.php
index 17ad64e456cf..24ae27de3f9d 100644
--- a/core/modules/views/src/Plugin/views/cache/Time.php
+++ b/core/modules/views/src/Plugin/views/cache/Time.php
@@ -6,19 +6,20 @@
 use Drupal\Core\Datetime\DateFormatterInterface;
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
+use Drupal\views\Attribute\ViewsCache;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Simple caching of query results for Views displays.
  *
  * @ingroup views_cache_plugins
- *
- * @ViewsCache(
- *   id = "time",
- *   title = @Translation("Time-based"),
- *   help = @Translation("Simple time-based caching of data.")
- * )
  */
+#[ViewsCache(
+  id: 'time',
+  title: new TranslatableMarkup('Time-based'),
+  help: new TranslatableMarkup('Simple time-based caching of data.'),
+)]
 class Time extends CachePluginBase {
 
   /**
-- 
GitLab