craftcms / cms

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

[4.x]: CP: JS file with dependency on asset bundle not being loaded #13792

Closed seibert-io closed 11 months ago

seibert-io commented 11 months ago

What happened?

Description

The described issue has been observed with plugin newism/craft3-fields@2.0.0, but appears to be a general issue. And yes, despite its name, newism/craft3-fields is compatible with Craft CMS 4.

Plugin newism/craft3-fields registers one asset bundle and one JS file with a dependency to the asset bundle (both for CP only). The asset bundle assets are being rendered to the markup and are loaded, whereas the JS file with the dependency to those are not.

An issue with the plugin was suspected at first. However, the plugin seems to correctly registering both asset bundle and JS file and it was verified the PPH code registering:

An interference with other plugins was excluded by disabling all plugins except newism/craft3-fields.

Steps to reproduce

  1. Install plugin newism/craft3-fields and set a Google Maps API key in the plugins settings (CP -> Settings -> NSM field settings)
  2. Create a field of type "Address (Newism)" with Option "Show autocomplete" and place it in a sections entry type
  3. Open the edit entry page with an entry of this entry type
  4. Address.js is loaded, https://maps.googleapis.com/maps/api/js isn't

Expected behavior

Both, the asset bundles assets and the extern JS file are being loaded.

Actual behavior

Only the asset bundle assets are being loaded. The registered JS file is not.

Craft CMS version

4.5.6.1

PHP version

8.2.10

Operating system and version

No response

Database type and version

MariaDB 10.3

Image driver and version

No response

Installed plugins and versions

i-just commented 11 months ago

Hi, thanks for getting in touch!

It looks like Yii doesn’t quite like that the Google Maps js that’s being included via registerJsFile doesn’t have the 3rd parameter ($key). Adding e.g. 'googleMaps' as the 3rd parameter here: https://github.com/newism/craft3-fields/blob/1249214be31a0c4369a80e7e4f762f0b96327104/src/fields/Address.php#L188-L196 fixes this issue. Additionally, since in that registerJsFile() you’re saying that it depends on AddressFieldAsset, you don’t necessarily have to register it separately here.

Another way to go about this would be to create a bundle for the Google Maps js, where the $this->js array contains the absolute URL to the maps.googleapis.com and then have the AddressFieldAsset include it as a dependency.

I hope this helps. I’m going to close this now, but please don’t hesitate to reach out if you have any further questions or if any of the above doesn’t make sense.