Skip to content

Commit de6f691

Browse files
committed
Change of approach - use custom conditions
1 parent 65bda46 commit de6f691

4 files changed

Lines changed: 29 additions & 118 deletions

File tree

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Restrict Fields
22

3-
> Restrict Fields is a Statamic addon that allows you to restrict fields in the control panel to certain users or groups
3+
> Restrict Fields is a Statamic addon that allows you to restrict fields in the control panel to certain users, groups or roles.
44
55
## How to Install
66

@@ -12,19 +12,19 @@ composer require thoughtco/statamic-restrict-fields
1212

1313
## How to Use
1414

15-
Once installed, add an array of `restrict_to_users` or `restrict_to_groups` to your blueprint YAML.
15+
Once installed, 3 new custom condition methods become available to use:
1616

17-
e.g.
17+
### restrictUsers
1818

19-
```
20-
handle: template
21-
field:
22-
type: template
23-
display: Template
24-
localizable: true
25-
restrict_to_users:
26-
- "a9368c46c-adfc-43c3-b6b5-6a552f60187c"
27-
- "b9368c46c-adfc-43c3-b6b5-6a552f60187d"
28-
```
19+
Use with a value in the format:
20+
`restrictUsers:user_id_1,user_id_2`
21+
22+
### restrictGroups
23+
24+
Use with a value in the format:
25+
`restrictGroups:group_slug_1,group_slug_2`
26+
27+
### restrictRoles
2928

30-
Note: `restrict_to_users` takes preference over `restrict_to_groups` so if both are present, groups will be ignored.
29+
Use with a value in the format:
30+
`restrictRoles:role_slug_1,role_slug_2`

resources/js/RestrictConditions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
Statamic.$conditions.add('restrictUsers', function ({ target, params, store, storeName, values }) {
3+
return params.includes(Statamic.user.id);
4+
});
5+
6+
Statamic.$conditions.add('restrictGroups', function ({ target, params, store, storeName, values }) {
7+
return params.filter(value => Statamic.user.groups.includes(value)).length > 0;
8+
});
9+
10+
Statamic.$conditions.add('restrictRoles', function ({ target, params, store, storeName, values }) {
11+
return params.filter(value => Statamic.user.roles.includes(value)).length > 0;
12+
});

src/Listeners/RestrictFieldsListener.php

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/ServiceProvider.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
22

3-
namespace Thoughtco\RestrictFields;
3+
namespace Thoughtco\StatamicRestrictFields;
44

55
use Statamic\Providers\AddonServiceProvider;
66

77
class ServiceProvider extends AddonServiceProvider
88
{
9-
protected $listen = [
10-
'Statamic\Events\EntryBlueprintFound' => [
11-
Listeners\RestrictFieldsListener::class,
12-
],
9+
protected $scripts = [
10+
__DIR__.'/../resources/js/RestrictConditions.js'
1311
];
1412
}

0 commit comments

Comments
 (0)