Login

API - Getting Fields PDF Print E-mail
Tutorials - Community Builder
Tuesday, 28 July 2009 09:03
Getting a field is relatively simple, but oddly complicated at times. The code to get a field will need to be tweaked to fit your site, so be sure you're ready to do a lot of refreshes until you get the proper format. To help we'll explain what each format and display mode does.

First you must establish $user if using external code from CB (see API - Establishing $user) if you're not and are using a CB Plugin then use the following to establish $cbUser.

1
2
3
4
5
$cbUser			=&	CBuser::getInstance( $user->id );
 
if ( ! $cbUser ) {
	$cbUser		=&	CBuser::getInstance( null );
}

Ok now that $cbUser has been established it's time to examine the getField function for $cbUser class.

1
function getField( $fieldName, $defaultValue = null, $output = 'html', $formatting = 'none', $reason = 'profile', $list_compare_types = 0 ) {

Most of the getField function won't be of interest to you, but it will be explained if you plan to use getField in a more advanced way. As you can see it's possible to change the output, format, and reason. All 3 of which are highly important to its usage. $list_compare_types will likely never be of significant use for you as it simply signifies its search mode. Please review the following functions descriptive text for help with its parameters.

1
2
3
4
5
6
7
8
9
10
11
12
/**
* Formatter:
* Returns a field in specified format
*
* @param  string    $fieldName               Name of field to render
* @param  mixed     $defaultValue            Value if field is not in reach of viewer user or innexistant
* @param  string    $output                  'html', 'xml', 'json', 'php', 'csvheader', 'csv', 'rss', 'fieldslist', 'htmledit'
* @param  string    $formatting              'tr', 'td', 'div', 'span', 'none',   'table'??
* @param  string    $reason                  'profile' for user profile view, 'edit' for user profile edit, 'register' for registration, 'search' for searches, 'list' for lists
* @param  int       $list_compare_types      IF reason == 'search' : 0 : simple 'is' search, 1 : advanced search with modes, 2 : simple 'any' search
* @return mixed                
*/

Contrary to what name of field implies you may also use a fields ID instead of its name. This is purely personal preference, but 1 must be provided for a valid existing field or nothing will be returned.

Output of a field is highly important as this determines what format the API is going to return the fields value in. HTML by default will return the full html and rendering of a field, but if you change this to CSV you will get the RAW value of a field. If you get the Avatar field and have it set as HTML it will display the actual image, but if you set it s CSV it will only get the name of the image and not return it in an image tag.

Formatting a field is generally left untouched, but with formatting you can add div, span, etc.. around a field to help it display better for your code design.

The reason of a field determines WHERE the field is located. A good example is the Avatar. As you can see on your profile it's full sized and has no link, but looks completely different on the userlist. You can change the output to fit what location you want to mimic. If you leave it at default of profile then it will display as if it was being shown on profile, but a simple change to list will display it as if it was being shown on a userlist.

Example
1
$avatar	=	$cbUser->getField( 'avatar', null, 'html', 'none', 'list' );
 

You are not authorized to comment on this content.