0

I have been testing the following code to retrieve and output the values from a repeatable subform field with no success.

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;
}

$programFields = json_decode($fields['programs']);

The $programs field is the repeatable field with three sub fields attached to it. When I var_dump $programFields, the following error is displayed. Warning: json_decode() expects parameter 1 to be string, object given.

If I var_dump `$fields['programs'], the following is outputted.

object(stdClass)[1473]
  public 'id' => string '16' (length=2)
  public 'title' => string 'Programs' (length=8)
  public 'name' => string 'programs' (length=8)
  public 'checked_out' => string '910' (length=3)
  public 'checked_out_time' => string '2020-03-17 18:15:15' (length=19)
  public 'note' => string '' (length=0)
  public 'state' => string '1' (length=1)
  public 'access' => string '1' (length=1)
  public 'created_time' => string '2020-02-21 14:55:50' (length=19)
  public 'created_user_id' => string '910' (length=3)
  public 'ordering' => string '0' (length=1)
  public 'language' => string '*' (length=1)
  public 'fieldparams' => 
    object(Joomla\Registry\Registry)[1344]
      protected 'data' => 
        object(stdClass)[1353]
          public 'fields' => 
            object(stdClass)[1348]
            ...
              protected 'initialized' => boolean true
              public 'separator' => string '.' (length=1)
              public 'params' => 
                object(Joomla\Registry\Registry)[1346]
                  protected 'data' => 
                  object(stdClass)[1355]
                    public 'hint' => string '' (length=0)
                    public 'class' => string '' (length=0)
                    public 'label_class' => string '' (length=0)
                    public 'show_on' => string '' (length=0)
                    public 'render_class' => string '' (length=0)
                    public 'showlabel' => string '0' (length=1)
                    public 'label_render_class' => string '' (length=0)
                    public 'display' => string '0' (length=1)
                    public 'layout' => string '' (length=0)
                    public 'display_readonly' => string '2' (length=1)
                    protected 'initialized' => boolean true
                    public 'separator' => string '.' (length=1)
                    public 'type' => string 'repeatable' (length=10)
                    public 'default_value' => string '' (length=0)
                    public 'context' => string 'com_content.article' (length=19)
                    public 'group_id' => string '0' (length=1)
                    public 'label' => string 'Programs' (length=8)
                    public 'description' => string '' (length=0)
                    public 'required' => string '0' (length=1)
                    public 'language_title' => null
                    public 'language_image' => null
                    public 'editor' => string 'Mike Hermary' (length=12)
                    public 'access_level' => string 'Public' (length=6)
                    public 'author_name' => string 'Mike Hermary' (length=12)
                    public 'group_title' => null
                    public 'group_access' => null
                    public 'group_state' => null
                    public 'group_note' => null
                    public 'value' => string '{"programs0":{"Image":"","Title":"Preschool to Grade 1\/2","Sypnosis":"<h4>Sypnosis<\/h4>\r\n<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"},"programs1":{"Image":"","Title":"Grades 2\/3 - 6","Sypnosis":"<h4>Syp'... (length=2335)
                    public 'rawvalue' => string '{"programs0":{"Image":"","Title":"Preschool to Grade 1\/2","Sypnosis":"<h4>Sypnosis<\/h4>\r\n<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"},"programs1":{"Image":"","Title":"Grades 2\/3 - 6","Sypnosis":"<h4>Syp'... (length=2335)

The programs repeatable field has the following stored in it's fieldparams table column:

{"fields":{"fields0":{"fieldname":"Image","fieldtype":"media","fieldfilter":"0"},"fields1":{"fieldname":"Title","fieldtype":"text","fieldfilter":"JComponentHelper::filterText"},"fields2":{"fieldname":"Sypnosis","fieldtype":"editor","fieldfilter":"safehtml"},"fields3":{"fieldname":"Duration","fieldtype":"text","fieldfilter":"JComponentHelper::filterText"},"fields4":{"fieldname":"Class Size","fieldtype":"text","fieldfilter":"JComponentHelper::filterText"}}}

Repeatable Field plugin override code

$fieldValue = $field->value;
echo $fieldValue;

Updated tested code

$programFields = json_decode($fields['programs']->rawvalue);
echo $programFields->Title;

The following error is displayed from this code: Notice: Undefined property: stdClass::$Title.

How would I output the individual repeating fields?

Could someone explain why this issue is occurring? All other custom fields are functioning correctly.

1

$fields['programs'] is already an object. Run json_decode() only on its value.

$programFields = json_decode($fields['programs']->rawvalue);
| improve this answer | |
  • I have implemented that code, but am still having issues. I have added additional code to my question showing what I have tested. – Mike Hermary Mar 17 at 21:05
0

Here is the code that finally worked for me. Thanks to @Sharky for the tips. Hopefully it is helpful to others.

Example working code

<?php
$programs = json_decode($fields['programs']->rawvalue);
$class = $fields['programs']->params->get('render_class');
?>
<div class="programs">
<?php foreach($programs as $program): ?>
    <div class="program <?php echo $class; ?>">
        <?php if(isset($program->Image) && !empty($program->Image)): ?>
            <div class="program__image">
                <?php echo $program->Image; ?>
            </div>
        <?php endif; ?>
        <?php if(isset($program->Title) && !empty($program->Title)): ?>
            <h3 class="program__title">
                <?php echo $program->Title; ?>
            </h3>
        <?php endif; ?>
        <?php if(isset($program->Sypnosis) && !empty($program->Sypnosis)): ?>
            <?php echo $program->Sypnosis; ?>
        <?php endif; ?>
        <div class="program__meta">
            <ul>
                <?php if(isset($program->Duration) && !empty($program->Duration)): ?>
                    <li>
                        <strong><?php echo JText::_('MRWC_PROGRAM_DURATION_TITLE'); ?></strong>
                        <?php echo $program->Duration; ?>
                    </li>
                <?php endif; ?>
                <?php if(isset($program->Size) && !empty($program->Size)) : ?>
                    <li>
                        <strong><?php echo JText::_('MRWC_PROGRAM_CLASS_SIZE_TITLE'); ?></strong>
                        <?php echo $program->Size; ?>
                    </li>
                <?php endif; ?>
                <?php if((isset($program->Duration) && !empty($program->Duration)) || (isset($program->Size) && !empty($program->Size))): ?>
                    <li>
                        <a href="mailto:education@name.com?subject=Booking for <?php echo $program->Title; ?> Program"><?php echo JText::_('MRWC_PROGRAM_BOOK_LABEL'); ?></a>
                    </li>
                <?php endif; ?>
            </ul>
        </div>
    </div>
<?php endforeach; ?>
| improve this answer | |
  • FYI, isset() is always redundant/unnecessary when followed by !empty() (e.g. if(isset($program->Image) && !empty($program->Image)). Why check both isset() and !empty() Making these many code adjustments WILL make your code more readable without sacrificing any code logic. Also you should urlencode() your entire mailto value for stability. The rewrite can look like this: 3v4l.org/JgYpH If Sharky's answer helped you, please remember to upvote his post. – mickmackusa Mar 18 at 20:10
  • 1
    @mickmackusa Thanks for the tips and suggestions. Your time and knowledge is greatly appreciated. I have streamlined my if statements and rewrote the email code. – Mike Hermary Mar 19 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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