AnnotationToAttributeRector not working OOTB with SectionStorage plugins

Created on 29 August 2025, 17 days ago

Drupal version

11.2.x

Drupal Rector version

HEAD

Problem / Motivation

AnnotationToAttributeRector not working OOTB with SectionStorage plugins.

Not handling e.g. @ContextDefinition.

The good news is that this is exactly as @Translation.

🐛 Bug report
Status

Active

Component

Code

Created by

🇪🇸Spain penyaskito Seville 💃, Spain 🇪🇸, UTC+2 🇪🇺

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

Comments & Activities

  • Issue created by @penyaskito
  • 🇪🇸Spain penyaskito Seville 💃, Spain 🇪🇸, UTC+2 🇪🇺

    In \DrupalRector\Drupal10\Rector\Deprecation\AnnotationToAttributeRector

    \DrupalRector\Drupal10\Rector\Deprecation\AnnotationToAttributeRector::convertAnnotation

    Add

                '@ContextDefinition' => $this->convertContextDefinitionAnnotation($value),
    

    then

        public function convertContextDefinitionAnnotation(DoctrineAnnotationTagValueNode $value): ?Node\Expr\New_
        {
            // Check the annotation type, this will be helpful later.
            if ($value->identifierTypeNode->name !== '@ContextDefinition') {
                return null;
            }
    
            $valueArg = null;
            $argumentArg = null;
            $contextArg = null;
    
            // Loop through the values of the annotation, just to make 100% sure we have the correct argument order
            foreach ($value->values as $translateValue) {
                if ($translateValue->key === null) {
                    $valueArg = $this->nodeFactory->createArg($translateValue->value->value);
                }
                if ($translateValue->key === 'context') {
                    $contextArg = $this->nodeFactory->createArg(['context' => $translateValue->value->value]);
                }
                if ($translateValue->key === 'arguments') {
                    $argumentArg = [];
                    foreach ($translateValue->value->values as $argumentValue) {
                        $argumentArg[$argumentValue->key->value] = $argumentValue->value->value;
                    }
                    $argumentArg = $this->nodeFactory->createArg($argumentArg);
                }
            }
    
            $argArray = [];
            if ($valueArg !== null) {
                $argArray[] = $valueArg;
            }
            if ($argumentArg !== null) {
                $argArray[] = $argumentArg;
            }
            if ($contextArg !== null) {
                $argArray[] = $contextArg;
            }
    
            return new Node\Expr\New_(new Node\Name('\Drupal\Core\Plugin\Context\ContextDefinition'), $argArray);
        }
    
    
  • 🇪🇸Spain penyaskito Seville 💃, Spain 🇪🇸, UTC+2 🇪🇺

    ^ Change above doesn't add the \Drupal\Core\Plugin\Context\ContextDefinition usage though :-(

  • 🇪🇸Spain penyaskito Seville 💃, Spain 🇪🇸, UTC+2 🇪🇺

    ah, we need to inspect the context definition. e.g. if it's "entity:x", needs to be a EntityContextDefinition, and then omit the "entity:" in the constructor. So it's not as straightforward as I thought.

Production build 0.71.5 2024