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.
if tinymce_elements define $type['textarea']['#process'] it will override system_elements textarea. That's bad, bad, bad. Makes hook_elements almost unusable -- I think it's much more common to extend an existing element than defining a new one.
Comment | File | Size | Author |
---|---|---|---|
#16 | hook_elements_4.patch | 1.56 KB | chx |
#13 | hook_elements_3.patch | 1.97 KB | chx |
#11 | hook_elements_2.patch | 1.98 KB | chx |
#3 | hook_elements_1.patch | 1.94 KB | chx |
#2 | hook_elements_0.patch | 1.94 KB | chx |
Comments
Comment #1
m3avrck CreditAttribution: m3avrck commentedJust thoroughly tested patch and it works. Been talking with chx on IRC for almost 2 hours, if this patch was in there I don't think I would have had any issues and chx would not have had a headache ;-)
However, one problem does exist. If the same value is to be overriden twice, there is a race condition. For example, if one module has hook_elements() defined and defines #suffix = foo for all textareas, and another defines #suffix = bar for all textareas, whichever module's hook is called last (alphabetically) will take precedence. This happens for *all* form values, #prefix, #process, etc...
Comment #2
chx CreditAttribution: chx commentedWell.. let's try this.
Comment #3
chx CreditAttribution: chx commentedComment #4
m3avrck CreditAttribution: m3avrck commentedNew patch works great.
One caveat, if you use hook_elements() and set #process to a callback, if this callback overrides say #suffix or #prefix for a textarea that has been defined elsewhere, you will have to do something of this nature:
if (isset($element['#prefix'])) if (is_array($element['#prefix'])) ... else $elemen...=array($ele... $mystring)
Comment #5
m3avrck CreditAttribution: m3avrck commentedAn example of how to test this (see TinyMCE.module for full code) if you have defined a #process callback:
Comment #6
m3avrck CreditAttribution: m3avrck commentedComment #7
Dries CreditAttribution: Dries commentedWouldn't it be nicer if modules would not have to worry about overwriting other module's values?
Comment #8
chx CreditAttribution: chx commentedIf you override $node->body in nodeapi view (or load) then you have, well, overridden that. So it's not unheard of that you should be careful when operating on objects that others may operate on, too. We are far more ahead with this patch than without it.
Comment #9
m3avrck CreditAttribution: m3avrck commentedDries this patch fixes the *biggest* override problem that I encountered when defining hook_elements() that was simply not even the slightest intuitive and chx can attest to this :-)
While I agree about my last comment and #process, I'm not aware of a better practice for this isolate case. This patch doesn't introduce this problem (at least to my knowledge it is already there) but at least fixes the other more major override problem. Perhaps someone with a better understanding of forms.inc can create a patch to fix this caveat in that special circumstance (only override left after this patch).
Comment #10
chx CreditAttribution: chx commentedOnce again, if you are operatin in place on an object, you should be careful. But if we want, we can add this
after
if (is_array($form['#process'])) {
. I am not too fond this one, that's why I have not rolled a patch. But if Dries thinks so, well, the code is there, even code style is ok, so it's peanuts to roll into the patch.Comment #11
chx CreditAttribution: chx commentedAfter much consideration I arrived to this. #prefix and #suffix is always a string and the exception is the #process. Why? It's very awkward to type #prefix => array('
Comment #12
chx CreditAttribution: chx commentedsomething eat my followup:
array('<div....'); when that array will be practically always one element. And while theming it's also strange to have #prefix as an array. So, in the beginning and at the end we need strings. However, we need to 'escape' the string during #process so that if two textarea processors are adding prefixes, it's OK.
As I thought more, here is the simplified version: just take care to append/prepend to #prefix instead of overwriting it. No need to muck with arrays. Commiting new patch soon.
Comment #13
chx CreditAttribution: chx commentedThere we go -- now I collapse #prefix and #suffix just after merging the default element info in. That's where an array may arise. So, #prefix and #suffix now are always strings, treat them as such.
Comment #14
chx CreditAttribution: chx commentedComment #15
m3avrck CreditAttribution: m3avrck commentedPatch fully tested and working 100%. Tested various #suffix defined in different places, including hook_elements() and in #process callback functions. Nothing was overwritten so long as
['#suffix'] .= 'foobar'
was use, e.g., appending the string to the #suffix or #prefix string.I think this solution is very elegant and *much* nicer than the above proposed array patch. Modules can make use of this extremely easily, all they have to do is append the string and it works, no long, overdrawn array tests and what not. Please commit!
Comment #16
chx CreditAttribution: chx commentedOn further thought, it's unlikely to set static #prefix for an element. Use #process to do that. Now please, commit this, even the performance penalty is almost zero, now.
Comment #17
m3avrck CreditAttribution: m3avrck commentedModules still work with this patch, however after recent commits, the patch is off by 19 lines, but still applies cleanly.
Comment #18
chx CreditAttribution: chx commentedWhat holds this patch?
Comment #19
Dries CreditAttribution: Dries commented- I don't understand where you explain that people need to append the string. Please elaborate.
- In general, I don't like code which allow both scalar and array values to be used. Simpler is better.
Comment #20
chx CreditAttribution: chx commentedBy now, array is gone. Elaborate: if you define a #process callback and you change #prefix or #suffix then just append to these. This is the same as in form_alter now, there would have been a difference with former versions of the patch. The current version is very simple.
Comment #21
m3avrck CreditAttribution: m3avrck commentedRight Dries, simple example:
Simple enough! We'll make sure the Forms API docs make this clear since it can be confusing, but in reality, this is much of Drupal works, just append strings :-)
Comment #22
Dries CreditAttribution: Dries commentedCommitted to HEAD. Thanks.
Comment #23
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedI guess an equivalent patch which isn't attached to this issue got committed?
I see the $cache = array_merge_recursive changes in CVS, but the changes around line 268 are different from any of the patches here.
Comment #24
chx CreditAttribution: chx commentedbecause there were other patches since then :)
Comment #25
(not verified) CreditAttribution: commented