[policy, no patch] Decide on a namespace for PHP Attribute classes

Created on 24 February 2023, over 1 year ago
Updated 8 March 2023, over 1 year ago

Problem/Motivation

PHP 8 introduces attributes, and there are several issues working on using PHP 8 attributes in Drupal -- for routes, for plugins, for test output, etc.

Attributes require an attribute class, similar to how our existing Doctrine annotations work.

It would be good to first standardize on the namespace for attribute classes, so that the various core issues and contrib code can all build these the same way.

Steps to reproduce

Proposed resolution

Broadly two options have been discussed in πŸ“Œ Use PHP attributes instead of doctrine annotations Fixed

  1. Drupal\Component\Attribute
    Drupal\my_module\Attribute
    

    This requires πŸ“Œ Move Attribute classes under Drupal\Component Needs review to use Drupal\Component\Html\Attribute to avoid namespace collision.

  2. Drupal\Component\PhpAttribute
    Drupal\my_module\PhpAttribute
    

    This prefixes the namespace with PHP, which ensures there's never a collision with HTML Attribute classes - note that we would probably still do Drupal\Component\Html\Attribute for HTML attributes regardless of which option we decide on here.

  3. Drupal\[Component/Core]\COMPONENT\Attribute
    Drupal\MODULE\Attribute
    

    Attributes don't exist outside of a system so for example plugin attributes should use: Drupal\Component\Plugin\Attribute

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Fixed

Version

10.1 ✨

Component
BaseΒ  β†’

Last updated about 3 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom joachim

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

Comments & Activities

  • Issue created by @joachim
  • πŸ‡¬πŸ‡§United Kingdom catch
  • πŸ‡«πŸ‡·France andypost

    Option 2 looks good.

    And related issues still makes sense

  • πŸ‡¬πŸ‡§United Kingdom joachim

    > Drupal\Component\Attribute

    Does that mean that the EntityType attribute class, for instance, would be \Drupal\Component\Attribute\EntityType, rather than \Drupal\Core\Attribute\EntityType? That seems a bit weird.

    Also, \Drupal\Component\Foo are meant to be components which are reusable outside of Drupal. I don't think there's anything especially reusable about a bunch of attribute classes.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Correct me if I am wrong but this is setting a coding standard so should be in Coding Standards project.

  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    I definitely prefer the first option. Adding Php as a namespace component seems rather strange to me. It’s all PHP, so why should this specific set of classes get an explicit Php namespace?

    If we consider the potential for confusion with other kinds of attributes as too great, I would go for

    Drupal\Component\PhpAttribute
    Drupal\my_module\PhpAttribute
    

    instead. We want to make clear that it’s a β€œPHP attribute”, not a β€œPHP” that is also an β€œAttribute”. As I don’t think we’d ever put a class into the Drupal\Component\Php namespace, it shouldn’t be a separate namespace.

  • πŸ‡¬πŸ‡§United Kingdom catch

    \Drupal\Component\Attribute\EntityType, rather than \Drupal\Core\Attribute\EntityType?

    No I think it would be Drupal\Core\Entity\Attribute\EntityType or Drupal\Core\Entity\PhpAttribute\EntityType

    For me, Drupal\Core\Entity\Attribute\EntityType is a lot neater and I don't think there'll be any confusing with HTML attributes for the specific implementations.

    I've updated the issue summary to change from Php\Attribute to PhpAttribute - that was a typo on my part..

    @quietone I don't think this is a coding standards issue, it's just deciding on class naming/namespacing for new core code, so moving it back to the core queue.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    > No I think it would be Drupal\Core\Entity\Attribute\EntityType or Drupal\Core\Entity\PhpAttribute\EntityType

    Ah ok. That makes more sense! :)

    Can I change the IS to make it clearer that "Drupal\Component\Attribute" means "Drupal\*COMPONENT*\Attribute"? It's confusing as it is because Drupal\Component is an actual real namespace.

  • πŸ‡¬πŸ‡§United Kingdom catch

    @joachim there is going to be some code in Drupal\Component\Attribute, so it's really both not either/or.

  • πŸ‡«πŸ‡·France andypost

    Not sure we need to disturb Drupal\Component namespace at all of Attributes will be placed in related subsystems

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    I think option 1 is fine and I disagree that πŸ“Œ Move Attribute classes under Drupal\Component Needs review is need for us to make this decision. I think it is okay that \Drupal\Core\Template\Attribute is not a PHP attribute. Twig blocks are not Drupal blocks. HTML entities are not Drupal entities. A Twig node is not a Drupal node.

    And for the ultimate one... Drupal\Core\Condition !== \Drupal\Core\Config\Entity\Query\Condition !== \Drupal\Core\Database\Query\Condition

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    +1 for option 1 - I agree with everything @alexpott said, and Symfony has a similar pattern so to avoid adding a new Drupalism I think we should follow Symfony here.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Side note, don't want to detail - what would go under Drupal\Component\Attribute?

  • πŸ‡¬πŸ‡§United Kingdom catch

    @joachim see the MR on the parent issue, for example https://git.drupalcode.org/project/drupal/-/merge_requests/1576/diffs#25...

    @alexpott, the original plan in πŸ“Œ Move Attribute classes under Drupal\Component Needs review was to put that code in Drupal\Component\Attribute - i.e. literally the same namespace and directory as being proposed here. It's not just a naming issue but actual namespace conflicts.

    We don't need to do that issue first or anything, but we do need to decide in both issues based on what's happening in the other. The latest version there using Drupal\Component\HtmlAttribute so there's no conflict now and I think we're fine, but that's only because of this discussion happening.

  • πŸ‡«πŸ‡·France andypost

    Plugins using Drupal\Component\Plugin\Discovery so AttributeClassDiscovery.php fits there but if we need to use it for controllers it should live in "core"

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    I think I made a mistake in πŸ“Œ Use PHP attributes instead of doctrine annotations Fixed - I don't think that should be create Drupal\Component\Attribute ... I think I made an Attribute component because I was copying the Annotation component. But looking at this more closely I think the Drupal\Component\Attribute in that issue should be Drupal\Component\Plugin\Attribute because that's what it is for. No I look at it, I think having a Drupal\Component\Attribute is odd - like what are the attributes for?!!? It's the same with Drupal\Component\Annotation - it looks generic but actually it is part of the plugin system.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Added Drupal\Component\Plugin\Attribute as option #3 in the issue summary, that works for me.

  • πŸ‡«πŸ‡·France andypost

    +1 to third as there will be no conflicts

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Option 3 doesn't cover everything -- other things which aren't plugins will need attributes too.

    It should be:

    - Drupal\[Component/Core]\COMPONENT\Attribute
    - Drupal\MODULE\Attribute

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    @joachim yep - I've updated option 3 to be more explicit and use your text.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Thanks!

    +1 to option 3.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Shall we say this is RTBC? Consensus seems to be option 3.

  • Status changed to Needs work over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    @joachim I think you accidentally reverted option 3 from the IS? But otherwise I move my vote to option 3 and agree this is RTBC as there seems to be no support for the other options.

  • Status changed to Active over 1 year ago
  • Status changed to RTBC over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom joachim

    Oops, sorry! I've reverted.

  • πŸ‡¬πŸ‡§United Kingdom catch

    I also like option #3.

  • Status changed to Fixed over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom catch

    πŸ“Œ Use PHP attributes instead of doctrine annotations Fixed is already following this, and we've got consensus here, so let's close this one out. Definitely easier in its own spin-off issue!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024