From dde2f75176c93bd75ba2308f4fbff7f87363106f Mon Sep 17 00:00:00 2001 From: chesn0k Date: Tue, 21 May 2024 20:19:33 +0300 Subject: [PATCH] Use PHP attribute for RestResource plugin. --- .../Plugin/_rest-resource/rest-resource.twig | 19 ++++++++++--------- .../src/Plugin/rest/resource/FooResource.php | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/templates/Plugin/_rest-resource/rest-resource.twig b/templates/Plugin/_rest-resource/rest-resource.twig index 5b5b92bb8..3e71ffdc2 100644 --- a/templates/Plugin/_rest-resource/rest-resource.twig +++ b/templates/Plugin/_rest-resource/rest-resource.twig @@ -6,6 +6,8 @@ namespace Drupal\{{ machine_name }}\Plugin\rest\resource; use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; use Drupal\Core\KeyValueStore\KeyValueStoreInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\rest\Attribute\RestResource; use Drupal\rest\ModifiedResourceResponse; use Drupal\rest\Plugin\ResourceBase; use Drupal\rest\ResourceResponse; @@ -17,15 +19,6 @@ use Symfony\Component\Routing\Route; /** * Represents {{ plugin_label }} records as resources. * - * @RestResource ( - * id = "{{ plugin_id }}", - * label = @Translation("{{ plugin_label }}"), - * uri_paths = { - * "canonical" = "/api/{{ plugin_id|u2h }}/{id}", - * "create" = "/api/{{ plugin_id|u2h }}" - * } - * ) - * * @DCG * The plugin exposes key-value records as REST resources. In order to enable it * import the resource configuration into active configuration storage. An @@ -48,6 +41,14 @@ use Symfony\Component\Routing\Route; * Drupal core. * @see \Drupal\rest\Plugin\rest\resource\EntityResource */ +#[RestResource( + id: '{{ plugin_id }}', + label: new TranslatableMarkup('{{ plugin_label }}'), + uri_paths: [ + 'canonical' => '/api/{{ plugin_id|u2h }}/{id}', + 'create' => '/api/{{ plugin_id|u2h }}', + ], +)] final class {{ class }} extends ResourceBase { /** diff --git a/tests/functional/Generator/Plugin/_rest_resource/src/Plugin/rest/resource/FooResource.php b/tests/functional/Generator/Plugin/_rest_resource/src/Plugin/rest/resource/FooResource.php index bdba39bed..be438ef66 100644 --- a/tests/functional/Generator/Plugin/_rest_resource/src/Plugin/rest/resource/FooResource.php +++ b/tests/functional/Generator/Plugin/_rest_resource/src/Plugin/rest/resource/FooResource.php @@ -6,6 +6,8 @@ use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; use Drupal\Core\KeyValueStore\KeyValueStoreInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\rest\Attribute\RestResource; use Drupal\rest\ModifiedResourceResponse; use Drupal\rest\Plugin\ResourceBase; use Drupal\rest\ResourceResponse; @@ -17,15 +19,6 @@ /** * Represents Foo records as resources. * - * @RestResource ( - * id = "example_foo", - * label = @Translation("Foo"), - * uri_paths = { - * "canonical" = "/api/example-foo/{id}", - * "create" = "/api/example-foo" - * } - * ) - * * @DCG * The plugin exposes key-value records as REST resources. In order to enable it * import the resource configuration into active configuration storage. An @@ -48,6 +41,14 @@ * Drupal core. * @see \Drupal\rest\Plugin\rest\resource\EntityResource */ +#[RestResource( + id: 'example_foo', + label: new TranslatableMarkup('Foo'), + uri_paths: [ + 'canonical' => '/api/example-foo/{id}', + 'create' => '/api/example-foo', + ], +)] final class FooResource extends ResourceBase { /**