Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
Project
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
Gorodkov Denis
Project
Commits
43883931
Commit
43883931
authored
May 27, 2022
by
Gorodkov Denis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
access for role 'Editor'
parent
9e34105d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
0 deletions
+91
-0
mymodule.module
app/docroot/modules/custom/mymodule/mymodule.module
+91
-0
No files found.
app/docroot/modules/custom/mymodule/mymodule.module
View file @
43883931
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
* Main file for hooks and custom functions.
* Main file for hooks and custom functions.
*/
*/
use
Drupal\Core\Access\AccessResult
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
;
use
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Entity\EntityInterface
;
...
@@ -107,3 +108,93 @@ function template_preprocess_mymodule_example_first(&$variables) {
...
@@ -107,3 +108,93 @@ function template_preprocess_mymodule_example_first(&$variables) {
$date_formatter
=
\Drupal
::
service
(
'date.formatter'
);
$date_formatter
=
\Drupal
::
service
(
'date.formatter'
);
$variables
[
'date'
]
=
$date_formatter
->
format
(
time
());
$variables
[
'date'
]
=
$date_formatter
->
format
(
time
());
}
}
/**
* Implements hook_ENTITY_TYPE_access()
*/
function
mymodule_node_access
(
\Drupal\Core\Entity\EntityInterface
$node
,
$operation
,
\Drupal\Core\Session\AccountInterface
$account
)
{
$user_roles
=
$account
->
getRoles
();
$isRedactor
=
false
;
foreach
(
$user_roles
as
$role
)
{
if
(
$role
==
'redactor'
)
{
$isRedactor
=
true
;
}
}
if
(
$operation
==
'delete'
)
{
if
(
$isRedactor
)
{
return
AccessResult
::
forbidden
();
}
}
if
(
$operation
==
'update'
)
{
if
(
$node
->
getOwnerId
()
==
$account
->
id
())
{
/**
* Если это создатель
*/
return
AccessResult
::
allowed
();
}
if
(
$node
->
hasField
(
'field_editor'
))
{
$allow_redactors
=
$node
->
get
(
'field_editor'
)
->
referencedEntities
();
}
else
{
/**
* Если поля field_editor нету
*/
return
AccessResult
::
allowed
();
}
if
(
empty
(
$allow_redactors
))
{
/**
* Если поля field_editor не заполнено
*/
return
AccessResult
::
allowed
();
}
else
{
if
(
$isRedactor
)
{
foreach
(
$allow_redactors
as
$allow_redactor
)
{
if
(
$allow_redactor
->
id
()
==
$account
->
id
())
{
/**
* Если роли Redactor разрешено редактирование
*/
return
AccessResult
::
allowed
();
}
}
/**
* Если роли Redactor НЕ разрешено редактирование
*/
return
AccessResult
::
forbidden
();
}
else
{
/**
* Если у пользователя нет роли Redactor
*/
return
AccessResult
::
allowed
();
}
}
}
else
{
/**
* Если операция не update
*/
}
}
function
mymodule_entity_form_display_alter
(
\Drupal\Core\Entity\Display\EntityFormDisplayInterface
$form_display
,
array
$context
)
{
if
(
$context
[
'entity_type'
]
==
'node'
)
{
$user
=
Drupal
::
currentUser
();
$user_roles
=
$user
->
getRoles
();
$isRedactor
=
false
;
foreach
(
$user_roles
as
$role
)
{
if
(
$role
==
'redactor'
)
{
$isRedactor
=
true
;
}
}
if
(
$isRedactor
)
{
$form_display
->
setComponent
(
'field_editor'
,
array
(
'type'
=>
'hidden'
,
));
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment