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.