custom repeatable fields in joomla

I really cant understand why frontend templates in joomla always miss the last step. Basic standard templates never show the potential under the hood.

My previous article explained how to assign and call custom fields in your template layout. When the awesome "repeatable custom fields" came out with 3.9 I believed it was going to work the same exact way. I was wrong. When you call your custom field var contianing the sub repeated fields you get an ugly useless unordered list. So I had to find a way to extract them...

That's why I'm taking this new note, to make it work as I expected. If you did not, read the previous article Custom fields in joomla, the right way!

Describing step by step for everyone who will look for it.

Here how I solved:

1: Remember to call and assign to variables your custom fields, in the template (article, featured or category):

$myFields = array();
foreach($this->item->jcfields as $field) {
$myFields[$field->name] = $field->value;

2: The file responsible in echoing the unordered list of repeatable subfield is "\plugins\fields\repeatable\tmpl\repeatable.php". Override it.

3: Repeatable.php is getting a json string from database, with a sequence of all your subfields. The variable $fieldvalue is all we need, just echo it. Replace the following code:

$html = '<ul>';
foreach ($fieldValues as $value)
    $html .= '<li>' . implode(', ', $value) . '</li>';
$html .= '</ul>';
echo $html;

echo $html;


echo $fieldValue;

4: In the template file json decode the repeatable field and assign to a variable. The second parameter (true) in json_decode it's the magic of transforming a static string in a perfect php array.

$dataRepeatableField = json_decode($myFields['repeatable_filed_name'], true);

5: Done! It's now possible to call each repeatable field, stored in a multidimensional array, like so:

echo $dataRepeatableField['fieldname']['repeatablefieldname'];

A foreach loop can echo all the fields formatted the way you want!

I'm quite proud of the result. The potential is huge: you can create and layout custom content with a variable quantity of custom fields in each article content, without using any third party extension.

For example an image gallery with captions, custom size and layout option for each item.  Or everything you can invent mixing the standard joomla fields. The basic joomla templates are just ignoring all the potential of custom fields.

Good coding.