5

With Joomla 3.7.x article custom fields have finally gotten part of Joomla content.

However, I would like to ask how I can render these Custom Fields in the Joomla Category List?

Adding custom fields to the Joomla Category List is still not part of Joomla (hopefully it will come one day too).

So in order to add the custom fields to the Joomla Category List, I've made an template override of 'com_content/category/default_articles.php' to add two columns that I want to use to display Two Custom fields:

  • Date field with Title: 'Entry into force' and name 'gildiskoma'
  • Text field with Title 'Type' and name 'slag'

My current override of default_articles.php looks like this:

<?php /**--- Enable direct access by name to custom fields (Top) ----*/  ?>
<?php foreach($item->jcfields as $jcfield)
     {
    $item->jcFields[$jcfield->name] = $jcfield;
     }
?>

<?php /**--------- Custom Fields Header (Around line 120) -----------*/  ?>
<?php if ($this->params->get('list_show_votes')) : ?>
    <th id="categorylist_header_dato">
        <?php echo JHtml::_('grid.sort', 'LIST_GILDISKOMA', 'gildiskoma', $listDirn, $listOrder); ?>
    </th>
<?php endif; ?>
<?php if ($this->params->get('list_show_votes')) : ?>
    <th id="categorylist_header_type">
        <?php echo JHtml::_('grid.sort', 'LIST_SLAG', 'slag', $listDirn, $listOrder); ?>
    </th>
<?php endif; ?>

<?php /**--- Custom Category List Fields Content (Around line 250) ---*/  ?>
<?php if ($this->params->get('list_show_votes', 0) && $this->vote) : ?>
    <td headers="categorylist_header_date" class="list-gildiskoma">
        <?php echo $item->jcFields['gildiskoma']->value; ?>
    </td>
<?php endif; ?>
<?php if ($this->params->get('list_show_votes', 0) && $this->vote) : ?>
    <td headers="categorylist_header_type" class="list-slag">
        <?php echo $item->jcFields['slag']->value; ?>
    </td>
<?php endif; ?>

We don't use the article vote feature, so I've chosen to use the Custom Fields in its place as you might notice from the code above, which gives me two custom columns.

I've looked at the documentation for J3.x:Adding custom fields/Overrides and also found this post on How to fetch Joomla article custom fields in another component?

The documentation has a section explaining how to add individual fields with override. But my code above doesn't display the custom fields value.

So I'm not sure what I'm missing to get the fields value to show in the list?

Below also a screenshot which illustrates what I'm trying to do. Screenshot

5
  • maybe better consider to hire someone to work on your requirements, maybe
    – FFrewin
    Aug 2, 2017 at 12:46
  • It is possible to show custom fields in article listing, please refer to the official documentation on how to show them in overrides docs.joomla.org/J3.x:Adding_custom_fields/Overrides
    – Ervin
    Aug 15, 2017 at 8:12
  • I've looked at the documentation. I noticed it has recently been modified with how to load individual fields. But the code explained there is not working for me with the Category List, my custom two columns are still empty. I've modified my question with my current override code.
    – Marius
    Aug 16, 2017 at 11:36
  • Did you ever find a way to display custom fields in a category list?
    – user11704
    Oct 24, 2017 at 16:44
  • you was can resolve it? i have same problem. Dec 6, 2017 at 20:21

3 Answers 3

2

It is very easy to use the custom fields in article-list-view. It works out of the box. You don't have to use JLoader etc. And you don't have to define another "foreach"-argument.

Just look, how the "foreach" in default-articles.php for every line in the table is called:

<?php foreach ($this->items as $i => $article) : ?>

So you only have to change $this->item to $article

For example look how the hits will be created:

<?php echo JText::sprintf('JGLOBAL_HITS_COUNT', $article->hits); ?>

So you can see, that there is

$article->hits

instead of

$this->item->hits

Now to insert custom fields just modify the standard-code for custom-fields from this

<?php echo $this->item->jcfields[id]->value; ?>

to

<?php echo $article->jcfields[id]->value; ?>

So you can display every custom field with every id you like in columns for every line of the table.

In your example:

<?php /**--------- Custom Fields Header (Around line 120) -----------*/  ?>
<?php if ($this->params->get('list_show_votes')) : ?>
    <th id="categorylist_header_dato">
        <?php echo JHtml::_('grid.sort', 'LIST_GILDISKOMA', 'gildiskoma', $listDirn, $listOrder); ?>
    </th>
<?php endif; ?>
<?php if ($this->params->get('list_show_votes')) : ?>
    <th id="categorylist_header_type">
        <?php echo JHtml::_('grid.sort', 'LIST_SLAG', 'slag', $listDirn, $listOrder); ?>
    </th>
<?php endif; ?>

<?php /**--- Custom Category List Fields Content (Around line 250) ---*/  ?>
<?php if ($this->params->get('list_show_votes', 0) && $this->vote) : ?>
    <td headers="categorylist_header_date" class="list-gildiskoma">
        <?php echo $article->jcfields['gildiskoma']->value; ?>
    </td>
<?php endif; ?>
<?php if ($this->params->get('list_show_votes', 0) && $this->vote) : ?>
    <td headers="categorylist_header_type" class="list-slag">
        <?php echo $article->jcfields['slag']->value; ?>
    </td>
<?php endif; ?>
1
  • This is a much easier solution then the other one and works great! Simple yet not directly obvious, as the documentation doesn't mention the use of $article->jcfields at all.
    – Marius
    Feb 24, 2018 at 11:39
2
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$customFields = FieldsHelper::getFields('com_content.article', $articleitem, true);
$fields = array();
foreach ($customFields as $field)
{
    $fields[$field->name] = $field->value ? $field->rawvalue : $field->default_value;
}

Yay!

2

My solutions to get 2 new columns in my com_content/category/default_articles.php template override. These docs.joomla and the above replies helped me a lot.

In the <?php foreach ($this->items as $i => $article) : ?> loop. Where all the item info is collected place:

<!-- this code to grab the fields by use of the fieldshelper -->

<?php JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); ?>
<?php foreach($article->jcfields as $jcfield)
   {
    $article->jcFields[$jcfield->name] = $jcfield;
    }
?>

Place the extra columns header info in tableheaders

<!-- table header -->
<th>
    your field 1:
</th>
<th>
    your field 2:
</th>

And finally place the custom field info per item in the tbody. Like this:

<td><?php echo $article->jcFields['your-field-1']->value; ?></td>

<td><?php echo $article->jcFields['your-field-2']->value; ?></td>

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