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
You should be able to access the fields using:
echo $item->jcFields['name-of-field']->rawvalue;
Get an error/warning and custom fields cannot be loaded via php.
Warning: Creating default object from empty value in {FILENAME}
Joomla 4.0.0 RC 4
Was working properly in RC 1...did not test it in RC 2 or 3. Not working in RC 4.
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 issue needs to be marked as closed but documentation for Joomla 4 needs to be updated.
https://docs.joomla.org/ is an open wiki feel free to update the documentation
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2021-07-23 06:27:35 |
Closed_By | ⇒ | alikon |
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.