2

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"}}
7
  • Where are you overriding? Asking because this is the title of your question, but what you do isn't looking like an override of the repeatable field. Anyway, yes - after having your $repeatableField in your $fields array, what type of content does it have in it? You certainly have it in your hands at this point and can json_decode it and loop over it to output its contents the way you want.
    – FFrewin
    Commented Feb 21, 2020 at 18:12
  • @FFrewin I have updated my question with the current working code. I would prefer to loop over the code instead of manually echoing objects. Any tips are appreciated. Commented Feb 21, 2020 at 18:53
  • @Mike it is completely aceptable and in this case most appropriate for you to shift your resolution from your question to a new answer to your question. Afterwhich, please award the green tick to your answer to mark this page as resolved within the system. (And of course, please rollback your edit so that only your question details exist in your question. Commented Feb 22, 2020 at 0:06
  • Assuming $items is an indexed array, don't manually inctement your own counter, use foreach($items as $i => $program): ...or don't declare it all, because you don't seem to be using it. Commented Feb 22, 2020 at 0:09
  • @mickmackusa I have reverted my question to the original and move my solution into an answer. I can accept it in 2 days. Commented Feb 22, 2020 at 2:02

1 Answer 1

2

Here is the solution I came up with for my question:

$programs = $fields['programs']->rawvalue;
$class = $fields['programs']->params->get('render_class');
$items = json_decode($programs, true);

<?php $i = 0; foreach($items as $program): ?>
<div class="program <?php echo $class; ?>">
    <div class="program__image">
        <?php echo $program['Image']; ?>
    </div>
    <h3 class="program__title">
        <?php echo $program['Title']; ?>
    </h3>
    <?php echo $program['Sypnosis']; ?>
    <div class="program__meta">
        <ul>
            <li>
                <strong><?php echo JText::_('MRWC_PROGRAM_DURATION_TITLE'); ?></strong>
                <?php echo $program['Duration']; ?>
            </li>
            <li>
                <strong><?php echo JText::_('MRWC_PROGRAM_CLASS_SIZE_TITLE'); ?></strong>
                <?php echo $program['Class Size']; ?>
            </li>
            <li>
                <a href="mailto:[email protected]?subject=Booking for <?php echo $program['Title']; ?> Program"><?php echo JText::_('MRWC_PROGRAM_BOOK_LABEL'); ?></a>
            </li>
        </ul>
    </div>
</div>
<?php $i++; endforeach; ?>
1
  • For researchers studying this snippet, $i is not used, so all references to it can be removed. Commented Mar 18, 2020 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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