?
avatar David2376
David2376
23 Jul 2021

Steps to reproduce the issue

Create a custom field and try to load it via php.

// Load the FieldsHelper
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

// Load individual fields
foreach($item->jcfields as $jcfield)
{
$item->jcFields[$jcfield->name] = $jcfield;
}

// Display individual field
echo $item->jcFields['name-of-field']->rawvalue;

Reference article: https://docs.joomla.org/J3.x:Adding_custom_fields/Overrides/en

Expected result

You should be able to access the fields using:
echo $item->jcFields['name-of-field']->rawvalue;

Actual result

Get an error/warning and custom fields cannot be loaded via php.

Warning: Creating default object from empty value in {FILENAME}

System information (as much as possible)

Joomla 4.0.0 RC 4

Additional comments

Was working properly in RC 1...did not test it in RC 2 or 3. Not working in RC 4.

avatar David2376 David2376 - open - 23 Jul 2021
avatar joomla-cms-bot joomla-cms-bot - labeled - 23 Jul 2021
avatar David2376
David2376 - comment - 23 Jul 2021

Some of the code was cut out; see the reference article for the code that should work.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/34876.

avatar David2376
David2376 - comment - 23 Jul 2021

Actually after further investigation this appears to be working properly but still throws a warning. Joomla documentation needs to be updated for Joomla 4. Joomla 3 and Joomla 4 RC 1 worked properly with no warnings. To prevent warning you need to set the custom field array to an empty array before the for each statement.

// Load the FieldsHelper
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

// ***** ADD THIS *****
$item = new stdClass();

// Load individual fields
foreach($item->jcfields as $jcfield) :
$item->jcFields[$jcfield->name] = $jcfield;
endforeach;

// Display individual field
echo $item->jcFields['name-of-field']->rawvalue;


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/34876.
avatar David2376
David2376 - comment - 23 Jul 2021

This issue needs to be marked as closed but documentation for Joomla 4 needs to be updated.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/34876.

avatar David2376 David2376 - change - 23 Jul 2021
The description was changed
avatar David2376 David2376 - edited - 23 Jul 2021
avatar alikon alikon - close - 23 Jul 2021
avatar alikon
alikon - comment - 23 Jul 2021

https://docs.joomla.org/ is an open wiki feel free to update the documentation

avatar alikon alikon - change - 23 Jul 2021
Status New Closed
Closed_Date 0000-00-00 00:00:00 2021-07-23 06:27:35
Closed_By alikon

Add a Comment

Login with GitHub to post a comment