Displaying custom fields in mod_articles_category Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Post Reply
GillianBPP
Joomla! Explorer
Joomla! Explorer
Posts: 378
Joined: Wed Oct 01, 2014 10:25 am

Displaying custom fields in mod_articles_category

Post by GillianBPP » Sun Feb 05, 2023 3:29 pm

I have an articles category module in which I am displaying three custom fields for a season of concerts. I have created an override for this module called 'season.php', along with 'season_items.php'. In the head of the 'season_items.php' I have added this code:

Code: Select all

use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
and within the 'li' tags I have added these three divs:

Code: Select all

<div class="field-1"><?php $item->jcfields = FieldsHelper::getFields('com_content.article', $item, true);echo FieldsHelper::render('com_content.article', 'field.render', ['field'=>$item->jcfields[1]]);?></div>
	<div class="field-2"><?php $item->jcfields = FieldsHelper::getFields('com_content.article', $item, true);echo FieldsHelper::render('com_content.article', 'field.render', ['field'=>$item->jcfields[2]]);?></div>
	<div class="field-3"><?php $item->jcfields = FieldsHelper::getFields('com_content.article', $item, true);echo FieldsHelper::render('com_content.article', 'field.render', ['field'=>$item->jcfields[3]]);?></div>
This seems to be a rather long way of going about it. Is there a way to shorten the code so that all I have to do is add, for example,

Code: Select all

<?php echo $item->jcfields[n]->value; ?>
which is what I did in J3 but doesn't work in J4?

Also, I noticed that in using this route the field numbers are NOT the field IDs but rather the sequential order in which they are arranged - starting from 0 - and how they appear in the article editor fields tab, which could have disastrous consequences if I were to rearrange the fields. This is not how it works in articles, though. When manually adding fields to an article layout - i.e. not automatically displayed - and using the short code above the field IDs are used.
Last edited by toivo on Mon Feb 06, 2023 6:42 am, edited 1 time in total.
Reason: mod note: moved from 4.x Administration

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 15858
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Displaying custom fields in mod_articles_category

Post by toivo » Mon Feb 06, 2023 7:32 am

GillianBPP wrote:
Sun Feb 05, 2023 3:29 pm
I noticed that in using this route the field numbers are NOT the field IDs but rather the sequential order in which they are arranged - starting from 0 - and how they appear in the article editor fields tab, which could have disastrous consequences if I were to rearrange the fields.
You are absolutely right. The field names are easier to maintain and they make the code self-documenting.

This example is from an article override, templates/cassiopeia/html/com_content/article/default.php. It uses $this->item instead of $item in your module, but the principle is the same. The heavy lifting is done by the PHP function array_column(), which makes the field names accessible:

Code: Select all

<?php
$customFields = FieldsHelper::getFields('com_content.article', $this->item, true);
$values = array_column($customFields, 'value', 'name');
echo 'Field A = ' . $values['field-a'] . ' Field B = ' . $values['field-b'] . ' Field C = ' . $values['field-c'];	
?>
Toivo Talikka, Global Moderator

GillianBPP
Joomla! Explorer
Joomla! Explorer
Posts: 378
Joined: Wed Oct 01, 2014 10:25 am

Re: Displaying custom fields in mod_articles_category

Post by GillianBPP » Mon Feb 06, 2023 3:21 pm

Brilliant! Thank you, @Toivo.

As this is going in a mod_articles_category, a version of your code goes into the 'li' tags. As I want each field to be displayed in a separate div with its own CSS class, I don't need to repeat the entire code for each div, just the 'echo' part. So, for the benefit of anyone else wanting to do this, my code now looks like this:

Code: Select all

<?php

/**
 * @package     Joomla.Site
 * @subpackage  mod_articles_category
 *
 * @copyright   (C) 2010 Open Source Matters, Inc. <https://www.joomla.org>
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

/* this file is an override of mod_articles_category/default_items.php and named season_items.php */

defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
?>
<?php foreach ($items as $item) : ?>
<li>
	<div class="my-class-one"><?php $customFields = FieldsHelper::getFields('com_content.article', $item, true); $values = array_column($customFields, 'value', 'name');
	echo $values['field-name-1']; ?></div>
	<div class="my-class-two">echo $values['field-name-2']; ?></div>
	<div class="my-class-three">echo $values['field-name-3']; ?></div>
</li>
<?php endforeach; ?>
There is a parent file which is an override of mod_articles_category default.php - in my case it's called season.php and lines 27 and 33 have been changed from:

Code: Select all

                <?php require ModuleHelper::getLayoutPath('mod_articles_category', $params->get('layout', 'default') . '_items'); ?>
to this:

Code: Select all

                <?php require ModuleHelper::getLayoutPath('mod_articles_category', $params->get('layout', 'season') . '_items'); ?>


Post Reply

Return to “Joomla! 4.x Coding”