View source
<?php
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\field\FieldConfigInterface;
use Drupal\user\RoleInterface;
use Drupal\user\Entity\User;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
define('PBF_BY_ROLE_ID', 'role_id');
define('PBF_BY_USER_ID', 'user_id');
function pbf_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.pbf':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Manage access entity by Entity reference field') . '</p>';
$output .= '<pre>' . file_get_contents(__DIR__ . '/README.txt') . '</pre>';
return $output;
default:
}
}
function pbf_node_grants(AccountInterface $account, $op) {
$grants['pbf_author'] = [
$account
->id(),
];
$grants['pbf_' . PBF_BY_USER_ID] = [
$account
->id(),
];
$roles = $account
->getRoles();
$role_ids = \Drupal::config('pbf.settings')
->get('pbf_roles_gids');
foreach ($roles as $role) {
$grants['pbf_' . PBF_BY_ROLE_ID][] = $role_ids[$role];
}
$user = User::load($account
->id());
$field_definitions = $user
->getFieldDefinitions();
foreach ($field_definitions as $field_name => $field_definition) {
if ($field_definition instanceof FieldConfigInterface && $field_definition
->getType() == 'pbf') {
$values = $user->{$field_name}
->getValue();
if (empty($values)) {
continue;
}
$target_entity_type_id = $field_definition
->getSetting('target_type');
switch ($target_entity_type_id) {
case 'node':
case 'taxonomy_term':
foreach ($values as $value) {
if ($value) {
$grants['pbf_' . $field_name][] = $value['target_id'];
}
}
break;
case 'user':
foreach ($values as $value) {
if ($value) {
$grants['pbf_' . $field_name][] = $value['target_id'];
}
}
break;
}
}
}
return $grants;
}
function pbf_node_access_records(NodeInterface $node) {
if (!$node
->isPublished()) {
return NULL;
}
$field_definitions = $node
->getFieldDefinitions();
$grants = [];
foreach ($field_definitions as $field_name => $field_definition) {
if ($field_definition instanceof FieldConfigInterface && $field_definition
->getType() == 'pbf') {
$values = $node->{$field_name}
->getValue();
if (empty($values)) {
continue;
}
$target_entity_type_id = $field_definition
->getSetting('target_type');
$priority = $field_definition
->getSetting('priority');
$user_method = $field_definition
->getSetting('user_method');
switch ($target_entity_type_id) {
case 'node':
case 'taxonomy_term':
foreach ($values as $value) {
if ($value['grant_public']) {
continue;
}
$grants[] = pbf_create_grant($value, $field_name, $priority);
}
break;
case 'user':
foreach ($values as $value) {
if ($value['grant_public']) {
continue;
}
if ($user_method == 'user') {
$grants[] = pbf_create_grant($value, PBF_BY_USER_ID, $priority);
}
if ($user_method == 'ref_user') {
$grants[] = pbf_create_grant($value, $field_name, $priority);
$grants[] = pbf_create_grant($value, PBF_BY_USER_ID, $priority);
}
}
break;
case 'user_role':
foreach ($values as $value) {
if ($value['grant_public']) {
continue;
}
$role_ids = \Drupal::config('pbf.settings')
->get('pbf_roles_gids');
if (isset($role_ids[$value['target_id']]) && is_int($role_ids[$value['target_id']])) {
$value['target_id'] = $role_ids[$value['target_id']];
$grants[] = pbf_create_grant($value, PBF_BY_ROLE_ID, $priority);
}
}
break;
}
}
}
if ($grants && $node
->getOwnerId()) {
$grants[] = array(
'realm' => 'pbf_author',
'gid' => $node
->getOwnerId(),
'grant_view' => 1,
'grant_update' => $node
->getOwner()
->hasPermission('edit own ' . $node
->bundle() . ' content') ? 1 : 0,
'grant_delete' => $node
->getOwner()
->hasPermission('delete own ' . $node
->bundle() . ' content') ? 1 : 0,
'priority' => 0,
);
}
return $grants;
}
function pbf_create_grant($value, $field_name, $priority = 0) {
$grant = array(
'realm' => 'pbf_' . $field_name,
'gid' => $value['target_id'],
'grant_view' => $value['grant_view'],
'grant_update' => $value['grant_update'],
'grant_delete' => $value['grant_delete'],
'priority' => $priority,
);
return $grant;
}
function pbf_entity_delete(EntityInterface $entity) {
$config = \Drupal::configFactory()
->getEditable('pbf.settings');
if ($entity instanceof RoleInterface) {
$roles_gids = $config
->get('pbf_roles_gids');
unset($roles_gids[$entity
->id()]);
$config
->set('pbf_roles_gids', $roles_gids);
$config
->save();
}
if ($entity instanceof FieldableEntityInterface) {
\Drupal::service('pbf.synchronize')
->synchronize('delete', $entity);
}
}
function pbf_entity_insert(EntityInterface $entity) {
$config = \Drupal::configFactory()
->getEditable('pbf.settings');
if ($entity instanceof RoleInterface) {
$roles_gids = array_flip($config
->get('pbf_roles_gids'));
$roles_gids[] = $entity
->id();
$config
->set('pbf_roles_gids', array_flip($roles_gids));
$config
->save();
}
if ($entity instanceof FieldableEntityInterface) {
\Drupal::service('pbf.synchronize')
->synchronize('insert', $entity);
}
}
function pbf_entity_update(EntityInterface $entity) {
if ($entity instanceof FieldableEntityInterface) {
\Drupal::service('pbf.synchronize')
->synchronize('update', $entity);
}
}
function pbf_field_config_insert(FieldConfigInterface $entity) {
if ($entity
->getType() == 'pbf' && $entity
->getSetting('synchronized_with')) {
$entityTypeManager = \Drupal::entityTypeManager()
->getStorage('field_config');
$field_to_synchronize = $entityTypeManager
->load($entity
->getSetting('synchronized_with'));
if ($field_to_synchronize) {
$field_to_synchronize
->set('synchronized_by', $entity
->id())
->set('synchronized_from_target', $entity
->getSetting('synchronized_from_target'))
->save();
}
}
}
function pbf_field_config_update(FieldConfigInterface $entity) {
if ($entity
->getType() == 'pbf') {
$original = $entity->original;
$entityTypeManager = \Drupal::entityTypeManager()
->getStorage('field_config');
if ($entity
->getSetting('synchronized_with') == $original
->getSetting('synchronized_with') && $entity
->getSetting('synchronized_with')) {
if ($entity
->getSetting('synchronized_from_target') !== $original
->getSetting('synchronized_from_target')) {
$field_to_synchronize = $entityTypeManager
->load($entity
->getSetting('synchronized_with'));
if ($field_to_synchronize) {
$field_to_synchronize
->setSetting('synchronized_from_target', $entity
->getSetting('synchronized_from_target'))
->save();
}
}
else {
return;
}
}
if ($entity
->getSetting('synchronized_with') && empty($original
->getSetting('synchronized_with'))) {
$field_to_synchronize = $entityTypeManager
->load($entity
->getSetting('synchronized_with'));
if ($field_to_synchronize) {
$field_to_synchronize
->setSetting('synchronized_by', $entity
->id())
->setSetting('synchronized_from_target', $entity
->getSetting('synchronized_from_target'))
->save();
}
}
if (empty($entity
->getSetting('synchronized_with')) && $original
->getSetting('synchronized_with')) {
$field_to_synchronize = $entityTypeManager
->load($original
->getSetting('synchronized_with'));
if ($field_to_synchronize) {
$field_to_synchronize
->setSetting('synchronized_by', '')
->setSetting('synchronized_from_target', 0)
->save();
}
}
if ($entity
->getSetting('synchronized_with') && $original
->getSetting('synchronized_with') && $entity
->getSetting('synchronized_with') !== $original
->getSetting('synchronized_with')) {
$new_field_to_synchronize = $entityTypeManager
->load($entity
->getSetting('synchronized_with'));
if ($new_field_to_synchronize) {
$new_field_to_synchronize
->setSetting('synchronized_by', $entity
->id())
->setSetting('synchronized_from_target', $entity
->getSetting('synchronized_from_target'))
->save();
}
$old_field_to_synchronize = $entityTypeManager
->load($original
->getSetting('synchronized_with'));
if ($old_field_to_synchronize) {
$old_field_to_synchronize
->setSetting('synchronized_by', '')
->setSetting('synchronized_from_target', 0)
->save();
}
}
}
}
function pbf_field_config_delete(FieldConfigInterface $entity) {
if ($entity
->getType() == 'pbf' && $entity
->getSetting('synchronized_with')) {
$entityTypeManager = \Drupal::entityTypeManager()
->getStorage('field_config');
$field_to_synchronize = $entityTypeManager
->load($entity
->getSetting('synchronized_with'));
if ($field_to_synchronize) {
$field_to_synchronize
->set('synchronized_by', '')
->save();
$field_to_synchronize
->set('synchronized_from_target', 0)
->save();
}
}
}
function pbf_field_ui_preconfigured_options_alter(array &$options, $field_type) {
if ($field_type === 'pbf') {
$options = [];
}
}