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

Command icon 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

fgm created an issue. See original summary.

fgm’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

alex.mazaltov made their first commit to this issue’s fork.

alex.mazaltov’s picture

Status: Active » Needs review
alex.mazaltov’s picture

Status: Needs review » Reviewed & tested by the community
avpaderno’s picture

Status: Reviewed & tested by the community » Needs review
alex.mazaltov’s picture

Issue tags: +issue-with-fork
fgm’s picture

Status: Needs review » Needs work

Seems 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.

Mixologic’s picture

So, 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.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

fgm’s picture

Issue summary: View changes
Issue tags: -

Updated return types for D10.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.