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
.
Needs work
11.0 π₯
Last updated
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.