2

I am creating an Alternate Layout of /components/com_fields/layouts/field/render.php to change the layout of some given Custom Fields. And there I need the ID of the article in order to give a unique #id to the container.

There I wanted/needed to fetch the Article ID, but the Layout does not seem to have any information about the context.

I had first thought that the following could work:

$id   = $app->input->getCmd('id', '');

It indeed gives the article id... but only if you are on an Article View. If you are on a Blog View, it gives the ID of the category.

So would there be some direct or indirect way (by overriding another file for example) to get that Article ID in a Blog view?

Txs

1

Article data is not available in that layout. What you can do is create an override of components/com_fields/layouts/fields/render.php and modify it to pass the article to field/render.php layout. You'd have to change this line:

$content = FieldsHelper::render($context, 'field.' . $layout, array('field' => $field));

To this:

$content = FieldsHelper::render($context, 'field.' . $layout, array('item' => $item, 'field' => $field));

And then in the field/render.php layout access the article ID like this:

$id = $displayData['item']->id;
1
  • Thank you soooooooo much @Sharky for this answer! It works perfectly and avoids making an override/alternate layout for the blog view itself. This is a much more universal solution indeed. Sep 26 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.