Skip to content

Commit 32beb00

Browse files
authored
Merge pull request #160 from Chi-teck/php-attribute-rest-resource
Use PHP attribute for RestResource plugin.
2 parents 0395af0 + dde2f75 commit 32beb00

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

templates/Plugin/_rest-resource/rest-resource.twig

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Drupal\{{ machine_name }}\Plugin\rest\resource;
66
77
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
88
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
9+
use Drupal\Core\StringTranslation\TranslatableMarkup;
10+
use Drupal\rest\Attribute\RestResource;
911
use Drupal\rest\ModifiedResourceResponse;
1012
use Drupal\rest\Plugin\ResourceBase;
1113
use Drupal\rest\ResourceResponse;
@@ -17,15 +19,6 @@ use Symfony\Component\Routing\Route;
1719
/**
1820
* Represents {{ plugin_label }} records as resources.
1921
*
20-
* @RestResource (
21-
* id = "{{ plugin_id }}",
22-
* label = @Translation("{{ plugin_label }}"),
23-
* uri_paths = {
24-
* "canonical" = "/api/{{ plugin_id|u2h }}/{id}",
25-
* "create" = "/api/{{ plugin_id|u2h }}"
26-
* }
27-
* )
28-
*
2922
* @DCG
3023
* The plugin exposes key-value records as REST resources. In order to enable it
3124
* import the resource configuration into active configuration storage. An
@@ -48,6 +41,14 @@ use Symfony\Component\Routing\Route;
4841
* Drupal core.
4942
* @see \Drupal\rest\Plugin\rest\resource\EntityResource
5043
*/
44+
#[RestResource(
45+
id: '{{ plugin_id }}',
46+
label: new TranslatableMarkup('{{ plugin_label }}'),
47+
uri_paths: [
48+
'canonical' => '/api/{{ plugin_id|u2h }}/{id}',
49+
'create' => '/api/{{ plugin_id|u2h }}',
50+
],
51+
)]
5152
final class {{ class }} extends ResourceBase {
5253
5354
/**

tests/functional/Generator/Plugin/_rest_resource/src/Plugin/rest/resource/FooResource.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
88
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
9+
use Drupal\Core\StringTranslation\TranslatableMarkup;
10+
use Drupal\rest\Attribute\RestResource;
911
use Drupal\rest\ModifiedResourceResponse;
1012
use Drupal\rest\Plugin\ResourceBase;
1113
use Drupal\rest\ResourceResponse;
@@ -17,15 +19,6 @@
1719
/**
1820
* Represents Foo records as resources.
1921
*
20-
* @RestResource (
21-
* id = "example_foo",
22-
* label = @Translation("Foo"),
23-
* uri_paths = {
24-
* "canonical" = "/api/example-foo/{id}",
25-
* "create" = "/api/example-foo"
26-
* }
27-
* )
28-
*
2922
* @DCG
3023
* The plugin exposes key-value records as REST resources. In order to enable it
3124
* import the resource configuration into active configuration storage. An
@@ -48,6 +41,14 @@
4841
* Drupal core.
4942
* @see \Drupal\rest\Plugin\rest\resource\EntityResource
5043
*/
44+
#[RestResource(
45+
id: 'example_foo',
46+
label: new TranslatableMarkup('Foo'),
47+
uri_paths: [
48+
'canonical' => '/api/example-foo/{id}',
49+
'create' => '/api/example-foo',
50+
],
51+
)]
5152
final class FooResource extends ResourceBase {
5253

5354
/**

0 commit comments

Comments
 (0)