Form arrays should never be altered after a form array is returned by drupal_get_form(). hook_forms+hook_form_[$form_id]_alter is made for this. As such I've moved some new user_block_view code around to be saner. This code was introduced by #1772584: Get rid of user_login_block() & user_login() in favour of user_login_form().

Hopefully this makes sense to everyone.

Eclipse

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

EclipseGc’s picture

FileSize
2.17 KB

Here's the patch!

EclipseGc’s picture

Status: Active » Needs review

need testing

Damien Tournoud’s picture

+function user_form_user_login_form_block_alter(&$form, &$form_state, $form_id) {
+  if ($form_id == 'user_login_form_block') {

That looks slightly redundant.

EclipseGc’s picture

I was asked to explain a bit more why this was needed, so here goes:

When you have two forms that are virtually identical, except slightly different, hook_forms becomes really useful. Historically if you look at node_forms() this was how individual form elements could easily be added per form_id. User module provides the user_login_form form, but then the block wants to use ALMOST that form with a few alterations. As such, instead of providing a completely different form, or doing something like altering the form elements after drupal_get_form is called (which cannot be undone or altered out no matter how hard you try) implementing hook_forms simply gave us an alias for the form we cared about, where by we could now apply typical form_alteration methodology.

Hopefully this makes sense of the patch.

EclipseGc’s picture

FileSize
2.1 KB

Changes from #3

amateescu’s picture

+++ b/core/modules/user/user.module
@@ -1568,6 +1560,28 @@ function user_set_authmaps($account, $authmaps) {
+/**
+ * Implement hook_form_alter().
+ */

Implements hook_form_FORM_ID_alter() :/

EclipseGc’s picture

FileSize
2.11 KB

If 5 passes this will pass, just changed the docblock from 6.

sun’s picture

The downside of hook_form_alter() is that it runs only after retrieval of the form definition and is not cached with it.

So why don't we take the hook_forms() approach suggested here and skip the alter? I.e.:

function user_forms() {
  $forms['user_login_form_block'] = array(
    'callback' => '_user_login_form_block',
  );
}

function _user_login_form_block($form, &$form_state) {
  $form = user_login_form($form, $form_state);
  // Perform adjustments...
  return $form;
}

The reason for still using hook_forms() with this would be to automatically leverage Form API's built-in base form ID mechanisms (invoking hook_form_alter() additionally for the base form ID, likewise for #validate, #submit, and #theme).

andypost’s picture

Any reason to make another form? Original issue was about removal of 2 forms #1772584: Get rid of user_login_block() & user_login() in favour of user_login_form()
Originally @catch suggested this approach with customization in block, suppose it needs his review

sun’s picture

Here's what I suggested in #8, but skipping the hook_forms() workaround.

EclipseGc’s picture

@sun,

This feels more arcane than what I did to begin with and that felt pretty freaking arcane. Forgive me for not understanding all the dynamics at play here, but is the caching of this sort of alteration a consideration? especially since this is anonymous user facing only?

I guess I'm fine with the approach, but it does seem very arcane, I've never seen this done anywhere (not to say it isn't just that I've not seen it). Any core maintainer want to weigh in on this?

Eclipse

effulgentsia’s picture

The downside of hook_form_alter() is that it runs only after retrieval of the form definition and is not cached with it.

Huh? We cache not the retrieved form, but the prepared form, so alters are included.

I like #5 except for user.module altering its own form, and I prefer alters to be limited to a module altering something of another module. Ideally, we should change hook_forms() to decouple 'base form id' from 'callback', but I don't think it's worth making that part of the scope of this issue. Can we go with #5 for this issue, and have a follow up for improving hook_forms()?

Any reason to make another form? Original issue was about removal of 2 forms Originally @catch suggested this approach with customization in block, suppose it needs his review

Well, the original issue was about removing it as 2 totally disconnected forms: this issue is about making a form that's a customization of a base form, which seems the correct approach to me. I don't see comments in that issue which are incompatible with this issue, but please point one out if I missed something.

andypost’s picture

So #7 or #10 approach is a better?

jibran’s picture

Ping!

jhedstrom’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs reroll
drupaldrop’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

During rerolling this patch , the functions mentioned in the patch doesnot exist in the current 8.0.x branch . Hence this patch cannot be rerolled i guess .

Any thoughts?, otherwise we can close this issue.

I am removing the tag need reroll and changing status needs work to needs review.

jhedstrom’s picture

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +Needs issue summary update

Indeed--if the underlying logic has not been fixed already, this issue needs a summary update.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.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.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.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.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.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.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.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.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

pameeela’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)
Issue tags: +Bug Smash Initiative

Thanks everyone who contributed to this issue.

Given that the latest comments were seeking additional information and none has been provided, I am marking this issue Closed (cannot reproduce).

If anyone believes this issue should not be closed, please provide specific steps to reproduce when reopening it.