Skip to content

What’s the best way to load a field’s assets only when the field is actually rendered? #45861

@tassosm

Description

@tassosm

I have a custom field assigned to an article, with the “Automatic Display” option set to “Do not automatically display”. The article does not include a {field.xxx} shortcode either.

So, in this case, the field is not rendered on the frontend — as expected.

However, if the field layout (tmpl/myfield.php) includes asset loading (CSS or JS), those assets are still loaded into the DOM.

My question

Is Joomla still rendering the field’s layout even when the field isn’t going to be displayed?

If yes:

  • Is this considered expected behavior, or is it a bug?
  • What’s the recommended way to load a field’s CSS/JS only when the field is actually rendered?

Additional context

This happens consistently across all custom fields I’m offering through my Advanced Custom Fields extension.

To rule out issues in my implementation, I also ran a test using Joomla’s core Text field:

  • I modified its layout to load a dummy CSS file.
  • I set the field’s Automatic Display option to “Do not automatically display”.
  • I confirmed that the field does not appear on the frontend — but the CSS file was still injected into the DOM.

This suggests Joomla calls the layout even when the field isn’t going to be displayed.

Is there a clean or official way to detect whether the field will be actually rendered, so that assets are only loaded when necessary?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions