3

I want to show some specific Custom Fields inside the Joomla 4 Articles News (mod_articles_news) module. I used to use this code in Joomla 3 :

<?php $customFields = FieldsHelper::getFields('com_content.article', $item, true);
    foreach ($customFields as $customField){
    $customFields[$customField->name] = $customField;
}?> 
<p><?php echo $customFields['name-of-the-custom-field']->value; ?></p>

but in Joomla 4 I am taking this error:

Class 'FieldsHelper' not found

Any help on this?

2
  • docs.joomla.org/API_Guides Around Joomla 3.8 the Joomla development team began changing the naming convention of Joomla classes to use namespaces, so that for example JFactory changed to Factory in the Joomla\CMS namespace. As you read existing Joomla code and documentation you may find the classes following either the new or the old naming standard. You can find the mapping between the two naming conventions in the libraries/classmap.php file in your Joomla instance.
    – framontb
    Jun 27, 2022 at 18:50
  • 1
    If the goal is to merely "associatively key" the first level of the object, then a concise functional-style alternative to the foreach() is: $nameKeyedCustomFields = array_column($customFields, null, 'name'); Jun 27, 2022 at 22:28

2 Answers 2

2

By adding this line

use Joomla\ Component\ Fields\ Administrator\ Helper\ FieldsHelper

before the close (?>) of

defined( '_JEXEC' )or die;

issue solved.

1

I did this little helper for getting the custom field values:

namespace Your\Component\Name\Administrator\Helper;

\defined('_JEXEC') or die;

use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

/**
 * Pro Fields Helper
 *
 * @since  __BUMP_VERSION__
 */
class ProFieldsHelper
{
    public $userKeyedCustomFields;

    function __construct(string $context, $item)
    {
        // Get field list
        $userCustomFields = FieldsHelper::getFields($context, $item, true);
        $this->userKeyedCustomFields = array_column($userCustomFields, null, 'name');
    }

    /**
     * Get the custom field value, or if empty the $default value
     */
    function getFieldValue(string $fieldName, string $default=null)
    {
        return $this->userKeyedCustomFields[$fieldName]->value ?? $default;
    }
}

So you can call it anywhere:

use Your\Component\Name\Administrator\Helper\ProFieldsHelper;
...
   // Get custom field 'telephone' for a user (defaults to '000000000')
   $user = Factory::getUser();
   $userCustomFields = new ProFieldsHelper('com_users.user', $user);
   $tel = $userCustomFields->getFieldValue('telephone', '000000000');

EDITED Applied changes proposed by @mickmackusa in his comment

1
  • 1
    I would probably condense the whole second method as a one-liner via the null coalescing operator. return $this->userKeyedCustomFields[$fieldName]->value ?? $default; As a general purpose method, I recommend not using empty() because it evaluates falseness loosely (will effectively destroy zero values). Versus my recommendation Jul 8, 2022 at 2:17

Your Answer

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

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