Search
Other Articles Anywhere questions
Forum

Alternate Layout for Custom Fields

Marc's Avatar Marc
Hi Peter 🙂

This ticket goes beyond the scope of your extensions but if you can have a look at it it would be very much appreciated.
I am indeed preparing an article for the next Joomla Community Magazine, illustrating the new Type of Custom Field introduced in J!4, namely the CF of Type SubForm.

In the example, I am playing with the Alternate Layout (very well explained in regularlabs.com/blog/1360-how-to-create-...tom-fields-in-joomla 🙂 Txs for that!).
For the sake of the example I display a carousel for each article. But we could take any other example (OpenStreetMap, ...).

The context is the following: for a carousel, an OpenStreetMap etc, since there is javascript involved you need a unique #id for the <div> if there are multiple articles on a page (like for a Blog view).

So the most straighforward way to do that would be to fetch the Article ID itself... but the latter is not available in $displayData.

Do you see any way or any trick to get the ID of the Article within the Alternate Layout for a Custom Field ?

Since you've played a lot with those things with Article Anywhere, I would be happy to get your insights 🙂
Peter van Westen's Avatar Peter van Westen ADMIN
No, I wouldn't know how to do that.
The layout is not aware of whatever calls it.

You could get the article based on the id in the URL (input data), but that will only work on article views, not on other places articles show up (like category blog pages).

If you have control over the field itself, you can probably do something in the onCustomFieldsPrepareField($context, $item, $field) method inside the main field class. I believe the $item will be the article or whatever the custom field is attached to.
Please post a rating at the Joomla! Extensions Directory
Andrea Malalana's Avatar Andrea Malalana ADMIN
This may be a long shot, but... You could also try something like:
<div id="{article-nested}[id]{/article-nested}">
in your custom field layout override...
Marc's Avatar Marc
Thank you very much Peter & Andrea!

@Peter: I will investigate that

@Andrea: I will test that... as soon as Articles Anywhere is available for J!4 😁 (since I am speaking here about a CF of Type SubForm, which is new since J!4)
Peter van Westen's Avatar Peter van Westen ADMIN
You can test the new Articles Anywhere for Joomla 4 (nearly ready to release) from here:
regularlabs.com/development-releases
Please post a rating at the Joomla! Extensions Directory
Nicolas OLIVIER's Avatar Nicolas OLIVIER
Lol @marc ^^ you v'e got same anwser two time this day ^^ "The layout is not aware of whatever calls it." The obvious way is to override the blog's children view, call your alternate layout from there.
Marc's Avatar Marc
Hi Nicolas.

I don't give up 😁
Sure if I make an override of the blog view I can reach the result.
But my goal was to get something more universal.

And I think I found it exactly one hour ago.
No time to test it today but if it works you will see the result in the next JCM edition 😉
Marc's Avatar Marc
I could not wait: I have made the test and it works 🙂

Next to the Alternate Layout for components/com_fields/layouts/field/render.php, simply make an override of
components/com_fields/layouts/fields/render.php and there change only one line, namely
$content = FieldsHelper::render($context, 'field.' . $layout, array('field' => $field));
into
$content = FieldsHelper::render($context, 'field.' . $layout, array('item' => $item, 'field' => $field));
and then in the Alternate Layout
echo $displayData['item']->id;

Txs again to all of you for your kind help.
Nicolas OLIVIER's Avatar Nicolas OLIVIER
Hooo never thought of using the render helper with item in the displaydata argument even if it seems obvious now ^^
GG Marc !
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription