1

I'm triing to use custom fields in users for personal details on members of a group, but I've got some "system" users that don't have these details. But everytime I want to edit these users I'm nagged with custom fields, have to disable, edit and enable again...

Is there a way to assign custom fields just to some usergroups? or to make them not required for some group/user...

Thanks!

2

(This answer has been updated from the original which suggested hacking Joomla code, to this much better solution proposed in the comment below - thanks to @Irata!)

Joomla includes the custom fields by means of a plugin which is triggered on onContentPrepareForm. This solution involves writing a second plugin, also triggered on onContentPrepareForm, which removes the custom fields if the usergroup of the user being edited is within a predefined subset.

Here's the code of the plugin – put in remove_custom_fields.php, but set the usergroups of your system users appropriately (ensuring the case and spacing for the names of the usergroups is correct). (You could also change the code to set the usergroup names via plugin config.)

class plgUserRemove_Custom_Fields extends JPlugin
{

public function onContentPrepareForm(JForm $form, $data)
{
    if (JFactory::getApplication()->isClient('administrator') && $form->getName() == "com_users.user" && $data->id > 0) // admin user edit form, and not creating a new user
    {
        $systemUsergroups = array("Super Users", "Manager");   // change to suit your system usergroups
        $user = JFactory::getUser($data->id);   // get details of user being edited 
        $usergrouphelper = JHelperUsergroups::getInstance();   
        $usergroups = $usergrouphelper->getAll();   // get all usergroups on the system via the helper class
        foreach ($usergroups as $usergroup)
        {   // for each usergroup, check if the usergroup is in your systems usergroup, and if this user belongs to that usergroup
            if (in_array($usergroup->id, $user->groups) && in_array($usergroup->title, $systemUsergroups))
            {
                $form->removeGroup('com_fields');
            }
        }
    }
}
}

By way of explanation … The plugin checks that it's the administrator Edit User form that's being shown and that an existing user is being edited. It then checks if the user being edited has a usergroup which belongs to the $systemUsergroups array, and if so then the function removes the custom fields from the form.

Also, here's the plugin xml file I used - in a directory plg_user_remove_custom_fields.

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.8" type="plugin" group="user" method="upgrade">
    <name>User - remove custom fields</name>
    <description>Plugin to remove custom fields of admin user edit</description>
    <files>
        <filename plugin="remove_custom_fields">remove_custom_fields.php</filename>
    </files>
</extension>

Finally, make sure that this plugin is triggered after the System - Fields plugin, via the ordering on the admin plugins page.

  • 2
    Rather than updating a core plugin, I think it may be possible to create a second plugin that runs after the System Fields plugin that uses much the same code to identify that the user being edited is in the Admin group(s) and then remove or modify the custom fields, make them read only or maybe be make them not required. I am not sure if custom fields can be manipulated like ordinary fields can using the built in methods, but the rest is just the code @Robbie Jackson has supplied. – Irata Jan 8 at 3:10
  • Yes @Irata, I think that would be a better idea. You could write some javascipt which would hide the custom fields tab on the form (and maybe also have to set the custom fields to not required), and then use the above code in a plugin to determine whether that javascript should be added to the page html document or not. What do you think? – Robbie Jackson Jan 8 at 10:00
  • If it works with custom fields I was thinking something like api.joomla.org/cms-3/classes/… to get rid of the field all together. With just hiding a required custom field there might be an issue when it comes to saving it. – Irata Jan 8 at 10:48
  • Thanks @Irata, that option was easier than I thought it was going to be! – Robbie Jackson Jan 8 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.