superbig/craft-usergroupfield

Field type that let you select one or more user groups

Fund package maintenance!
verbb

Installs: 11 324

Dependents: 0

Suggesters: 0

Security: 0

Stars: 7

Watchers: 3

Forks: 3

Open Issues: 0

Type:craft-plugin

3.0.1 2022-08-03 00:35 UTC

This package is auto-updated.

Last update: 2022-11-23 00:59:25 UTC


README

A field type that lets you select one or more user groups.

Installation

You can install User Group Field via the plugin store, or through Composer.

Craft Plugin Store

To install User Group Field, navigate to the Plugin Store section of your Craft control panel, search for User Group Field, and click the Try button.

Composer

You can also add the package to your project using Composer and the command line.

  1. Open your terminal and go to your Craft project:
cd /path/to/project
  1. Then tell Composer to require the plugin, and Craft to install it:
composer require verbb/user-group-field && php craft plugin/install user-group-field

Usage

This plugin provides a Field Type where you can select one or more user groups. To create the field, go to Settings → Fields in your control panel.

Field Settings

There is 3 display modes:

  • As a dropdown, where you can select only 1 group.
  • As a group of checkboxes, where you can select 1 or more groups.
  • As a group of radio buttons, where you can select only 1 group.

Templating

To list the group(s) selected:

{% set groups = entry.userGroupFieldHandle.getGroups() %}

{% for group in groups %}
    {{ group.name }}
{% endfor %}

To get only the first group:

{% set groups = entry.userGroupFieldHandle.getGroups() | first %}

{{ group.name }}

To check if the current user is in any of the groups selected:

{% if entry.userGroupFieldHandle.inGroup(currentUser) %}
    <h1>User can access</h1>
{% endif %}

To check if the current user can access something based on the group selection:

{% if entry.userGroupFieldHandle.canAccess(currentUser) %}
    <h1>User can access</h1>
{% endif %}

Note: This check is always true for admins.

You can also query a User Group Field based on the uid of a user.

{% set userGroup = craft.app.userGroups.getGroupByHandle('myUserGroup') %}

{% set entries = craft.entries.section('blog').userGroupFieldHandle(userGroup.uid).all() %}

Credits

Originally created by the team at Superbig.

Show your Support

User Group Field is licensed under the MIT license, meaning it will always be free and open source – we love free stuff! If you'd like to show your support to the plugin regardless, Sponsor development.

68747470733a2f2f76657262622e696f2f6173736574732f696d672f76657262622d70696c6c2e737667