Commit 1e886dda authored by Gorodkov Denis's avatar Gorodkov Denis

refactor role editor

parent 50f4eed6
name: Role editor
description: Custom task
package: Tasks
type: module
core: 8.x
core_version_requirement: ^8 || ^9
<?php
use Drupal\Core\Access\AccessResult;
/**
* Implements hook_ENTITY_TYPE_access()
*/
function role_editor_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->hasField('field_editor')) {
$allow_redactors = $node->get('field_editor')->referencedEntities();
if ($isRedactor) {
foreach ($allow_redactors as $allow_redactor) {
if ($allow_redactor->id() == $account->id()) {
return AccessResult::neutral();
} else {
return AccessResult::forbidden();
}
}
}
}
}
}
/**
* Implements hook_node_insert()
*/
function role_editor_node_insert(Drupal\Core\Entity\EntityInterface $node) {
$user = Drupal::currentUser();
$user_roles = $user->getRoles();
$isRedactor = false;
foreach ($user_roles as $role) {
if ($role == 'redactor') {
$isRedactor = true;
}
}
if ($isRedactor) {
if ($node->hasField('field_editor')) {
$node->field_editor[] = $user->id();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment