In this tutorial you will learn how to create Layout Overrides for Joomla! 4 Custom Fields, when displaying them with Articles Anywhere in Joomla! 4.

Introduction

With the Pro version of Articles Anywhere, you can display any Joomla! Custom Field of an article using the name of that specific field.

Let's say you want to show the value of the custom field "Habitat" - that has a field name habitat-area - of article "Lions". You can simply do:

{article Lions}[habitat-area]{/article}

If you assigned a Layout to your custom field in its Render Options, Articles Anywhere will account for it and output the field using that layout (without the label).

But in the data tag you are able to customize the output of the custom field by setting an alternate Layout. Let's take a look at how this is done:

Create Alternate Field Layout

First, to know exactly how to create an alternative layout file, and to see some layout examples, follow our tutorial on How to create an Alternate Layout for Custom Fields in Joomla.

Now, if you assigned this layout to the fields in its Render Options, this would also apply to the standard output of the field. Which we don't want, since we only want to use this alternate layout in Articles Anywhere, without affecting the standard output.

Using the Layout in Articles Anywhere

As mentioned, by default Articles Anywhere will use the Layout assigned to that custom field in its Render Options.

However, in the data tag you can override the set layout with another one (to be placed inside templates/my_template/html/layouts/com_fields/field/), by simply inserting the name of the file in the layout="..." attribute:

[habitat-area layout="habitat-field"]

Value Layout (New Approach) J4

The Joomla 3 version of Articles Anywhere also had the ability to customize the output specifically related to the "value" part of a custom field.

This feature has been removed from the Joomla 4 version, as we now made it easier to achieve the same result via different methods.

In particular, here is how to replicate the same Value Layout examples that were described for Joomla 3:

Example: List Field

Customizing Value Layouts is especially useful if you need further control on custom field types that have multiple values, such as in fields of type "List", or "Checkboxes".

In Articles Anywhere for Joomla 4, you can now use the new Foreach Structure feature on List Fields to have full control over the output of the values:

<ul>
{foreach data="habitat-area"}
<li>[row]</li>
{/foreach}
</ul>

Check the dedicated page for details on how to use it, which includes how to output the multiple values in a bullet points list.

Example: Articles Field

Articles Field from Regular Labs is a Joomla! custom field type that gives you the ability to choose and display a list of articles, allowing you to directly link articles to each other.

The Pro version of Articles Field already allows to fully customize the output of the linked articles in the article page, thanks to its Custom HTML Layout feature. But what if you would like to output an Articles Field with a different/alternate layout when displaying it through Articles Anywhere? You can, too!

Since Articles Anywhere for Joomla 4 now supports the ability to override field attributes inside the data tag, this means that you no longer need to create an alternate PHP layout for Articles Fields.

What you can do instead, is simply override the custom_html parameter to pass another Custom HTML Layout to the field. 

So if you have an Articles Field called [speaker], you can simply override the Custom HTML Layout set in the field parameters by doing:

[speaker custom_html="<a href='[url]'>[title]</a>"]

And yes, you can use any of the data available for the Articles Field Layout, along with any Custom HTML you want. The only thing you have to be careful of, is the editor not messing up your syntax. So you may need to use single quotes instead of double quotes, but that's just fine as well.

You can even add/override separators between the articles returned by Articles Field, if you desire:

[speaker separator=", " last-separator=" and " custom_html="<a href='[url]'>[title]</a>"]

For a complete overview of all the features included in Articles Anywhere, head over to the full Documentation.