Accessing Custom Field Information

10 years 11 months ago - 10 years 10 months ago #227518 by 69Mikie
Accessing Custom Field Information was created by 69Mikie
I don't know why it's so hard to find this information but I've been trying to figure out how to access the data for a custom field for a couple of days now because I'm new to Joomla! and to Community Builder and now that I've got it working I figured I'd maybe save someone else the time it took me to figure it out. I'm no expert but I know enough about how things work to usually be able to get things done.

I added this code to an article in Joomla! I needed a button to link to another page based on the value of a custom field, "cb_field1". Based on the value of cb_field1 I want the button to link to one of 3 pages on another site which I call "www.mysite.com" in this code.

It's also not completely clear how to add code to articles because the editor tries to turn it into text that will display on the page rather than execute so you can't just throw it in there. I used a plug-in or module or whatever it is, called Sourcerer (extensions.joomla.org/extensions/core-enhancements/coding-a-scripts-integration/custom-code-in-content/5051#action> which worked out pretty well for adding executable code to the article.

My implementation may be way out of whack but it works, which is nothing to sneeze at. Replace every instance of "cb_field1" down at the end there with the name of the field you created (the Name, not the Title) or whatever field you're interested in, change what you want it to match (put in your values to replace "Single", "Couple",and "Business Lunch") and replace the "echo" statements with whatever php code you want to execute for each case.

I put the pages I found the information in the comments if you care and want to read more.

If there's other information out there that shows how to do this I couldn't find it. I hope this helps someone.
<?php
global $_CB_framework, $mainframe;
 
/**********************************************************
 Make sure Community Builder is installed and include the
 CB Foundation
 http://www.joomlapolis.com/support/tutorials/120-api-usage/18357-including-cb-api-for-usage-outside-of-cb
**********************************************************/
if (defined('JPATH_ADMINISTRATOR'))
	{
		if (!file_exists(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php'))
		{
			echo 'CB not installed!';
			return;
		}
	include_once(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php');
	}
else
	{
	if (!file_exists($mainframe->getCfg('absolute_path') . '/administrator/components/com_comprofiler/plugin.foundation.php'))
		{
		echo 'CB not installed!';
		return;
		}
	include_once($mainframe->getCfg('absolute_path') . '/administrator/components/com_comprofiler/plugin.foundation.php');
	}
/**********************************************************
 Include some helpful functions
 http://www.joomlapolis.com/support/tutorials/120-api-usage/18357-including-cb-api-for-usage-outside-of-cb
**********************************************************/
cbimport( 'cb.database' );		//Database functions
cbimport( 'cb.snoopy' );		//Snoopy functions
cbimport( 'cb.imgtoolbox' );	//Image toobox functions
cbimport( 'cb.html' );			//Class and plug-in functions
cbimport( 'cb.plugins' );		//Plug-in functions

/**********************************************************
 Establish the CB $user object
 http://www.joomlapolis.com/support/tutorials/120-api-usage/18359-establishing-a-cb-user-object
**********************************************************/
Global $_CB_framework;
 
$myId = $_CB_framework->myId();
$cbUser =& CBuser::getInstance( $myId );
 
if ( ! $cbUser ) {
 $cbUser =& CBuser::getInstance( null );
}
 
$user =& $cbUser->getUserData();

/**********************************************************
 Make the button based on the value of field1
**********************************************************/
if ($user->cb_field1 == "Single")
	{
		echo "<form action=\"http://www.mysite.com/single.htm\">";
		echo "<input type=\"submit\" value=\"Buy Passes\">";
		echo "</form>";
	}
elseif ($user->cb_field1 == "Couple")
	{
		echo "<form action=\"http://www.mysite.com/couple.htm\">";
		echo "<input type=\"submit\" value=\"Buy Passes\">";
		echo "</form>";
	}
elseif ($user->cb_field1 == "Business Lunch")
	{
		echo "<form action=\"http://www.mysite.com/businesslunch.htm\">";
		echo "<input type=\"submit\" value=\"Buy Passes\">";
		echo "</form>";
	}
?>

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.151 seconds

Facebook Twitter LinkedIn