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.

CommentFileSizeAuthor
#2 issue-2543174.patch1.26 KBTorenware
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Torenware’s picture

Issue summary: View changes
Torenware’s picture

Assigned: Unassigned » Torenware
FileSize
1.26 KB

I've gotten the example working again. Patch enclosed.

Mile23’s picture

Status: Active » Needs review
Issue tags: -Libraries, -attachments

Great, 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.

Status: Needs review » Needs work

The last submitted patch, 2: issue-2543174.patch, failed testing.

Mile23’s picture

If 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.

Torenware’s picture

Miles -- 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.

Torenware’s picture

Mile23’s picture

The 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.

Mile23’s picture

Assigned: Torenware » Unassigned

Generally unassigning issues. Please re-assign yourself as desired.

Mile23’s picture

Status: Needs work » Closed (cannot reproduce)

Trying to reproduce this with these steps:

  • Enable the module.
  • Edit the 'Basic page' content type to manage fields: admin/structure/types/manage/page/fields
  • Add an Example Color RBG field for single value.
  • Manage form display: admin/structure/types/manage/page/form-display
  • Set the widget to Color Picker.
  • Add content.
  • Use the color picker to set a color value.
  • Save content.
  • Edit content.
  • Use color picker to set a color value.
  • Save content.

Everything works like it should.

Closing this as 'can't reproduce.' File a follow-up if it's still happening.

Thanks.