pastvne
Bruce Decker
I can't seem to contribute to the wiki any longer but thought this might be useful for others:
In this example, I needed to create a list plug-in to update certain Joomla User Custom fields. Here's the code I developed to perform this task:
In this example, I needed to create a list plug-in to update certain Joomla User Custom fields. Here's the code I developed to perform this task:
Code:
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
JLoader::import('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models', 'FieldsModel');
$fieldModel = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true));
#userId = 605; // set to your preferred Joomla user id
$fieldName = "company-name"; // using the 'name' property of the cutomer fieldModel
$fieldValue = 'ACME, Inc.'; // assigning a value to the field
$result = csp_setCustomFieldByName($userId, $fieldName, $fieldValue, $fieldModel);
Function csp_setCustomFieldByName($userId, $fieldName, $fieldValue, $fieldModel){
$customFields = FieldsHelper::getFields('com_users.user', $userId, true);
foreach($customFields as $customField){
if ($customField->name == $fieldName){
$fieldModel->setFieldValue($customField->id, $userId, $fieldValue);
return true;
}
}
return false;
}