I am struggling to find a way to output the values from a repeatable field in Joomla 3.9.15.
I am currently accessing the custom fields assigned to articles using the following code:
JHtml::addIncludePath(JPATH_COMPONENT . '/helpers');
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$context = 'com_content.article';
$article = $this->item;
$fields = FieldsHelper::getFields($context, $article, true);
foreach($fields as $field) {
$fields[$field->name] = $field;
}
I am outputting single fields using the following code:
<?php if($fields['article-subheading']->value != ''): ?>
<?php $class = $fields['article-subheading']->params->get('render_class'); ?>
<h3 class="<?php echo $class; ?>">
<span><?php echo $fields['article-subheading']->value; ?></span>
</h3>
<?php endif; ?>
Would I need to select the repeatable field, assign it to a variable, then use json_decode
? I see the repeatable field subform code is stored as JSON in the database. See example below:
{"programs0":{"Image":"","Title":"Preschool to Grade 1\/2","Sypnosis":"<p>We introduce the children to the concept of wildlife rehabilitation and the differences between wild and domestic animals. We use storytelling, pictures and one or more of our education animals. The children learn how every one of them can make a difference to help wildlife.<\/p>","Duration":"30 to 40 minutes","Class Size":"Min - 12, Max - 30"}}
$items
is an indexed array, don't manually inctement your own counter, useforeach($items as $i => $program):
...or don't declare it all, because you don't seem to be using it.