-
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.
Merge pull request #60 from OS2Forms/f/permissions_by_term_custom_field
Adding custom permissions_by_term field
- Loading branch information
Showing
5 changed files
with
153 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' |
105 changes: 105 additions & 0 deletions
105
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,105 @@ | ||
<?php | ||
|
||
namespace Drupal\os2forms_permissions_by_term\Form; | ||
|
||
use Drupal\Core\Entity\EntityFieldManagerInterface; | ||
use Drupal\Core\Form\ConfigFormBase; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Configure os2forms_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'; | ||
|
||
/** | ||
* Entity field manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityFieldManagerInterface | ||
*/ | ||
private $entityFieldTypeManager; | ||
|
||
/** | ||
* Constructs an SettingsForm object. | ||
* | ||
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager | ||
* Entity field manager. | ||
*/ | ||
public function __construct(EntityFieldManagerInterface $entity_field_manager) { | ||
$this->entityFieldTypeManager = $entity_field_manager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('entity_field.manager'), | ||
); | ||
} | ||
|
||
/** | ||
* {@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 => $this->t('None')]; | ||
|
||
$userFields = $this->entityFieldTypeManager->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); | ||
} | ||
|
||
} |