Skip to content

Commit

Permalink
Adding custom permissions_by_term field
Browse files Browse the repository at this point in the history
  • Loading branch information
stankut committed Sep 21, 2023
1 parent f9a13ce commit f86dfdf
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa
## [Unreleased]

- [OS-58] New company address fields
- Custom permissions by term field

## [3.10.0] 2023-08-23

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
os2web_permissions_by_term.settings:
title: OS2Forms permissions by term
description: Settings for OS2Forms permissions by term.
route_name: os2web_permissions_by_term.settings_form
parent: system.admin_config_system
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\os2forms_permissions_by_term\Form\SettingsForm;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\webform\WebformInterface;
Expand Down Expand Up @@ -138,3 +139,37 @@ function os2forms_permissions_by_term_options_list_alter(array &$options, array
function os2forms_permissions_by_term_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
\Drupal::service('os2forms_permissions_by_term.maestro_template_helper')->viewsQueryAlter($view, $query);
}

/**
* Implements hook_user_update().
*/
function os2forms_permissions_by_term_user_update($account) {
$config = \Drupal::config(SettingsForm::$configName);

if ($customField = $config->get('os2web_permissions_by_term_custom_field')) {
if ($account->get($customField)) {
$accessTerms = $account->get($customField)->referencedEntities();

/** @var \Drupal\permissions_by_term\Service\AccessStorage $access_storage */
$access_storage = \Drupal::service('permissions_by_term.access_storage');

// First, we delete existing values from the db.
$access_storage->deleteAllTermPermissionsByUserId($account->id());

if (!empty($accessTerms)) {
// For term permissions use user preferred language.
$langcode = $account->getPreferredLangcode();

// Second, we insert updated values.
foreach ($accessTerms as $accessTerm) {
$access_storage->addTermPermissionsByUserIds([$account->id()], $accessTerm->id(), $langcode);
}
}

// Rebuild permissions for nodes if needed.
if (!\Drupal::config('permissions_by_term.settings')->get('disable_node_access_records')) {
node_access_rebuild(TRUE);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
os2web_permissions_by_term.settings_form:
path: '/admin/config/system/os2forms_permissions_by_term'
defaults:
_title: 'OS2Forms Permissions by terms'
_form: 'Drupal\os2forms_permissions_by_term\Form\SettingsForm'
requirements:
_permission: 'administer site configuration'
77 changes: 77 additions & 0 deletions modules/os2forms_permissions_by_term/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Drupal\os2forms_permissions_by_term\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Configure os2web_permissions_by_term settings for this site.
*/
class SettingsForm extends ConfigFormBase {

/**
* Name of the config.
*
* @var string
*/
public static $configName = 'os2web_permissions_by_term.settings';

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'os2web_permissions_by_term_settings';
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [SettingsForm::$configName];
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$options = [0 => t('None')];

$userFields = \Drupal::service('entity_field.manager')->getFieldDefinitions('user', 'user');

/** @var \Drupal\field\Entity\FieldConfig $field */
foreach ($userFields as $field_key => $field) {
// If fieldType is entity_reference, we only support taxonomy terms.
if ($field->getType() == 'entity_reference' && $field->getSetting('target_type') == 'taxonomy_term') {
$options[$field_key] = $field_key;
}
}

$form['os2web_permissions_by_term_custom_field'] = [
'#type' => 'select',
'#options' => $options,
'#title' => $this->t('Permissions by term custom field'),
'#description' => $this->t('The value of this custom field is mapped to Permission by term real field on hook_user_update().'),
'#default_value' => $this->config(SettingsForm::$configName)
->get('os2web_permissions_by_term_custom_field'),
];

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();

$config = $this->config(SettingsForm::$configName);
foreach ($values as $key => $value) {
$config->set($key, $value);
}
$config->save();

parent::submitForm($form, $form_state);
}

}

0 comments on commit f86dfdf

Please sign in to comment.