Problem/Motivation

The documentation on the Drupal\Core\Render\Element\Select element doesn't warns that using #empty_value could lead to overwriting a value in #options.

See #1936636: Fix the documentation for #empty_value for more details.

Steps to reproduce

N/A

Proposed resolution

Add a warning in the comments about this situation.

Remaining tasks

User interface changes

API changes

None.

Data model changes

None.

Release notes snippet

None.

CommentFileSizeAuthor
#33 3361695-nr-bot.txt1.22 KBneeds-review-queue-bot

Issue fork drupal-3361695

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

tunic created an issue. See original summary.

tunic’s picture

Please ignore the 10.1.x, I pushed there by mistake. The same fix is in the 3361695-fix-the-documentation branch.

tunic’s picture

Status: Active » Needs review
Related issues: +#1936636: Fix the documentation for #empty_value
tunic’s picture

Additionally, should the Form API or render system detect this problem and raise a waring? Some kind of render/form API array structure validation

borisson_’s picture

Status: Needs review » Needs work
Creating list of files to check by comparing branch to 10.1.x
/var/www/html/core/lib/Drupal/Core/Render/Element/Select.php:44:38 - Unknown word (overriden)

Should be overridden instead.

tunic’s picture

Status: Needs work » Needs review

Thanks for the review, typo fixed and branch rebased so it's mergeable again.

smustgrave’s picture

Status: Needs review » Needs work

Seems there are out of scope indent changes happening.

tunic’s picture

Status: Needs work » Needs review

Ouch! I'm trying a new IDE and I've discovered it is not configured for Drupal coding standards. Sorry for the noise.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Text appears to match the issue described in the issue summary and no issues in the tests.

LGTM.

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.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work

I think adding an assert (as proposed in #5) for this condition would be a great DX improvement since this would be a real DrupalWTF to deal with.

tunic’s picture

Status: Needs work » Needs review

#12, I think it is interesting to automatically check that the issue is not happening (aka #options value overriden by #empty_value if #empty_option is present in #options), but I'm not sure how to do it.

Using assert will only work when assertions are enabled. This can be the case for automatic testing but it is not on production environments. Given the dynamic nature of forms, it is possible that the case where #options is overriden only happens on certain situations. This would not be detected by testing. And having a fatal error can be too drastic, I guess.

I would check the condition and raise a warning. This way Drupal is able to continue running, even though there's probably a problem. Any devs checking the logs or debugging a weird behavior on a form will see error.

But this discussion can ne longer, and the initial issue is about documentation. I think we should focus this issue on the documentation and open a new one for the warning where we can discuss how to do it.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs followup

Maybe open a follow up for #12?

quietone’s picture

Status: Reviewed & tested by the community » Needs review

I am doing triage on the core RTBC queue.

The issue summary is short and clear. And looking at the MR it is fixing the problem stated there. I read the comments and see that a suggestion was moved to another issue. That looks like a good choice here.

On reading the MR I though the sentence could be improved and made a suggestion on the MR. Setting back to NR for that.

Then, I realized I should have read the code. It appears this is working as designed, therefor I am not convinced that the doc change should include 'Warning'. Right now I am thinking the sentence should begin with 'Note that' instead of 'Warning:'

smustgrave’s picture

Status: Needs review » Needs work

For the update mentioned in 16

tunic’s picture

Status: Needs work » Needs review

#16, it may work as designed, but the behavior doesn't seem right, hence the warning. See https://www.drupal.org/project/documentation/issues/1936636#comment-1505... for an example.

However, I think 'Note' would work. If it is preferred I won't complain, but I would like to have a decision soon so we can fix the MR with the agreed value.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Searching core found examples of using both Warning: and Note: so seems like a coin toss. Going to mark it though as the sentence has been updated.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work

+1 that "note that" would read better here. It's definitely more in line with our pre-existing comments.

tunic’s picture

Status: Needs work » Needs review
tunic’s picture

Status: Needs review » Needs work
tunic’s picture

Status: Needs work » Needs review

Changed "Warning: " by "Note that" (with an intermediate commit where I wrongly used "Note: ")

smustgrave’s picture

Seemed to cause CC failure.

tunic’s picture

PHPStan step has been executed successfully.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the quick fix. Going to go ahead and mark it as I doubt this will cause test failures.

quietone’s picture

I'm triaging RTBC issues. I read the IS and the comments since my last comment. I didn't find any unanswered questions or other work to do.

Leaving at RTBC.

lauriii’s picture

Status: Reviewed & tested by the community » Needs review

I've probably come to lurk on this issue more than 3 times by now and I've always gotten stuck at trying to remember what does it mean that the #empty_option overrides a value from the #options. I remember this made sense after stepping through this with a debugger but maybe we need to improve the docs so that that wouldn't be required? 😅

tunic’s picture

@laurii I guess you mean more details in the sentence are required on the proposed sentence.

This is the current sentence:

Note that if #empty_value is the same as a key in #options then that value in #options is overridden with the value of #empty_option

Would it be clear with this?

Note that if #empty_value is the same as a key in #options then the value of #empty_option is used for that key in the #options array.

Also, I guess the status should be "Needs work" instead of "Needs review", right?

smustgrave’s picture

Status: Needs review » Needs work

Does sound like NW is the correct status.

tunic’s picture

Status: Needs work » Needs review

I stumbled upon this issue again. I've tried to better explain the situation to address #28:

Note that if #empty_value is the same as a key in #options then the value of #empty_option is used for that key in the #options array. This is because #empty_value and #empty_option are merged into the #options array. Hence, make sure #empty_value is not a key in #options array.

If this is not enough, please point out what is missing or what would be a better approach to this. The issue is complex and it is hard to understand without a little bit of context or checking all the comments in the class' header.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Took another look and believe this one is good.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new1.22 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

tunic’s picture

Status: Needs work » Reviewed & tested by the community

  • nod_ committed e2774eb7 on 11.x
    Issue #3361695 by tunic, smustgrave, lauriii, quietone, borisson_: Fix...
nod_’s picture

Status: Reviewed & tested by the community » Fixed

Committed e2774eb and pushed to 11.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.