TLDR
Introduce syntactical sugar that makes string literals clickable in an IDE, if they refer to callables.
Background / history
In PHP and especially in Drupal, we often have string literals that correspond to actual functions:
E.g. a form builder function, a page callback in hook_menu(), etc.
A typical IDE (e.g. PhpStorm) can make such string literals clickable, and include them in "find usages", if they are in some way annotated as callable.
Problem
In typical Drupal code, a lot of string literals that do refer to callables are not clickable.
Solution / Proposed change
Introduce syntactic sugar to make them clickable.
Renderkit is a big enough module to justify having its own class for this.
Proposed API
Introduce a class with static methods, where the method parameters are annotated as 'callable'.
Class name and method name must be as short as possible, to minimize distraction.
This syntax should only be used in places where a minor indirection overhead does not matter.
use Drupal\renderkit\R;
function mymodule_menu() {
// Clickable wildcard loader '%node'.
$items['node/' . R::w('node_load') . '/mymodule'] = [
// Clickable page callback.
'page callback' => R::f('mymodule_node_page'),
];
}
Risks / Downsides
We might decide some day that this was a bad idea and awkward.
In this case we shall stop using the class, and perhaps mark it as deprecated.
This can be done with no disruption whatsoever.
The only cost is we are polluting an IDE's autocomplete list with a single-letter class.
(technically this is fine, the class is in a namespace)
Comments
Comment #2
donquixote commentedComment #3
donquixote commented