Sample code to retrieve field data (also creating parameters and checking access)

  • Published by
    George Papadakis
  • Last modified
    01 December 2013
  • Up to date
    Yes
  • Since Version
    2.0
  • Voting
    Average rating
    2 votes
    • 1
    • 2
    • 3
    • 4
    • 5
  • Favourites
    500 Sample code to retrieve field data (also creating parameters and checking access) /documentation/tutorials-english/70-developer-api-field-plugins/500-sample-code-to-retrieve-field-data-also-creating-parameters-and-checking-access.html
// db and current fronten user objects
$db   = JFactory::getDBO();
$user = JFactory::getUser();

// Get user's access levels
if (!FLEXI_J16GE) $aid = (int) $user->get('aid');
else $aid_arr = $user->getAuthorisedViewLevels();

// Get field data from DB
$my_fieldnames = array("field_name1", "field_name2", "field_name3");
$query = 'SELECT * FROM #__flexicontent_fields WHERE name IN ("'
	.implode($my_fieldnames, '", "')
	.'")';
$db->setQuery($query);
$my_fields = $db->loadObjectList('name');

// For every found field create field parameter and check (VIEW) access level
if ( !empty($my_fields) ) foreach($my_fields as $my_field)
	$my_field->parameters = FLEXI_J16GE ? new JRegistry($my_field->attribs) : new JParameter($my_field->attribs);
	$my_field->has_access = FLEXI_J16GE ? in_array($my_field->access, $aid_arr) : $my_field->access <= $aid;
}