Obtaining field values or tabs using API

Getting a field or tab is relatively simple, but oddly complicated at times. The code to get a field or tab 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 Community Builder (see Establishing a CB $user object) if you're not and are using a CB Plugin then use the following to establish $cbUser.

// Specific User
$cbUser = CBuser::getInstance( USER_ID_HERE, false );

// My User
$cbUser = CBuser::getMyInstance();

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

/**
 * 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 and edit, 'register' for registration, 'search' for searches
 * @param  int                   $list_compare_types   IF reason == 'search' : 0 : simple 'is' search, 1 : advanced search with modes, 2 : simple 'any' search
 * @param  boolean               $fullAccess    IF true do not take in account current user's viewing rights
 * @param  null|array            $fieldVariables    parses an array of variables into field variables to be sent with the field
 * @return mixed
 */
public function getField( $fieldName, $defaultValue = null, $output = 'html', $formatting = 'none', $reason = 'profile', $list_compare_types = 0, $fullAccess = false, $fieldVariables = array() )
/**
 * Gets the rendered $tab
 *
 * @param  int          $tab           Tab id to render
 * @param  string       $defaultValue  Default HTML to render if the tab does not render
 * @param  string       $output        [optional default='html'] Output format
 * @param  string       $formatting    [optional] Formatting
 * @param  string       $reason        View of tab (default: 'profile')
 * @return string|null                 HTML or null if $position does not exist
 */
public function getTab( $tab, $defaultValue = null, $output = 'html', $formatting = null, $reason = 'profile' )

Most of the getField or getTab function won't be of interest to you, but it will be explained if you plan to use getField or getTab 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. For getTab the $default is what will output if the tab can not be displayed either by access or if the tab has no contents.

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. getTab requires you to always supply tab id and will not work with tab class or title.

Output of a field or tab is highly important as this determines what format the API is going to return the fields value or tab in. HTML by default will return the full html and rendering of a field or tab, but if you change this to CSV you will get the RAW value of a field or tab. 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 path of the image and not return it in an image tag.

Formatting a field or tab is generally left untouched, but with formatting you can add div, span, etc.. around a field or tab to help it display better for your code design. So for example with getTab using a formatting of divs it should display the same div based field rows as if the tab was displaying on a profile.

The reason of a field or tab determines WHERE the field or tab 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.

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

Facebook Twitter LinkedIn