Incorrect annotation to attribute conversion for plugins with a single constructor annotation

Created on 28 August 2024, 5 months ago

Drupal version

10.3

Drupal Rector version

0.20.3

Problem / Motivation

Following the instructions on https://github.com/palantirnet/drupal-rector/blob/main/docs/core_plugin_... does not yield correct conversions for plugin annotations that use a single constructor.
For example: ViewsField
Annotation: @ViewsField("my_custom_views_field")
Converted Attribute: #[\Drupal\views\Attribute\ViewsArea] - This is missing the id argument for the attribute.

πŸ› Bug report
Status

Needs review

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States recrit

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

Comments & Activities

  • Issue created by @recrit
  • Status changed to Needs review 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States recrit

    Proposed Solution:
    Update "drupal-rector/src/Drupal10/Rector/Deprecation/AnnotationToAttributeRector.php" with the following to detect single argument plugins with a missing key.
    The patch attached for palantirnet/drupal-rector applies the following update:

        /**
         * @param array|ArrayItemNode[] $parsedArgs
         *
         * @return Attribute
         */
        private function createAttribute(string $attributeClass, array $parsedArgs): Attribute
        {
            $fullyQualified = new FullyQualified($attributeClass);
            // Support attributes with a single value constructor.
            // For example: '@ViewsField("my_custom_views_field")'.
            if (count($parsedArgs) === 1 && empty($parsedArgs[0]->key)) {
              $parsedArgs[0]->key = 'id';
            }
    
            $args = [];
           ...
    
Production build 0.71.5 2024