craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 637 forks source link

[5.x]: Craft JS not loading in control panel slideout #16155

Closed phrichards closed 5 days ago

phrichards commented 6 days ago

What happened?

Description

I'm building a custom plugin and trying to render some content in a slideout with asCpScreen(). The slideout is triggered by a button that I added on to a Commerce product. The slideout works and my template renders, but there are console errors that imply that Craft's JS is not loaded.

Script to open the slideout:

document.getElementById('configure').addEventListener('click', function (e) {

    e.preventDefault();
    // Post the redirect URI to the controller action
    const slideout = new Craft.CpScreenSlideout('https://mysite.ddev.site/actions/product-configurator/default')

    // Open the slideout
    slideout.open();

    // Listen for the submit event
    slideout.on('submit', function (e) {
        window.location = window.location;
    })
});

Controller action:

public function actionIndex(): Response
 {
     $response = $this->asCpScreen()
         ->action('product-configurator/default/save')
         ->title('Configure Product')
         ->contentTemplate('product-configurator/slideout/content.twig');

     return $response;
 }

The template is currently just a {{_self}}.

The slideout opens, and I can see the cancel and save buttons at the bottom, but errors in the console:

Image

The top error for appendBodyHtml() was there on load, the rest popped up after clicking the save button, which also caused an indefinite spinning indicator.

I can see all of Craft's JS in the sources panel, including the files where these functions are defined. Everything is fine elsewhere in the CP and on the product page, this only happens in the slideout.

Steps to reproduce

  1. Click on the button to open the slideout panel
  2. Check the console

Expected behavior

Craft's JS is loaded and is not blocking custom scripts from loading.

Actual behavior

Craft's JS is missing and other scripts are not loaded.

Craft CMS version

5.5.0.1

PHP version

8.2.24

Operating system and version

Linux 6.10.11-linuxkit

Database type and version

MySQL 8.0.36

Image driver and version

Imagick 3.7.0 (ImageMagick 6.9.11-60)

Installed plugins and versions

phrichards commented 6 days ago

I'm actually noticing something similar when editing a matrix field. When I choose to edit the entry types the slideout opens and I am able to save it, but I can't drag fields into the field layout and I see this error in the console:

Image

brandonkelly commented 6 days ago

You should only be passing the action into CpScreenSlideout, not the full URL to an action:

const slideout = new Craft.CpScreenSlideout('product-configurator/default');

Does that help?

phrichards commented 5 days ago

That did it! I think I tried a few variations of that with the same issue and concluded that wasn't the cause, guess I missed the one I needed. Thanks!

brandonkelly commented 5 days ago

Sweet! Did that fix the Craft.SortableCheckboxSelect error as well? If not, can you share your plugin/module code with us, along with steps to reproduce? (support@craftcms.com)