Skip to content

Drupal form and render callbacks - #565

Merged
AJenbo merged 6 commits into
PHPantom-dev:mainfrom
giuseppe-esposito-87:drupal-dr11
Nov 12, 2026
Merged

AJenbo merged 6 commits into
PHPantom-dev:mainfrom
giuseppe-esposito-87:drupal-dr11

Conversation

@giuseppe-esposito-87

Copy link
Copy Markdown
Contributor

🤖 This was written by an AI agent on behalf of @giuseppe-esposito-87 (claude-code/claude-opus-5-5).

Adds DR11: a Drupal form or render callback written as a string navigates to the method or function it names, completes, and is checked.

$form['#validate'][] = '::validateName';                         // the form's method
'#value_callback' => static::class . '::trimName',               // read as one 'Class::method'
'#ajax' => ['callback' => '::refreshGreeting'],
$form['#after_build'] = ['Drupal\acme\Form\GreetingForm::addHint'];
'#submit' => ['::submitForm', 'acme_greeting_form_submit'],      // a global function
'#lazy_builder' => ['acme.greeter:buildGreeting', [$name]],      // a service's method
  • Where. The list keys (#submit, #validate, #element_validate, #process, #after_build, #entity_builders, #pre_render, #post_render), #value_callback, #access_callback, the #ajax callback and the first item of #lazy_builder, in Drupal projects only.
  • Callback shapes. '::method' names a method of the form when the code sits in a form class or a trait. 'Class::method', X::class . '::method' and 'service:method' name a method of that class or service, and a string without : names a global function. Class and function names in these strings are fully qualified, as Drupal reads them at run time.
  • Features. Go to definition, hover, Find References and rename, which rewrites only the name inside the string. Completion after '::, 'Class:: and 'service:, and function names elsewhere.
  • Warnings.
    • An unknown method, function, class or service is reported.
    • A private or protected method is reported too, since Drupal calls it from outside the class.
    • A new invalid_drupal_callback warning reports a callback Drupal cannot call: '::method' in a render callback, an array callable that names a non-static method by its class, or a value callback that is not a native callable (Drupal ignores it).

On Drupal core 11.4, analyze reports 5 new warnings, all real: 3 in the *.api.php examples and 2 in a big_pipe test module that uses broken callbacks on purpose.

Five follow-ups are filed in the roadmap:

  • '::method' in a form alter hook (DR32);
  • render callbacks that are not trusted (DR33);
  • callback parameter checks (DR34);
  • invokable classes and services as callbacks (DR35);
  • array callables that name their class in a string (DR36).

How to test: run composer install in examples/drupal, open examples/drupal with this branch's build, then open web/modules/custom/acme/src/Form/GreetingForm.php and follow its "Try:" comments:

  • Go to the definition of validateName in '::validateName', and of acme_greeting_form_submit (it lands in acme.module).
  • Rename validateName(): the '::validateName' string follows.
  • Misspell '::validateName', or make the method private, to see the warning.
  • In src/Controller/GreetingController.php, go to the definition of buildGreeting and of acme.greeter in 'acme.greeter:buildGreeting'.

Checklist

If applicable:

  • I have updated CHANGELOG.md
  • I have updated the documentation (README.md, docs/, examples/)
  • I have updated the config schema (config-schema.json). Not applicable: this PR adds or changes no setting.
  • I have added/updated tests to cover my changes
  • If I used AI tools, I have reviewed what they produced and can explain
    the changes and why they are needed.
  • I have read through any AI-generated text, especially documentation,
    the changelog, and other user-facing content, and removed or corrected
    anything inaccurate, unclear, or unnecessary.

@codecov-commenter

codecov-commenter commented Nov 11, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 97.64454% with 22 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/definition/type_definition.rs 0.00% 6 Missing ⚠️
src/completion/drupal_callbacks.rs 97.58% 5 Missing ⚠️
src/diagnostics/drupal_callbacks.rs 95.93% 5 Missing ⚠️
src/symbol_map/extraction/drupal.rs 98.76% 4 Missing ⚠️
src/drupal/services.rs 96.42% 1 Missing ⚠️
src/highlight/mod.rs 97.50% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

giuseppe-esposito-87 and others added 6 commits November 12, 2026 09:05
In a Drupal project, a callback written as a string in a form or render
array's callback keys (#submit, #validate, #process, #pre_render, the
#ajax callback and the others Drupal calls) now leads to the method or
function it names: go-to-definition, hover, find references and rename.
'::method' names a method of the form it is written in, or of the trait;
'Class::method' and a class name joined to '::method' name a method of
that class, the class name in the string read as fully qualified; a name
without a colon names a global function. The playground's acme module
has a form with a callback of each shape.

Assisted-by: claude-code/claude-opus-5-5
In a Drupal project, a 'service:method' string in a form or render
array's callback keys now leads to the method of the service's class:
go-to-definition, hover, find references and rename, an alias leading to
its target's class. The service part navigates to the service like any
other service ID. The playground's greeting card has a lazy builder
that names a method of the acme.greeter service.

Assisted-by: claude-code/claude-opus-5-5
In a Drupal project, a string in a form or render array's callback keys
now completes what it can name while it is typed: after '::', the public
methods of the form it is written in, or of the trait; after
'Class::', the public methods of that class, the class name read as
fully qualified; after 'service:', the public methods of the service's
class; and before any colon, function names, written as their fully
qualified names. A class name joined to '::' completes that class's
methods. '::' completes nothing under the render callback keys, where
Drupal does not call it on the form, and a value callback is offered
only static methods and functions. The Drupal completion benchmark has a
function and a method callback case.

Assisted-by: claude-code/claude-opus-5-5
In a Drupal project, a callback in a form or render array's callback
keys that cannot work is now reported. A callback naming an unknown
method, function or class gets the usual unknown-member, unknown-function
or unknown-class warning, array callables included, while array
callables outside callback keys stay unchecked. A callback naming a
private or protected method is reported as one Drupal cannot call from
outside the class. '::method' in a render callback, which Drupal cannot
resolve, and a value callback that is not a native callable, which
Drupal ignores, get the new invalid_drupal_callback warning. '::method'
outside a form, a method a trait lacks, and a name that is a class or a
service stay silent. The playground's greeting form says how to see the
warnings.

Assisted-by: claude-code/claude-opus-5-5
Assisted-by: claude-code/claude-sonnet-5-5
The unknown-service check and the Drupal callback checks now ask the same
DrupalServiceIds whether a site may declare a service, instead of each
reading the declared IDs and the PHP-defined ones separately.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AJenbo
AJenbo merged commit ec06501 into PHPantom-dev:main Nov 12, 2026
15 checks passed
@AJenbo

AJenbo commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Thanks

@giuseppe-esposito-87
giuseppe-esposito-87 deleted the drupal-dr11 branch November 16, 2026 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants