Problem/Motivation

ConfirmFormInterface::getFormName() is only called by ConfirmFormBase. The return value, a string, is used to build a hidden form element:

    $form[$this->getFormName()] = ['#type' => 'hidden', '#value' => 1];

The form value is not used by ConfirmFormBase's submit handler, although one subclass in core, UserMultipleCancelConfirm does check it.

However, because this form element uses the #value property, its value will ALWAYS be 1 according to the docs for Drupal\Core\Render\Element\Hidden:

 * - #value: The value of the form element. The Form API ensures that this
 *   value remains unchanged by the browser.

Therefore, AFAICT it doesn't serve any purpose and both the element and the ConfirmFormInterface::getFormName() could be deprecated.

Steps to reproduce

Proposed resolution

Remaining tasks

- Remove ConfirmFormInterface::getFormName().
- Deprecate implementations in core form classes. See how to deprecate for what needs to be done to add the deprecation.
- Remove the calls to getFormName() in core form classes.

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3497021

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

joachim created an issue. See original summary.

godotislate’s picture

Code archaeology seems to show this is cruft from this commit 19 years ago (#18329: Unify confirmation messages), and how confirm forms originally worked: https://git.drupalcode.org/project/drupal/-/blob/198ec98f756673da8c899bb0236a91808ed010ec/includes/theme.inc#L898

arunsahijpal’s picture

Working on it.

joachim’s picture

Status: Active » Needs work

Looks like a good start.

We also need to remove the condition check from UserMultipleCancelConfirm.

And I don't know what BC handling we need.

ConfirmFormInterface is what I call an inward-facing interface: 3rd parties are meant to implement it, not call it. So I don't think we need to deprecate getFormName() before removing it. But we might want to notify implementors that they can remove getFormName() from their class.

arunsahijpal’s picture

Status: Needs work » Needs review

Hi @joachim,
Yes, I think we can remove the if ($form_state->getValue('confirm')) condition from UserMultipleCancelConfirm::submitForm() as the confirm field is no longer relevant. Since ConfirmFormInterface is inward-facing, I agree that getFormName() can be removed without deprecation.

I've done the changes pls check.

Thanks
Arun

joachim’s picture

Status: Needs review » Needs work

Removing the if() in core/modules/user/src/Form/UserMultipleCancelConfirm.php means that the indentation needs to be changed too!

arunsahijpal’s picture

Status: Needs work » Needs review

Corrected the indentation.

joachim’s picture

Status: Needs review » Needs work

Nearly there -- needs a few coding style fixes.

arunsahijpal’s picture

Status: Needs work » Needs review
godotislate’s picture

Status: Needs review » Needs work

@arunsahijpal It may be helpful for you to make sure the MR build passes before setting to Needs Review. The build for the latest changes still fails.

See https://git.drupalcode.org/project/drupal/-/merge_requests/10785. You can review the build job log https://git.drupalcode.org/issue/drupal-3497021/-/jobs/3926956 to see why it is failing.

joachim’s picture

Looks like you've run it through a code formatter that's not configured for Drupal, as it's put all the { on new lines.

arunsahijpal’s picture

I've fixed the indentation issue but now a phpunit functional is failing
I think it is due to this Line of code as it should be this if ($form_state->getValue('confirm')) { because Form submissions in Drupal are processed through $form_state.

Should i change it?

godotislate’s picture

When making the change, you'd need to make sure the functionality is not affected. If it isn't, then the test needs to be updated as well. There are two test failures, so you'd also need to make sure that the necessary changes can be made and are applied for both.

arunsahijpal’s picture

@joachim, IDK why it is not finding ''confirm" key in SearchConfigSettingsFormTest could you pls check it, and the second SearchMultilingualEntityTest error is not due to the change made in getFormName() function.

joachim’s picture

Yup, looks like we need to remove

> if ($form['confirm']

in ReindexConfirm.

Which is really weird code anyway -- that's ALWAYS going to be in the form array!

niharika.s made their first commit to this issue’s fork.

joachim’s picture

@niharika.s You seem to have made a new branch and a new MR, neither of which are necessary. New branches are only needed when there's a completely different approach to fixing an issue.

If you want to work on this issue, you can make a commit to the existing branch.

Your change isn't quite right though, as it also needs the indentation fixing.

arunsahijpal’s picture

Status: Needs work » Needs review

@joachim,
I've updated the ReindexForm and the pipeline is also green now, pls check.

joachim’s picture

Status: Needs review » Needs work

There's stray whitespace being added in core/modules/search/tests/src/Functional/SearchMultilingualEntityTest.php.

Other than that, it's RTBC.

arunsahijpal’s picture

Status: Needs work » Needs review

Removed the whitespace.

joachim’s picture

Status: Needs review » Needs work

I'm not sure you've removed the right line -- the diff in the MR for core/modules/search/tests/src/Functional/SearchMultilingualEntityTest.php now shows one removed and one added.

It's a good idea to do a `git diff 11.x` locally before you commit and push.

arunsahijpal’s picture

@joachim , could you pls check now.

joachim changed the visibility of the branch 3497021- to hidden.

joachim’s picture

Status: Needs work » Reviewed & tested by the community

Looks good. Thanks!

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs review

This looks fine except for BC, which @joachim mentioned in #5. The first thing I did was search for getFormName and there 3 pages of results. This change would break at lease one of those module (I don't recall which). I then asked in committer slack and catch confirmed that this needs to have deprecations to prevent fatals in at least some of the found modules.

Setting back to NW.

smustgrave’s picture

Status: Needs review » Needs work
arunsahijpal’s picture

Hi @quietone and @joachim,
So should i revert my changes and add a deprecation comment instead?

joachim’s picture

We need

joachim’s picture

joachim’s picture

We need to restore getFormName() in the base class, and instead, add to it a deprecation notice so that any calling code gets notified to stop calling it.

We should keep the removal of getFormName() in the interface though, as we want implementations to remove it.

I wonder whether we could/should give a deprecation notice to classes that have that method, or whether that's overkill.

joachim’s picture

> I wonder whether we could/should give a deprecation notice to classes that have that method, or whether that's overkill.

Spoke to @longwave in Slack who says we don't need to do this.

joachim’s picture

We don't need a new merge request or a new branch -- you can keep working on the existing one.

arunsahijpal’s picture

@joachim
Could you please provide me the Change Record link so that I can use it.

joachim’s picture

There isn't a Change Record yet. Use the link in the issue details to create a draft.

vladimiraus made their first commit to this issue’s fork.

vladimiraus’s picture

Status: Needs work » Needs review

Added change record.

arunsahijpal changed the visibility of the branch 3497021-confirmforminterfacegetformname-serves-no to hidden.

joachim’s picture

Thanks!

I've rewritten it a bit as it was mostly the text of the issue summary. The CR has a different audience from the IS -- the IS says what's wrong and what we're going to fix and how, but the CR needs to tell people what's changed and how to update their code in response. If people reading the CR want the background, they can follow the link to the issue.

joachim’s picture

Status: Needs review » Needs work

Overall looks good, but the deprecation docs and error messages need to follow the standard format at https://www.drupal.org/about/core/policies/core-change-policies/how-to-d....

ankitv18 made their first commit to this issue’s fork.

arunsahijpal’s picture

Status: Needs work » Needs review

@joachim,
I've made the changes that you suggested, pls review.

joachim’s picture

Status: Needs review » Needs work

Your new MR is missing all the changes from the original MR, such as removing the calls to this method in core!

Please can you make the new deprecation message changes on the *original* branch and MR, and close the new branch and MR.

A new branch/MR is only needed when we are trying a completely different approach. That is not the case here.

arunsahijpal changed the visibility of the branch 3497021-confirmforminterfacegetformname-serves-no to active.

arunsahijpal changed the visibility of the branch 3497021-deprecate-getFormName to hidden.

arunsahijpal’s picture

Status: Needs work » Needs review

@joachim
I've made the changes in the original MR and hide the new MR, please check.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Can summary be updated. The title mentions deprecating getFormNames() pretty straight forward. But the MR appears to be doing extra stuff, so think that should be marked so easier to review.

joachim’s picture

Issue summary: View changes
Status: Needs work » Needs review

Done.

smustgrave’s picture

Status: Needs review » Needs work

Thanks left some comments on the MR.

godotislate’s picture

Status: Needs work » Needs review

Changes are in scope. See explanation in MR comments.

joachim’s picture

Status: Needs review » Reviewed & tested by the community

I think this is ready.

BTW @vladimiraus it's much clearer to rebase on 11.x rather than merge it in, as that way you don't get merge commits in the feature branch's history.

catch’s picture

Status: Reviewed & tested by the community » Needs work

This looks like good cleanup, but git apply is complaining about trailing whitespace errors - could that be cleaned up?

godotislate’s picture

Status: Needs work » Needs review

PHPCS wasn't picking up any white space issues, so I took a guess that it's the deprecation message string.

I also removed the string typehints on getFormName() in (unlikely, granted) the case that there are subclasses overriding the base class method.

smustgrave’s picture

Status: Needs review » Needs work

Appears to need a rebase, will keep an eye out on it though probably good to self RTBC

If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

arunsahijpal’s picture

Status: Needs work » Reviewed & tested by the community

@smustgrave ,
pls check now

arunsahijpal’s picture

Status: Reviewed & tested by the community » Needs review

Changed the status by mistake to RTBC
moving back to NW.

arunsahijpal’s picture

@joachim,
Could you please review it.

joachim’s picture

Status: Needs review » Needs work

Just a couple of minor things to change.

arunsahijpal’s picture

Status: Needs work » Needs review

Please check now.

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Looks good. Thanks!

godotislate’s picture

FYI for future reference, I think using __METHOD__ can save having to type ConfirmFormBase::getFormName(), etc.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

There are quite a few usages in in contrib - see http://codcontrib.hank.vps-private.net/search?text=-%3EgetValue%28%27con...

I discussed with @longwave - the thing that's tricky here is that we are only removing this because it is dead code but the value is still being used by contrib and these modules will break silently because the deprecation we're adding here is only for the method. The current implementation will break modules using $form_state->getValue('confirm') immediately. Obviously form arrays are not part of the BC promise - see https://www.drupal.org/about/core/policies/core-change-policies/frontend... - but I think we can/should do better. I think we should consider adding a #deprecated key to the form arrays so we can issue a deprecation when calling $form_state->getValue() for a specific key. I think doing this would allow us to remove old stuff like this and get modules to update without breaking stuff.

I think we should postpone this issue on adding this capability to the form system. Going to set this issue to needs work to open the issue to do this and then we can postpone this on that issue.

godotislate’s picture

Title: ConfirmFormInterface::getFormName() serves no purpose in ConfirmFormBase » [PP-1] ConfirmFormInterface::getFormName() serves no purpose in ConfirmFormBase
Status: Needs work » Postponed

Created #3519300: Provide a way to deprecate form API array value keys per #64.

Added a note that FormStateInterface::getValue() can take nested keys as parameters, so that might need to be accounted for with the #deprecated implementation

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.