How to Access Joomla 3.7 Custom USER Field?

1,796 views
Skip to first unread message

Mhlonishwa Njabulo Mkhize

unread,
May 25, 2017, 2:54:16 PM5/25/17
to Joomla! General Development
Hello Guys
I am new to Joomla and I need help in ENGLISH, please
I have created a Custom Field for Users, I want to fetch and display its content on the page right under the name of the User. I am able to fetch the name of the User with the Joomla User Object ($user = JFactory::getUser();) but, this does not help me get the Custom Field.
If you know how to do this, please help me in simple English, I am not a Geek just yet, I am working on it though.
Thank you.

Allon Moritz

unread,
May 28, 2017, 8:18:05 AM5/28/17
to joomla-de...@googlegroups.com
There is a similar question here https://groups.google.com/forum/m/#!topic/joomla-dev-general/d47-L1jnrNU

Basically you nees to trigger an event on the user object.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

Mhlonishwa Njabulo Mkhize

unread,
May 29, 2017, 7:41:43 AM5/29/17
to Joomla! General Development
Thanks laoneo
But how to do that? How to trigger that event? How to then access the content of the custom field? Would you please explain briefly so that I can learn and understand this thing?

Hils

unread,
May 30, 2017, 8:37:34 AM5/30/17
to Joomla! General Development
Have you checked the Joomla Documentation? for example:

https://docs.joomla.org/J3.x:Adding_custom_fields (introduction so look at the index for further info)

Mhlonishwa Njabulo Mkhize

unread,
May 31, 2017, 11:56:17 AM5/31/17
to Joomla! General Development
Hi Hils
I have checked that documentation, it does not answer my question.
I am not asking how to add or create the custom fields, I am asking how to access the data stored by them and display it wherever I want?

Ed Stafford

unread,
May 31, 2017, 12:20:56 PM5/31/17
to Joomla! General Development
I guess a better question might be:  How are you using the field?  In an article?  There's a 'Field' button on the TinyMCE panel.  

Mhlonishwa Njabulo Mkhize

unread,
May 31, 2017, 12:33:37 PM5/31/17
to Joomla! General Development
Hi Ed Stafford

See, now we are getting somewhere. Here is my wish:
I want to have user accounts on my website, I want to have a page (I wish it can be an article) on which each user can see some of their profile information - the information I created Custom Fields for. Their image/photo, their national Identification Number or Passport Number. I want the flexibility to pick any field and display its content for the user logged-in on an article. I have been able to do this with the Name and email address of the user, but I could not be able to insert this into a line in an article - I had to make a space on the template frame and I don't like doing that. And with the User Custom Fields, I can't even pull that data to display it using the template frame.

I hope this helps to clear what I need help with.

Thank you for your time, Ed..

Ed Stafford

unread,
May 31, 2017, 3:40:46 PM5/31/17
to Joomla! General Development
That took more than a few minutes.  I can't say this is the perfect way, but it's as close as I'll get today:

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$fields = FieldsHelper::getFields('com_users.user',  JFactory::getUser());

foreach ($fields as $f) {
    echo $f->label . " = " . $f->value;
}

Stephen

unread,
Oct 31, 2017, 5:19:10 PM10/31/17
to Joomla! General Development
I guess Milo somehow figured it out or just got too frustrated at the incomplete answers. I'm a Joomla newbie as well and when I paste your code into a PHP page. I get this error:

"Fatal error: Class 'JLoader' not found"

I have looked all over the internet to find a way to load the values of my custom USER fields after a user registers.

I'm able to get all basic Joomla user fields like username, email, user ID, address1, phone, etc... but NOT any of the custom user fields I setup in a new field group called "Business Information".

Why does Joomla not have decent information in their documentation for this (or anything really)? It's all isolated information without in-use examples – like this thread and many, many other Joomla forum threads on the web.

All I find is tiny snippets of code that require other code to work but usually no one provides the code in context (this includes Joomla documentation!). For newbies this is extremely FRUSTRATING.

I'm saying all of this in hopes seasoned Joomla developers will approach all answers as if they're speaking to newbies... cause that's typically who asks all these questions. I also hope those responsible for writing Joomla documentation will do better at adding complete code (i.e. include dependencies/context) to their examples.

ste...@gmail.com

unread,
Nov 1, 2017, 6:44:57 AM11/1/17
to Joomla! General Development
I think you will have better luck asking on the joomla stack exchange site.

Marvin_Martiano

unread,
Nov 1, 2017, 7:43:45 AM11/1/17
to Joomla! General Development
That seems to be another issue:

On Tuesday, October 31, 2017 at 9:19:10 PM UTC, Stephen wrote:
> "Fatal error: Class 'JLoader' not found" 

So..
1/ You've turned on DEBUG in global configuration, and also set ERROR REPORTING to development/max? Then it will tell you where stuff is missing exactly.
2/ You've checked JLoader is actually where it's supposed to be inside the file which is at the location it's being called to?
3/ You've checked file permissions? Because that exact error turns up if your file isn't 644 (say, 600) or its containing folder isn't 755 (say, 511); this would happen more frequently if you've ftp'd to a remote location, not working locally on (L/M/W)AMP.

Note that any results from 2/ or 3/ suggest your install has been damaged, so better start from scratch with what you're working on --- clean fresh install, no Fabrik/K2 ccc's, no fancy templates, ... . That's what I always do, maybe JCE and Akeeba Backup if your tweaking is sufficiently complicated (say something needing various overrides, of layout in the active template and of language; or maybe you've temporarily edited a core file so that would get lost if there's an update; or you're messing with multiple components and plugins at once).

Hannes Papenberg

unread,
Nov 1, 2017, 12:22:01 PM11/1/17
to joomla-de...@googlegroups.com
You are writing, that you are copying the given code in "a" PHP page and
I'm wondering if you are really talking about a separate PHP file. You
can use this code inside of Joomla, not in a standalone PHP file. I'm
currently wondering how to explain all of this without going into a
book-length answer here...

I'll try to make this short, even though this will frustrate you even
more: Please read up on Joomla extension types and convert your code to
a Joomla extension. Then the given code will work.

Hannes

Stephen Smiroldo

unread,
Nov 1, 2017, 5:03:24 PM11/1/17
to joomla-de...@googlegroups.com
Yes, I made an extension and need to retrieve the custom form fields as shown in my extension code snippet below. The code highlighted in yellow are the custom fields I would like to pull from Joomla.


------
define( '_JEXEC', 1 );

class PlgUserWebtocase_registrant extends JPlugin
{

public function onUserAfterSave($user, $isnew)
{

if ($isnew) {
 
$newUser = 1;
$userID = $user['id'];
$userEmail = $user['email'];
$userFirstLastName = $user['name'];
$userRegisterDate = $user['registerDate'];
$username = $user['username'];
 
$userProfile = JUserHelper::getProfile( $userID );
 
$address1 = $userProfile->profile['address1'];
$city = $userProfile->profile['city'];
$region = $userProfile->profile['region'];
$postal_code = $userProfile->profile['postal_code'];
$phone = $userProfile->profile['phone'];
if($address1 && $postal_code) {

$address=$address1.", ".$city.", ".$region." ".$postal_code;
$profession = $userProfile->profile['profession'];
$profession_title = $userProfile->profile['profession_title'];
$practice_name = $userProfile->profile['practice_name'];
$license_number = $userProfile->profile['license_number'];
$institution_attended = $userProfile->profile['institution_attended'];
$graduation_year = $userProfile->profile['graduation_year'];
$subscrb_email = $userProfile->profile['subscrb_email'];
$subscrb_mail = $userProfile->profile['subscrb_mail'];

}
}
}
------

--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/sDtU8B10WxI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to joomla-dev-general+unsubscribe@googlegroups.com.

Hannes Papenberg

unread,
Nov 1, 2017, 6:29:20 PM11/1/17
to joomla-de...@googlegroups.com
So, I've looked over this longer than I dare to admit and as far as I
can see, there is no automatic way that loads the data from the fields
into JUser or actually any other object/whatever. Long story short: You
seem to have to retrieve that data yourself from the database with a query.
> <https://groups.google.com/d/topic/joomla-dev-general/sDtU8B10WxI/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email to
> joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-general%2Bunsu...@googlegroups.com>.
> To post to this group, send an email to
> joomla-de...@googlegroups.com
> <mailto:joomla-de...@googlegroups.com>.
> <https://groups.google.com/group/joomla-dev-general>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.
> To post to this group, send email to joomla-de...@googlegroups.com
> <mailto:joomla-de...@googlegroups.com>.

Stephen Smiroldo

unread,
Nov 2, 2017, 2:25:14 PM11/2/17
to joomla-de...@googlegroups.com
Ok, thanks. I ran across that link Michel shared numerous times but can't get to pull up any data. This is what I'm trying in my extension file:

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$jcFields = FieldsHelper::getFields('com_users.user', JFactory::getUser( $userID ), false);
$profession = $jcFields[0]->value;
$profession_title = $jcFields[1]->value;

It doesn't error but it also doesn't load any values into the "profession" or "profession_title" variables. I'll just figure out a SQL query to do the job.




    To post to this group, send an email to

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsubscribe@googlegroups.com <mailto:joomla-dev-general+unsubsc...@googlegroups.com>.
To post to this group, send email to joomla-dev-general@googlegroups.com <mailto:joomla-dev-general@googlegroups.com>.
--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/sDtU8B10WxI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to joomla-dev-general+unsubscribe@googlegroups.com.
To post to this group, send an email to joomla-dev-general@googlegroups.com.

Marvin_Martiano

unread,
Nov 2, 2017, 5:15:23 PM11/2/17
to Joomla! General Development
YOu are right I'm afraid. I've now tried it myself on a memberlist thing I have -- I'm using a 'profile' plugin, so added some dummy fields. Doing the first two lines, then var_dump($jcFields); gives me loads of info about those custom fields, completely independent of that supplied userID (or maybe, it's all fields that that userID's ACL would permit)... It says who created the fields, their names, their possible values, ... , usergroups of the creator, userID of creator, ... .   So the last two lines won't do anything.

Allon Moritz

unread,
Nov 3, 2017, 3:30:19 AM11/3/17
to joomla-de...@googlegroups.com
The last argument in FieldsHelper::getFields('com_users.user', JFactory::getUser( $userID ), false); must be true.

To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send email to joomla-dev-general@googlegroups.com.

Stephen Smiroldo

unread,
Nov 3, 2017, 4:43:18 PM11/3/17
to joomla-de...@googlegroups.com
I was able to get the custom fields this way. Posting for any other Joomla noobies to use. Note, the "#" in the database table name is where your database prefix would be. I'm commenting out for security reasons. Some new to development may not know this.


class PlgUserWebtocase_registrant extends JPlugin
{

public function onUserAfterSave($user, $isnew)
{

if ($isnew) {
 
$newUser = 1;
$userID = $user['id'];
$userEmail = $user['email'];
$userFirstLastName = $user['name'];
$userRegisterDate = $user['registerDate'];
$username = $user['username'];
 
$userProfile = JUserHelper::getProfile( $userID );
 
$address1 = $userProfile->profile['address1'];
$city = $userProfile->profile['city'];
$region = $userProfile->profile['region'];
$postal_code = $userProfile->profile['postal_code'];
$phone = $userProfile->profile['phone'];
if($address1 && $postal_code) {

$address=$address1.", ".$city.", ".$region." ".$postal_code;
/*** GET CUSTOM FEILD VALUES ***/
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
    ->select($db->quoteName(array('field_id', 'item_id', 'value')))
    ->from($db->quoteName('#_fields_values'))
    ->where($db->quoteName('item_id') . ' LIKE '. $db->quote($userID))
    ->order('field_id ASC');
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects
$results = $db->loadObjectList();

// create an array of each custom field with the field ID value and value of the value column
$fieldArray = array ($results[0]->field_id => $results[0]->value, $results[1]->field_id => $results[1]->value, $results[2]->field_id => $results[2]->value, $results[3]->field_id => $results[3]->value, $results[4]->field_id => $results[4]->value, $results[5]->field_id => $results[5]->value, $results[6]->field_id => $results[6]->value, $results[7]->field_id => $results[7]->value);
$license_number = $fieldArray['1']; // lic#
$practice_name = $fieldArray['2']; // ClinicBiz name
$profession_title = $fieldArray['3']; // Title
$profession = $fieldArray['4']; // Profession
$institution_attended = $fieldArray['5']; // Institutution
$graduation_year = $fieldArray['6']; // Grad date
$subscrb_email = $fieldArray['7']; // subscr email
$subscrb_mail = $fieldArray['8']; // subscribe mail

/*** END Get Custom Field Values ***/


/*** Perform cURL operation to CRM ***/
/*** END cURL ***/
}
}
}
 
Reply all
Reply to author
Forward
0 new messages