Verified Commit d17c8302 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420994 by kim.pepper: Convert RestResource plugin discovery to attributes

parent 636182e6
Loading
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
namespace Drupal\dblog\Plugin\rest\resource;

use Drupal\Core\Database\Database;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rest\Attribute\RestResource;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -10,15 +12,14 @@

/**
 * Provides a resource for database watchdog log entries.
 *
 * @RestResource(
 *   id = "dblog",
 *   label = @Translation("Watchdog database log"),
 *   uri_paths = {
 *     "canonical" = "/dblog/{id}"
 *   }
 * )
 */
#[RestResource(
  id: "dblog",
  label: new TranslatableMarkup("Watchdog database log"),
  uri_paths: [
    "canonical" => "/dblog/{id}",
  ]
)]
class DbLogResource extends ResourceBase {

  /**
+10 −9
Original line number Diff line number Diff line
@@ -12,12 +12,14 @@
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Utility\Token;
use Drupal\file\Entity\File;
use Drupal\file\Upload\ContentDispositionFilenameParser;
use Drupal\file\Upload\InputStreamFileWriterInterface;
use Drupal\file\Validation\FileValidatorInterface;
use Drupal\file\Validation\FileValidatorSettingsTrait;
use Drupal\rest\Attribute\RestResource;
use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\Plugin\rest\resource\EntityResourceValidationTrait;
@@ -45,16 +47,15 @@
 *     to be later moved when they are referenced from a file field.
 *   - Permission to upload a file can be determined by a users field level
 *     create access to the file field.
 *
 * @RestResource(
 *   id = "file:upload",
 *   label = @Translation("File Upload"),
 *   serialization_class = "Drupal\file\Entity\File",
 *   uri_paths = {
 *     "create" = "/file/upload/{entity_type_id}/{bundle}/{field_name}"
 *   }
 * )
 */
#[RestResource(
  id: "file:upload",
  label: new TranslatableMarkup("File Upload"),
  serialization_class: File::class,
  uri_paths: [
    "create" => "/file/upload/{entity_type_id}/{bundle}/{field_name}",
  ]
)]
class FileUploadResource extends ResourceBase {

  use FileValidatorSettingsTrait;
+54 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\rest\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a REST resource attribute object.
 *
 * Plugin Namespace: Plugin\rest\resource
 *
 * For a working example, see \Drupal\dblog\Plugin\rest\resource\DbLogResource
 *
 * @see \Drupal\rest\Plugin\Type\ResourcePluginManager
 * @see \Drupal\rest\Plugin\ResourceBase
 * @see \Drupal\rest\Plugin\ResourceInterface
 * @see plugin_api
 *
 * @ingroup third_party
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class RestResource extends Plugin {

  /**
   * Constructs a RestResource attribute.
   *
   * @param string $id
   *   The REST resource plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
   *   The human-readable name of the REST resource plugin.
   * @param string|null $serialization_class
   *   (optional) The serialization class to deserialize serialized data into.
   * @param string|null $deriver
   *   (optional) The URI paths that this REST resource plugin provides.
   *   - key: The link relation type plugin ID.
   *   - value: The URL template.
   * @param array $uri_paths
   *   (optional) The deriver class for the rest resource.
   *
   * @see \Symfony\Component\Serializer\SerializerInterface
   * @see core/core.link_relation_types.yml
   */
  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $label,
    public readonly ?string $serialization_class = NULL,
    public readonly ?string $deriver = NULL,
    public readonly array $uri_paths = [],
  ) {}

}
+10 −1
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\rest\Attribute\RestResource;
use Drupal\rest\Plugin\ResourceInterface;

/**
 * Manages discovery and instantiation of resource plugins.
@@ -28,7 +30,14 @@ class ResourcePluginManager extends DefaultPluginManager {
   *   The module handler to invoke the alter hook with.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/rest/resource', $namespaces, $module_handler, 'Drupal\rest\Plugin\ResourceInterface', 'Drupal\rest\Annotation\RestResource');
    parent::__construct(
      'Plugin/rest/resource',
      $namespaces,
      $module_handler,
      ResourceInterface::class,
      RestResource::class,
      'Drupal\rest\Annotation\RestResource',
    );

    $this->setCacheBackend($cache_backend, 'rest_plugins');
    $this->alterInfo('rest_resource');
+13 −11
Original line number Diff line number Diff line
@@ -14,6 +14,9 @@
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Routing\AccessAwareRouterInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rest\Attribute\RestResource;
use Drupal\rest\Plugin\Deriver\EntityDeriver;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Psr\Log\LoggerInterface;
@@ -29,18 +32,17 @@
 * Represents entities as resources.
 *
 * @see \Drupal\rest\Plugin\Deriver\EntityDeriver
 *
 * @RestResource(
 *   id = "entity",
 *   label = @Translation("Entity"),
 *   serialization_class = "Drupal\Core\Entity\Entity",
 *   deriver = "Drupal\rest\Plugin\Deriver\EntityDeriver",
 *   uri_paths = {
 *     "canonical" = "/entity/{entity_type}/{entity}",
 *     "create" = "/entity/{entity_type}"
 *   }
 * )
 */
#[RestResource(
  id: "entity",
  label: new TranslatableMarkup("Entity"),
  serialization_class: "Drupal\Core\Entity\Entity",
  deriver: EntityDeriver::class,
  uri_paths: [
    "canonical" => "/entity/{entity_type}/{entity}",
    "create" => "/entity/{entity_type}",
  ],
)]
class EntityResource extends ResourceBase implements DependentPluginInterface {

  use EntityResourceValidationTrait;
Loading