Allow multiple annotations per class

Created on 20 April 2018, about 6 years ago
Updated 30 January 2023, over 1 year ago

Problem/Motivation

In the new JSON-RPC module, we would like to define RPC actions as plugins. It would be useful to share common behavior, especially dependency injection and access callbacks, between these methods.

Take a simplified 'user permissions' RPC service example:

/**
 * @JsonRpcMethod(
 *   id = "permissions.get",
 *   access_method = "checkAccess",
 * )
 *
 * @JsonRpcMethod(
 *   id = "permissions.add",
 *   access_method = "checkAccess",
 * . params = {
 * .   "permission" = "string",
 * .   "role" = "string",
 *   }
 * )
class UserPermissions {
  protected $permissionsHandler;
  public function __construct(PermisionHandlerInterface $permission_handler) {}
  public function get() {}
  public function add($permission, $role) {}
  public function checkAccess(AccountInterface $account) {}
}

Since methods are created via plugin annotations, it would be useful to have multiple annotations per class.

This is an existing feature of the underlying libraries.

Proposed resolution

Fix Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery to recognize all annotations of a particular type on a class, not just the first one found.

API changes

One class can now satisfy multiple plugins.

Data model changes

None.

✨ Feature request
Status

Needs work

Version

10.1 ✨

Component
PluginΒ  β†’

Last updated about 15 hours ago

Created by

πŸ‡«πŸ‡·France gabesullice πŸ‡«πŸ‡· β‘ β‘’ UTC+2

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

Production build 0.69.0 2024