-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding custom permissions_by_term field
- Loading branch information
Showing
5 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
modules/os2forms_permissions_by_term/os2forms_permissions_by_term.links.menu.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
modules/os2forms_permissions_by_term/os2forms_permissions_by_term.routing.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
modules/os2forms_permissions_by_term/src/Form/SettingsForm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |