The form_example from the Examples module will crash Drupal 8 if the ColorPickerWidget defined on a content type, and you attempt to edit that item.
The error in the watchdog is:
LogicException: You are not allowed to use js in #attached in drupal_process_attached() (line 648 of
/var/www/drupal/core/includes/common.inc).
The cause here seems to be related to changes described in https://www.drupal.org/developing/api/8/assets. The current version in the example (examples/field_example/src/Plugin/Field/FieldWidget/ColorPickerWidget.php) is doing this, which is now illegal:
$element['value'] += array(
'#suffix' => '<div class="field-example-colorpicker"></div>',
'#attributes' => array('class' => array('edit-field-example-colorpicker')),
'#attached' => array(
// Add Farbtastic color picker.
'library' => array(
'core/jquery.farbtastic',
),
// Add javascript to trigger the colorpicker.
'js' => array(drupal_get_path('module', 'field_example') . '/field_example.js'),
),
);
If I understand the discussions about the changes in #attach, this is an example of inline javascript, which is now verbotten in core.
I'm attempting to get the code to work again by defining a library for the field_example.js code to live in, but still don't have it working yet.
Comment | File | Size | Author |
---|---|---|---|
#2 | issue-2543174.patch | 1.26 KB | Torenware |
Comments
Comment #1
Torenware CreditAttribution: Torenware as a volunteer commentedComment #2
Torenware CreditAttribution: Torenware as a volunteer commentedI've gotten the example working again. Patch enclosed.
Comment #3
Mile23Great, thanks.
If you set the issue to 'needs review' then the testbot will try your patch and see if it works.
I don't recall offhand if there's a test that tries to edit the field, but if there's not, we'll need to add one.
Comment #5
Mile23If you click on the 'view' link in the testbot failure above, you'll see that the failure has to do with the block example module, and not this one. Probably another regression due to API change.
Comment #6
Torenware CreditAttribution: Torenware as a volunteer commentedMiles -- anything else I can do?
I've been looking at block_example, and think there is a general problem with Block plugin discovery (see #2547173). This appears to match the errors in SimpleTest.
Still investigating this.
Comment #7
Torenware CreditAttribution: Torenware as a volunteer commentedComment #8
Mile23The solution is that we need to define a library, and then use #attached to define it.
We already reference the farbtastic library, so define it as a dependency of the local library.
Change record here with some clues as to how: https://www.drupal.org/node/2201089
Also it would be nice to put the JS file into its own directory, like js/
We also need a test to navigate to the page where that crash happens and verify that it doesn't.
Thanks.
Comment #9
Mile23Generally unassigning issues. Please re-assign yourself as desired.
Comment #10
Mile23Trying to reproduce this with these steps:
Everything works like it should.
Closing this as 'can't reproduce.' File a follow-up if it's still happening.
Thanks.