Problem/Motivation

From #1998648-53: "Administration pages language" user account setting should be hidden if not used
Some form elements in AccountForm are created only under certain conditions.
contrib' form_alters have to check for elements existence before modifying them.

This is a problem because

  • the extra form_alters make performance worse?
  • it is bad contributor developer experience (CX)

Postponed on 8.1.x per https://www.drupal.org/contribute/core/beta-changes

Proposed resolution

"standardize" them by using #access

Remaining tasks

User interface changes

No. (The UI should be the same before and after. We should have screenshot to prove that.)

API changes

No.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

penyaskito’s picture

swentel’s picture

Isn't this solved now that we have the form display screen ?

penyaskito’s picture

Not yet, the idea behind this issue was to move the if-else conditions in AccountFormController::form to the #access property on the form elements.

penyaskito’s picture

Issue summary: View changes

typos everywhere.

penyaskito’s picture

Title: AccountFormController form clean-up » AccountForm clean-up
Issue summary: View changes
FileSize
7.32 KB

First attempt to clean-up the if-else structure, using #access instead.

penyaskito’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 4: 2058319-4.patch, failed testing.

penyaskito’s picture

Status: Needs work » Needs review
FileSize
6.6 KB

$form['account']['pass'] should not be overridden. That if remains.

penyaskito’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 7: 2058319-7.patch, failed testing.

penyaskito’s picture

Status: Needs work » Needs review
FileSize
6.61 KB
497 bytes

Oops... Missed a clause for checking only to validate the password when actually changing an existing user.

Status: Needs review » Needs work

The last submitted patch, 10: 2058319-10.patch, failed testing.

penyaskito’s picture

FileSize
6.69 KB

The password fields were not correctly displayed when admin registering users.

penyaskito’s picture

Status: Needs work » Needs review
penyaskito’s picture

Issue tags: +Security

A security review will be helpful.

penyaskito queued 12: 2058319-12.patch for re-testing.

pcambra’s picture

  1. +++ b/core/modules/user/src/AccountForm.php
    @@ -112,59 +112,60 @@ public function form(array $form, array &$form_state) {
    +      '#access' => !$register && $user->id() == $account->id(),
    

    The user/account comparison is used in several places, probably a variable would be useful

  2. +++ b/core/modules/user/src/AccountForm.php
    @@ -112,59 +112,60 @@ public function form(array $form, array &$form_state) {
    +      '#access' => (!empty($protected_values) && $user->id() == $account->id() && !$register),
    

    A comment here would be helpful

+++ b/core/modules/user/src/AccountForm.php
@@ -112,59 +112,60 @@ public function form(array $form, array &$form_state) {
+    if ($register && (!$config->get('verify_mail') || $admin)) {
       $form['account']['pass'] = array(
         '#type' => 'password_confirm',
         '#size' => 25,
         '#description' => $this->t('Provide a password for the new account in both fields.'),
-        '#required' => TRUE,
+        '#required' => $register && (!$config->get('verify_mail') || $admin),
+        '#access' => $register && (!$config->get('verify_mail') || $admin),

The same comparison is used for the if, access and required, is the if even needed?

penyaskito’s picture

Handled #16.1 and #16.2.

For the third point, probably we should revert the change.

Status: Needs review » Needs work

The last submitted patch, 17: 2058319-accountform-cleanup-17.patch, failed testing.

penyaskito’s picture

Status: Needs work » Needs review
FileSize
6.48 KB
661 bytes

Reverted the change at #16.3.

dawehner’s picture

+++ b/core/modules/user/src/AccountForm.php
@@ -113,54 +113,56 @@ public function form(array $form, FormStateInterface $form_state) {
+      $request_new = l($this->t('Request new password'), 'user/password', array('attributes' => array('title' => $this->t('Request new password via email.'))));

We no longer have to use l() but can rely on the link generator.

penyaskito’s picture

Right, thanks!

Status: Needs review » Needs work

The last submitted patch, 21: 2058319-accountform-cleanup-21.patch, failed testing.

dawehner’s picture

You just need something like in that drupal unit test.


public function setUp() {
  parent::setUp():


  $this->installSchema('system', ['router']);
}
penyaskito’s picture

Status: Needs work » Needs review
FileSize
11.68 KB
768 bytes

Thanks! Green again.

dawehner’s picture

  1. new file mode 100644
    index 0000000..96827c1
    
    index 0000000..96827c1
    --- /dev/null
    
    --- /dev/null
    +++ b/core/2324113-color-1.patch
    
    +++ b/core/2324113-color-1.patch
    @@ -0,0 +1,98 @@
    

    nope!

  2. +++ b/core/2324113-color-1.patch
    @@ -0,0 +1,98 @@
    + ¶
    ++use Drupal\Component\Utility\Color;
    ...
    + ¶
    ...
    + ¶
    ...
    + ¶
    ...
    + ¶
    ...
    + ¶
    ...
    + ¶
    

    whitespace all over the place.

  3. +++ b/core/2324113-color-1.patch
    @@ -0,0 +1,98 @@
    diff --git a/core/lib/Drupal/Component/Utility/NestedArray.php b/core/lib/Drupal/Component/Utility/NestedArray.php
    
    diff --git a/core/lib/Drupal/Component/Utility/NestedArray.php b/core/lib/Drupal/Component/Utility/NestedArray.php
    index c494c24..8e00e06 100644
    
    index c494c24..8e00e06 100644
    --- a/core/lib/Drupal/Component/Utility/NestedArray.php
    
    --- a/core/lib/Drupal/Component/Utility/NestedArray.php
    +++ b/core/lib/Drupal/Component/Utility/NestedArray.php
    
    +++ b/core/lib/Drupal/Component/Utility/NestedArray.php
    +++ b/core/lib/Drupal/Component/Utility/NestedArray.php
    @@ -34,10 +34,10 @@ class NestedArray {
    
    @@ -34,10 +34,10 @@ class NestedArray {
        * $value = NestedArray::getValue($form, $parents);
        * @endcode
        *
    -   * A return value of NULL is ambiguous, and can mean either that the requested
    -   * key does not exist, or that the actual value is NULL. If it is required to
    -   * know whether the nested array key actually exists, pass a third argument
    -   * that is altered by reference:
    +   * The return value will be NULL, regardless of whether the actual value is
    +   * NULL or whether the requested key does not exist. If it is required to know
    +   * whether the nested array key actually exists, pass a third argument that is
    +   * altered by reference:
        * @code
        * $key_exists = NULL;
        * $value = NestedArray::getValue($form, $parents, $key_exists);
    

    This change doesn't seem to be related?

  4. +++ b/core/modules/user/src/Tests/UserAccountFormFieldsTest.php
    @@ -26,6 +26,15 @@ class UserAccountFormFieldsTest extends DrupalUnitTestBase {
    +    \Drupal::service('router.builder')->rebuild();
    

    Do we really need this here? Afaik the router would rebuild itself is something requests it.

penyaskito’s picture

Status: Needs review » Needs work

#25.4: tried that, and the test didn't find "user.password".

+++ b/core/modules/user/src/Tests/UserAccountFormFieldsTest.php
@@ -26,6 +26,15 @@ class UserAccountFormFieldsTest extends DrupalUnitTestBase {
+  public function setUp() {

Must be protected.

penyaskito’s picture

Status: Needs work » Needs review
FileSize
7.28 KB
3.98 KB

Fixed those.

dawehner’s picture

What about point 4?

Xano’s picture

+++ b/core/modules/user/src/AccountForm.php
@@ -113,54 +113,56 @@ public function form(array $form, FormStateInterface $form_state) {
+    if ($register && (!$config->get('verify_mail') || $admin)) {

Why not use #access for this too?

penyaskito’s picture

@dawehner: Answered in #26:

#25.4: tried that, and the test didn't find "user.password".

@Xano: because it is creating an element with the same name of a previous one. I didn't want to rename because I guessed that if it is named the same is for a reason, but it's worth trying.

penyaskito’s picture

Status: Needs review » Needs work
penyaskito’s picture

Status: Needs work » Needs review
FileSize
7.83 KB
penyaskito’s picture

Re-uploaded last one with interdiff.
As renaming IMHO does not makes sense, I just moved that snippet so they are together, and alter the same array instead of creating a new one. Makes it more readable IMHO.

Maybe comments could be improved, suggestions welcome.

sun’s picture

Do we have test coverage for the complex logic being touched here?

I authored UserAccountFormFieldsTest, but that only asserts the expected order of input elements (cf. #2191785: Password managers are identifying/storing wrong username field when creating a user account). — btw, not sure why the test suddenly needs to rebuild the router; a clean-up like this should not cause behavior changes.

penyaskito’s picture

@sun I think we have coverage for everything here, as any wrong change I did broke any or several tests.
About rebuilding the router, in #20 requested to use link generator with named routes instead of l () with a path. That's why we need to change that.

Status: Needs review » Needs work

The last submitted patch, 33: 2058319-accountform-cleanup-33.patch, failed testing.

penyaskito’s picture

Issue tags: +Needs reroll
quietone’s picture

My attempt at a reroll

quietone’s picture

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

Status: Needs review » Needs work

The last submitted patch, 39: 2058319-accountform-cleanup-34.patch, failed testing.

quietone’s picture

Status: Needs work » Needs review
FileSize
7.54 KB

Well, that was an impressive number of fails. Here's an improvement.

Status: Needs review » Needs work

The last submitted patch, 42: 2058319-accountform-cleanup-35.patch, failed testing.

quietone’s picture

Well, that helped. But I've either completed messed this up or it needs someone with more knowledge than I currently have.

quietone’s picture

This should be better.

quietone’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 45: 2058319-accountform-cleanup-45.patch, failed testing.

quietone’s picture

The exception has to do with making the link discussed in #20, #34 and #35. My patch keeps the original l(), but the same failure happens with getLinkGenerator as used by penyaskito.

$request_new = $this->getLinkGenerator()->generate($this->t('Request new password'), 'user.pass', array(), array('attributes' => array('title' => $this->t('Request new password via email.'))));

What method should be used to create the link, and then what to do about the test?

quietone’s picture

Thank you to YesCT and mpdonadio.

LinkGenerator is working and removed the the changes to /core/modules/user/src/Tests/UserAccountFormFieldsTest.php

quietone’s picture

Status: Needs work » Needs review
penyaskito’s picture

Status: Needs review » Needs work
+++ b/core/modules/user/src/AccountForm.php
@@ -230,15 +233,14 @@ public function form(array $form, FormStateInterface $form_state) {
-    if ($this->moduleHandler->moduleExists('filter')) {
-      $form['signature_settings']['signature'] = array(
-        '#type' => 'text_format',
-        '#title' => $this->t('Signature'),
-        '#default_value' => $account->getSignature(),
-        '#description' => $this->t('Your signature will be publicly displayed at the end of your comments.'),
-        '#format' => $account->getSignatureFormat(),
-      );
-    }
+    $form['signature_settings']['signature'] = array(
+      '#type' => 'text_format',
+      '#title' => $this->t('Signature'),
+      '#default_value' => $account->getSignature(),
+      '#description' => $this->t('Your signature will be publicly displayed at the end of your comments.'),
+      '#format' => $account->getSignatureFormat(),
+      '#access' => $this->moduleHandler->moduleExists('filter'),
+    );

After discussion between, quietone, alexpott and myself, we should define a textfield here, and the filter module should alter the form, avoiding the moduleExists calls.

quietone’s picture

@penyaskito Thanks for sorting that out. Just so you know I don't yet know how to implement what you suggest.

YesCT’s picture

Version: 8.0.x-dev » 8.1.x-dev
Issue summary: View changes
Status: Needs work » Postponed
Related issues: +#1998648: "Administration pages language" user account setting should be hidden if not used

updated issue summary to try and specify why this is a problem.
we need to know so that we know if this should be postponed to 8.1.x per https://www.drupal.org/contribute/core/beta-changes

it looks like this is postponed to 8.1.x to me.

YesCT’s picture

there are bunch of issue still open for 8.0.x, especially bugs... but here is a link to some usability things also
https://www.drupal.org/project/issues/search/drupal?project_issue_follow...

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

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now 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.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now 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.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.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.

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.

quietone’s picture

Status: Postponed » Needs work

No longer postponded and looking at the current code this is still valid

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.