From fb396fbd26b8ee80ed97970c3393de121055e48e Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Fri, 8 Mar 2024 10:22:02 +0000
Subject: [PATCH] Issue #3421007 by sorlov, quietone, larowlan, smustgrave:
 Convert ViewsQuery plugin discovery to attributes

---
 .../views/src/Attribute/ViewsQuery.php        | 46 +++++++++++++++++++
 .../views/src/Plugin/views/query/Sql.php      | 13 +++---
 .../src/Plugin/views/query/QueryTest.php      | 13 +++---
 3 files changed, 60 insertions(+), 12 deletions(-)
 create mode 100644 core/modules/views/src/Attribute/ViewsQuery.php

diff --git a/core/modules/views/src/Attribute/ViewsQuery.php b/core/modules/views/src/Attribute/ViewsQuery.php
new file mode 100644
index 000000000000..bb554a30ad98
--- /dev/null
+++ b/core/modules/views/src/Attribute/ViewsQuery.php
@@ -0,0 +1,46 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Drupal\views\Attribute;
+
+use Drupal\Component\Plugin\Attribute\Plugin;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
+
+/**
+ * Defines a ViewsQuery attribute for plugin discovery.
+ *
+ * @see \Drupal\views\Plugin\views\query\QueryPluginBase
+ *
+ * @ingroup views_query_plugins
+ */
+#[\Attribute(\Attribute::TARGET_CLASS)]
+class ViewsQuery extends Plugin {
+
+  /**
+   * Constructs an ViewsDisplayExtender 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
+   *   A short help string; this is displayed in the views UI.
+   * @param bool $no_ui
+   *   Whether or not the plugin is selectable in the UI. If it's 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 bool $no_ui = FALSE,
+    public readonly ?string $deriver = NULL
+  ) {}
+
+}
diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php
index af57e33ec48e..395d689dea35 100644
--- a/core/modules/views/src/Plugin/views/query/Sql.php
+++ b/core/modules/views/src/Plugin/views/query/Sql.php
@@ -8,8 +8,10 @@
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Messenger\MessengerInterface;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
 use Drupal\views\Plugin\views\display\DisplayPluginBase;
 use Drupal\Core\Database\DatabaseExceptionWrapper;
+use Drupal\views\Attribute\ViewsQuery;
 use Drupal\views\Plugin\views\join\JoinPluginBase;
 use Drupal\views\Plugin\views\HandlerBase;
 use Drupal\views\ResultRow;
@@ -21,13 +23,12 @@
  * Views query plugin for an SQL query.
  *
  * @ingroup views_query_plugins
- *
- * @ViewsQuery(
- *   id = "views_query",
- *   title = @Translation("SQL Query"),
- *   help = @Translation("Query will be generated and run using the Drupal database API.")
- * )
  */
+#[ViewsQuery(
+  id: 'views_query',
+  title: new TranslatableMarkup('SQL Query'),
+  help: new TranslatableMarkup('Query will be generated and run using the Drupal database API.')
+)]
 class Sql extends QueryPluginBase {
 
   /**
diff --git a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
index cf1e5dc18d66..82392489840f 100644
--- a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
+++ b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
@@ -3,6 +3,8 @@
 namespace Drupal\views_test_data\Plugin\views\query;
 
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
+use Drupal\views\Attribute\ViewsQuery;
 use Drupal\views\Plugin\views\query\QueryPluginBase;
 use Drupal\views\Plugin\views\join\JoinPluginBase;
 use Drupal\views\ResultRow;
@@ -10,13 +12,12 @@
 
 /**
  * Defines a query test plugin.
- *
- * @ViewsQuery(
- *   id = "query_test",
- *   title = @Translation("Query test"),
- *   help = @Translation("Defines a query test plugin.")
- * )
  */
+#[ViewsQuery(
+  id: 'query_test',
+  title: new TranslatableMarkup('Query test'),
+  help: new TranslatableMarkup('Defines a query test plugin.')
+)]
 class QueryTest extends QueryPluginBase {
   protected $conditions = [];
   protected $fields = [];
-- 
GitLab