Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
See the parent issue for problem motivation. This issue covers the following elements:
- Hidden
- Token
- Value
Proposed resolution
Document them....
Comment | File | Size | Author |
---|---|---|---|
#15 | 2486993-14-document-hidden-elements.patch | 1.81 KB | metzlerd |
#15 | interdiff-2486993-12-14.txt | 1.02 KB | metzlerd |
#10 | interdiff-2486993-6-10.txt | 787 bytes | metzlerd |
#6 | 2486993-6-document-hidden-elements.patch | 1.64 KB | metzlerd |
#6 | interdiff-2486993-2-6.txt | 1.2 KB | metzlerd |
Comments
Comment #1
metzlerd CreditAttribution: metzlerd as a volunteer commentedPicking this up next.
Comment #2
metzlerd CreditAttribution: metzlerd as a volunteer commentedHere's the first take. After looking at Token value, it didn't seem to need a usage example because it sounds like you never need to use it?
Comment #3
jhodgdonSorry for the delay in reviewing -- I've been on vacation for two weeks.
So, this looks pretty good! I agree that Token doesn't need additional docs.
A couple of questions/comments:
The relationship between value and default value doesn't make sense to me in this documentation.
Actually does a hidden element have #value at all? I am not sure what it would do.
Could we say:
... but is only stored in the form array for use in...
Comment #4
metzlerd CreditAttribution: metzlerd as a volunteer commentedNo worries... I'm out on a motorcycle adventure this week myself... and I was expecting you to be unavailable.
Let me try and explain. If you set #value, the value of the hidden form element will show up for the browser js to use, but the values returned by $form_state will be immutable. You could make the argument that with HTML5 people should just use data- attributes in other form elements, but this is the way #value has been used with hidden elements in the past. It might be useful to to know that this is in fact (or was in d7) true for all elements. You can set "#value" on a textfield and it will become immutable as well, even though the browser will still show the field and it will look like the user can edit it.
I realize this is a tricky thing to explain and welcome any insights or suggestions. I suppose that we could just not document the #value behavior, but I have had enough accidental uses of #value when I meant to use #default_value with hidden elements that I thought it worth mentioning in the docs.
Will certainly address #2 when I can.
Comment #5
jhodgdonAh. If #value behavior is for all elements, then I think we should put it somewhere else, not on the Hidden form element docs.
Comment #6
metzlerd CreditAttribution: metzlerd as a volunteer commentedOk. Here's the changes as requested.
FYI: No one would probably ever use #value on a visible element as #disabled would be a much better choice.
Comment #7
jhodgdonLooks great, except:
This example still uses #value and not #default_value... not sure what to do here, but there is a disconnect.
Comment #8
metzlerd CreditAttribution: metzlerd as a volunteer commentedhmm... This brings me back to feeling the need to document the distinction. I started searching for other examples in core by searching for all occurrences of '#type' => 'hidden'. Most of the examples use #value for things like entity_id, but some use it for Javascript behaviors. The Vertical Tabs control uses a default_tab hidden element so that you could keep track of the currently selected tab across form rebuilds. The books module uses it to keep track of a tree hash to help you understand if you have changed the tree via javascript when you finally do the form submission. (It actually uses #value and #default_value in the same forms on different elements for the original and modified hashes)
If you choose the wrong implementation here the code will simply break, and it seems to me that choosing #value vs. #default value is important to this control in a way that it is not to other controls. I cannot think of another control where you would normally make a choice as to whether to use #value vs. #default value.
What are your thoughts?
Comment #9
jhodgdonYeah. OK. I am convinced - we should document #value for hidden.
Comment #10
metzlerd CreditAttribution: metzlerd as a volunteer commentedHere's a rewording that might be clearer.
Comment #11
jhodgdonLooks good, just a couple of typos and I think we're done:
api -> API
I think the - in this line should be an = right?
Comment #12
metzlerd CreditAttribution: metzlerd as a volunteer commentedComment #13
metzlerd CreditAttribution: metzlerd as a volunteer commentedComment #14
jhodgdonSorry, apparently my item 3 wasn't clear:
This line still has - in place of =
Sorry I didn't get that across in my last review.
Also if we're fixing that, you could change "form API" to "Form API", which I didn't say in my last review either.
Comment #15
metzlerd CreditAttribution: metzlerd as a volunteer commentedYou were clear enough. I just missed it in my haste, sorry. Here ya go.
Comment #16
jhodgdonGreat, thanks!
Comment #17
jhodgdonTagging for "it's OK to commit now". Just API docs.
Comment #18
alexpottCommitted af97e79 and pushed to 8.0.x. Thanks!