Hi
With D7 the #attached property is the correct way to add javascript/css to ensure that our files are always loaded even with cached output however the way to add javascript settings is not evident from http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
Whilst one can figure it out if they read up on drupal_process_attached a clear example would help.
The attached patch adds a new example of how to add js settings using attached.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | 1148620_14.patch | 887 bytes | grendzy |
| #13 | 1148620-attached-scope-example.patch | 1.07 KB | nick_schuch |
| #1 | settings-attached-js-1148620-1.patch | 1.25 KB | larowlan |
Comments
Comment #1
larowlanComment #2
jhodgdonWe need someone with more Drupal JavaScript knowledge than me to review this. I'll see who I can round up.
Comment #3
rfay@larowlan, I apologize for how hard it is to work with this doc, and it looks to me on a casual look like your patch is great..
But could you do me a favor in the future? Could you please put the old and the new markup into the issue here, so it can be reviewed without having to clone the project and apply the patch and then look at it in a browser?
Here's what I'm intending:
Existing
Usage example (AJAX Example):
The above javascript #attach could also be written as:
and inline javascript can be loaded with the javascript as the key of the array,
as described in drupal_add_js()
New (with patch)
Usage example (AJAX Example):
The above javascript #attach could also be written as:
and inline javascript can be loaded with the javascript as the key of the array,
as described in drupal_add_js()
Adding settings
You add a javascript setting by using the 'data' key (as php keys can't be arrays):
Wow, the codefilter on api must be different from the one here. Oh well, at least this way it's easy to get the gist of it.
Comment #4
rfayThis looks right to me. Thanks!
Comment #5
larowlanThanks Jennifer and Randy.
@rfay - will do regarding example next time.
Comment #6
jn2 commentedI've added the new example to the FAPI reference. Thanks, larowlan!
Comment #8
stefan freudenberg commentedHi.
Sorry for reopening, but I believe this documentations needs to be improved. Although it is correct it leads people to overwrite existing attached items in form alter hooks. In my opinion the documentation should recommend to add elements to the attached keys by appending them, i.e. $form['#attached']['js'][] etc. If you agree I'll provide a patch.
Best,
Stefan
Comment #9
jhodgdonStefan: That is definitely the correct recommendation. If you have a suggestion on how to make the documentation say that more clearly, especially if your suggestion comes in the form of a patch, then this would be most welcome. :)
We probably need one patch for Drupal 8.x/7.x, and another for 6.x... oh wait, #attached doesn't exist in 6.x. So, just a patch for 7.x (which will presumably also apply just fine to 8.x) would be great. Thanks!
Comment #10
wizonesolutionsI might be able to do this - I got a bit confused by the same thing, thinking, "Wait, so I'm overriding the Node module's attached JS potentially?"
To confirm, do I clone the documentation project and patch the HTML file in developer/topics?
Comment #11
jhodgdonYes, that's the file to patch -- start with the 8.x repository (even though we don't have a way to set the version on this project, since it's mostly for issues with on-line docs pages). Thanks!
Comment #12
j0rd commentedAppears the information related with how to use this field in D7 is in-correct, or at least not as correct as it should be.
A proper example for D7 should be
The whole #attached section in the field api documentation actually requires a full re-write to be clear and concise. Judging by the amounts of incorrect example snippets on the internet, I suspect this documentation is to blame.
Comment #13
nick_schuch commentedI have included a patch which provides a "scope" setting example for #attached.
Comment #14
grendzy commentedThis text is also incorrect:
The key must be numeric. This can be seen in the source for drupal_process_attached(): The
is_numeric()function is used to detect attached settings data.Comment #15
MauMauHope this helps someone.
Three ways to attach a script to a field.
Comment #16
jhodgdonUmmm... but there *is* a data key in there. I don't think the patch in #14 is needed or that it clarifies anything. Yes, the outer key is numeric, but the inner one in the example is 'data'.
And I don't think the patch in #13 resolves the issue that was brought up in previous comments.
Comment #17
grendzy commentedYeah, you're right that my patch in #14 is not helpful. The problem is there are 4 different ways to construct an #attached array. In the first three, the
$dataparameter can be stored in either the array key, the array value, or the'data'value nested inside the options array. (It's okay if your head just exploded, mine did too.)Here are the 4 ways, using variable names that match the drupal_add_* function parameters.
Seeing it in this format helps to understand the relationship with the drupal_add_* functions, but lacks working examples. So I think maybe we actually need 8 code snippets in the doc; the 4 above followed by 4 examples.
I'm also a little sad it's too late to simplify this construct in D8.
Comment #18
bdimaggio@grendzy, this snippet is hugely helpful in illuminating the #attached form element property. Thanks!
Comment #19
quietone commentedThanks for working to improve Drupal.
This is for the Drupal 7 Form API reference. Since Drupal 7 is past end-of-life this is now a "won't fix".
Work on documentation for form elements for supported versions of Drupal is at #2486967: [meta] Move/Create Form Element Documentation and #3313535: [Meta] Improve documentation for Render and Form Elements.