3

I've been unable to determine if/how the list of users can be filtered. The example below from User form field type, shows the filter in use, but I've been unable to come up with how to specify the group(s) to include in the list.

<field name="modified_user_id" type="user"
    label="JGLOBAL_FIELD_MODIFIED_BY_LABEL"
    class="readonly"
    readonly="true"
    filter="unset" />

Surely, if you have thousands of users, this list can be reduced to a particular user group somehow - am I wrong in that assumption?

1

3 Answers 3

4

You could simply use

<field name="modified_user_id" 
  type="user"
  label="JGLOBAL_FIELD_MODIFIED_BY_LABEL"
  desc="A_DESCRIPTION_IS_ALWAYS_GOOD"/>

It creates a modal field that display a list of users that you can filter by group by selecting the combo box on the top right of the modal window.

If you need a simple combo box and you don't mind having the user group hard-coded you can use

<field name="modified_user_id_2" 
                type="sql"
                label="JGLOBAL_FIELD_MODIFIED_BY_LABEL"
                desc=""
                query="SELECT u.id , u.name FROM #__users as u inner join #__user_usergroup_map AS ugm ON ugm.user_id = u.id where ugm.group_id=8"
                multiple="single" 
                key_field="id" 
                value_field="name"
                />

And replace 8 by the user group id of your choice.

EDIT: The function of the filter attribut is not to filter the displayed values of the field. It's more of a cleaning function that process the input value after the form is submitted. In this case it has little use as the 'user' field can only output a user id, but in a text field you could use the 'INT' filter to make sure the user enter a number.

The available values for the filter attribute are:

  • INT: An integer,
  • UINT: An unsigned integer,
  • FLOAT: A floating point number,
  • BOOLEAN: A boolean value,
  • WORD: A string containing A-Z or underscores only (not case sensitive),
  • ALNUM: A string containing A-Z or 0-9 only (not case sensitive),
  • CMD: A string containing A-Z, 0-9, underscores, periods or hyphens (not case sensitive),
  • BASE64: A string containing A-Z, 0-9, forward slashes, plus or equals (not case sensitive),
  • STRING: A fully decoded and sanitised string (default),
  • HTML: A sanitised string,
  • ARRAY: An array,
  • PATH: A sanitised file path,
  • USERNAME: Do not use (use an application specific filter),
  • RAW: The raw string is returned with no filtering,
  • unknown: An unknown filter will act like STRING. If the input is an array it will return an array of fully decoded and sanitised strings.

From the Joomla source code

4
  • Well, that would solve the immediate problem, though without the User dialog that works with the type=user, but it doesn't answer the question of how to use the filter for that field type.
    – GDP
    Jun 24, 2014 at 12:44
  • Let me update my answer to explain the use of the filter attribut.
    – Florian
    Jun 24, 2014 at 13:40
  • Well, I suppose then that the answer is that it cannot be done as asked, though the use of a filter seems meaningless since the field type provides a default form for selection - why would you further filter what that selection form returns - i.e. the user_id?
    – GDP
    Jun 24, 2014 at 17:01
  • In this case, the filter attribut is useless, that's right.
    – Florian
    Jun 24, 2014 at 19:47
5

In fact, it is very easy to filtering the list of users in User form field type.

First in your manifest you should create a new type for your field, for example userFiltering (see below)

<field
        name="myUser"
        type="userFiltering"
        label="LABEL"
        description="DESCRIPTION"
/>

In the fieldset of the manifest, add the attribute 'addfieldpath' with the path of your new field type declaration (see below)

<fieldset addfieldpath="/administrator/components/com_mycomponent/models/fields">

Finaly in the path "/administrator/components/com_mycomponent/models/fields", create a file userfiltering.php with the code below

defined('JPATH_BASE') or die();

class JFormFieldUserfiltering extends JFormFieldUser {

public $type = 'userfiltering';

protected function getGroups()
{
    $groups = array();
    $groups[] = 11; // put here the list of the groups you want to filter
    return $groups;
}
} 

That's all

1
  • Could you please check if this is still possible? I tried it but get error loading from file. Nov 16, 2017 at 14:27
1

I just found a new option in Joomla 3.8.2. Field type "user" has the option "groups".

<field
    name="partner_id"
    type="user"
    label="JGLOBAL_FIELD_MODIFIED_BY_LABEL"
    groups="10"
    required="true"/>

Maybe it will be useful for someone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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