Currently our coding standards state ( https://www.drupal.org/docs/develop/standards/coding-standards#naming ) that variables should be either snake_case or camelCase, with the general trend and conventions in other environments going for camelCase.
However, although form state has been a typed object since 8.0, we still rely on the function argument receiving it being actually named $form_state and nothing else, including $formState, so we have this double validation on argument name (from the value resolver) and type (from the type hint in form methods) and do not respect our standard saying both are valid. This causes problems for code aligned on general camelCase naming.
It is actually a small change to stop relying on the parameter name, and we already do such an argument-type-based injection for CurrentRouteMatch, all it needs is adding a new resolver for form state, which is an argument used quite a bit more than the current route match. All the resolver has to do it check whether the argument is of the proper type and copy the request form_state to the the FormStateInterface argument, like this:
/* ...snip ... */
class FormStateValueResolver implements ArgumentValueResolverInterface {
const NAME_LEGACY = 'form_state';
public function supports(Request $request, ArgumentMetadata $argument): bool {
$argumentInterfaceMatches = $argument->getType() === FormStateInterface::class;
$requestAttributeExists = $request->attributes->has(static::NAME_LEGACY);
return $argumentInterfaceMatches || $requestAttributeExists;
}
public function resolve(Request $request, ArgumentMetadata $argument): iterable {
$formState = $request->attributes->has(static::NAME_LEGACY)
? $request->attributes->get(static::NAME_LEGACY)
: NULL;
yield $formState;
}
And add that argument_resolver.form_state as the next before last resolver on the second argument of the http_kernel.controller.argument_resolver service, as is done on #3005108: ConfirmForm parameter name should be form_state.
With that small change all forms can now support any way of naming form state, as long as it is typed to FormStateInterface.
Issue fork drupal-3006502
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
fgmComment #10
alex.mazaltovPlease review this MR?
Comment #11
alex.mazaltovPlease review this MR?
Comment #12
avpadernoComment #13
alex.mazaltovComment #14
fgmSeems there's a bug in the Gitlab integration: following the MR link to https://git.drupalcode.org/project/drupal/-/merge_requests/4#note_3417
goes to a MR which has nothing to do with the one in this issue. The "related" links on the right sidebar carry the same issue number 3006502 but are entirely unrelated with this issue.
Comment #16
MixologicSo, not totally a bug with the integration, just that this is what it looks like when somebody connects the wrong issue to a different workspace.
Its the equivalent of posting the wrong patch to an issue.
Comment #25
fgmUpdated return types for D10.