Skip to content

Commit

Permalink
Merge pull request #160 from Chi-teck/php-attribute-rest-resource
Browse files Browse the repository at this point in the history
Use PHP attribute for RestResource plugin.
  • Loading branch information
Chi-teck committed May 21, 2024
2 parents 0395af0 + dde2f75 commit 32beb00
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
19 changes: 10 additions & 9 deletions templates/Plugin/_rest-resource/rest-resource.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 {

/**
Expand Down

0 comments on commit 32beb00

Please sign in to comment.