Allow plugin definition to be aliased

Created on 11 August 2024, 6 months ago
Updated 7 September 2024, 5 months ago

Problem/Motivation

Working with plugin_id is a not very good DX. There are issues to change the plugin_id to use constant/class name.

Steps to reproduce

Proposed resolution

I propose a different approach, instead of using the class name/constant as plugin ID (which will be a problem for derivative definitions), we can introduce plugin alias, just like service container can have service alias.

  • Add a method to get all aliases for definitions.
  • Looks for alias when calling \Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition and \Drupal\Component\Plugin\Factory\FactoryInterface::createInstance

This will allow getting the definition by the alias, something like this:

\Drupal::entityTypeManager()->getStorage(\Drupal\node\Entity\Node::class);
\Drupal::entityTypeManager()->getDefinition(\Drupal\node\Entity\Node::class);

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

โœจ Feature request
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Pluginย  โ†’

Last updated 1 day ago

Created by

๐Ÿ‡ฎ๐Ÿ‡ฉIndonesia el7cosmos ๐Ÿ‡ฎ๐Ÿ‡ฉ GMT+7

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @el7cosmos
  • ๐Ÿ‡ฎ๐Ÿ‡ฉIndonesia el7cosmos ๐Ÿ‡ฎ๐Ÿ‡ฉ GMT+7
  • ๐Ÿ‡ฎ๐Ÿ‡ฉIndonesia el7cosmos ๐Ÿ‡ฎ๐Ÿ‡ฉ GMT+7
  • ๐Ÿ‡ฎ๐Ÿ‡ฉIndonesia el7cosmos ๐Ÿ‡ฎ๐Ÿ‡ฉ GMT+7
  • Merge request !9166Allow plugin definition to be aliased โ†’ (Open) created by el7cosmos
  • Pipeline finished with Failed
    6 months ago
    Total: 683s
    #250669
  • Pipeline finished with Failed
    6 months ago
    Total: 616s
    #250800
  • Pipeline finished with Failed
    6 months ago
    Total: 459s
    #251103
  • Pipeline finished with Failed
    6 months ago
    Total: 950s
    #253867
  • Pipeline finished with Failed
    6 months ago
    Total: 497s
    #255970
  • Pipeline finished with Failed
    6 months ago
    Total: 593s
    #255987
  • Pipeline finished with Success
    6 months ago
    Total: 3654s
    #255999
  • ๐Ÿ‡ฎ๐Ÿ‡ฉIndonesia el7cosmos ๐Ÿ‡ฎ๐Ÿ‡ฉ GMT+7

    Adding aliases directly to the definitions causes problem with various plugin manager as they need some properties of the definitions when processing it. Changing the IS for a different approach.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Charlie ChX Negyesi ๐ŸCanada

    It's great to see in code how this could look.

    However, as far as I can see none of the weirdness raised in the parent are solved: when requesting a plugin by class you might get an object of a different class. Either because of an alter or because the module author deprecated the original class and created a new one but put the old class name in the class property of the definition. At this point the FQCN is no longer a class name just a different but sometimes confusing unique string.

    So perhaps this should be postponed on parent until there's agreement whether that's acceptable?

  • ๐Ÿ‡ฎ๐Ÿ‡ฉIndonesia el7cosmos ๐Ÿ‡ฎ๐Ÿ‡ฉ GMT+7

    I don't think the related issue is a parent of this, the discussion is more about the plugin ID itself. But in this, we keep the plugin ID as it is, and introduce an alias, which is new, and we can set a new convention around it without worrying about backward compatibility.

    As for the FQCN, using FQCN as a method/function argument doesn't necessarily mean we are requesting an instance of that class. If we take a look at this:

    \Drupal::entityTypeManager()->getStorage(\Drupal\node\Entity\Node::class);
    

    We don't want an instance of that class, we want something else.

    Using FQCN as an argument means better IDE support which will then improve DX.

  • ๐Ÿ‡ญ๐Ÿ‡บHungary mxr576 Hungary

    This will allow getting the definition by the alias, something like this:

    \Drupal::entityTypeManager()->getStorage(\Drupal\node\Entity\Node::class);
    \Drupal::entityTypeManager()->getDefinition(\Drupal\node\Entity\Node::class);
    

    I think

    \Drupal::entityTypeManager()->getStorage(\Drupal\node\NodeInterface::class);
    \Drupal::entityTypeManager()->getDefinition(\Drupal\node\NodeInterface::class);

    would be better, because it would not tie the configuration/database to an actual implementation but a contract that these implementations MUST implement.

    Yes, this relies on the assumption that a plugin always implement a dedicated contract and we know that not every plugins have these kinds of contracts available... which maybe only means that this aliasing feature could only support those that have these. (This limitation was also raised in a Slack thread by @joachim IIRC).

  • ๐Ÿ‡ฎ๐Ÿ‡ฉIndonesia el7cosmos ๐Ÿ‡ฎ๐Ÿ‡ฉ GMT+7

    Most plugins already have a common interface. An interface for each plugin seems too verbose for most and probably ends up being an empty interface.

    From what I understand, the concern was raised because there is no way yet to deprecate a plugin and this is an issue for FQCN as plugin ID, not as alias. Alias definition will still have the same plugin ID (eg Entity type with alias Node::class will still have a definition with ID node). Plugin ID will still end up in configurations, alias will always used in code and never in configurations. When a plugin class is deprecated this won't be a problem as long as the class is tagged as @deprecated.

Production build 0.71.5 2024