Displaying data from Community Builder in Joomla!
This is mostly for my own future reference, but if you've ever come across a Joomla website that uses Community Builder in some way and you've been tasked to display the data in a field from a user profile, here is how:
<?php // Include the Community Builder Plugin Foundation include_once(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php'); // Display some arbitrary user field echo CBuser::getInstance(JFactory::getUser()->id)->getMyUserDataInstance()->get("some_field_here");
The snippet above grabs an instance of the CBuser(Community Builder User) class, using the currently logged in user's id, then grabs a UserDataInstance from the CBUser Object which it finally runs the get method on.
The snippet above uses something called Method Chaining, You don't have to do it like that, but if you just need to drop in a small snippet to display some info one place in your site, it will suffice.
However, if you want to make it more verbose and readable you could also do it like this:
<?php // Include the Community Builder Plugin Foundation include_once(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php'); // Get the user id $userId = JFactory::getUser()->id; // Get an instance of the CBUser class $cbUser = CBuser::getInstance($userId); // Get an instance of the userData class. $userData = $cbUser->getMyUserDataInstance(); // Get the value of a field from the user profile. $occupation = $userData->get("occupation"); // Display this data in a clever way echo "Your occupation is " . $occupation;
If you're so unfortunate as to be a web developer who has to deal with Community Builder and Joomla and need some help, shoot me an email and I'll try to help out to the best of my ability.
The code above has only been tested with Joomla! v3.4.