Skip to content
Snippets Groups Projects
Commit 0c2a72ca authored by Reinhard Hutter's avatar Reinhard Hutter
Browse files

Merge branch '3313803-allow-to-togle' into '2.0.x'

Allow to togle between allow-list and block-list

See merge request !8
parents 99182adf 15f5a665
No related branches found
No related tags found
No related merge requests found
Pipeline #218922 passed with warnings
......@@ -6,6 +6,8 @@ entity_reference_selection.config:
type: mapping
label: 'Filter settings'
mapping:
negate:
type: integer
allowed_ids:
type: sequence
label: 'Allowed ids'
......
<?php
/**
* Add default allowlist setting to existing fields using config entity reference selection
*/
function config_entity_reference_selection_update_8200() {
$configFactory = \Drupal::configFactory();
foreach ($configFactory->listAll('field.field.') as $id) {
$fieldConfig = $configFactory->getEditable($id);
if ($fieldConfig->get('field_type') !== 'entity_reference') {
continue;
}
if (strpos($fieldConfig->get('settings.handler'), 'config:') !== 0) {
continue;
}
if(is_null($fieldConfig->get('settings.handler_settings.filter.negate'))) {
$fieldConfig->set('settings.handler_settings.filter.negate', 0);
$fieldConfig->save(TRUE);
}
}
}
......@@ -60,6 +60,7 @@ class ConfigEntityReferenceSelection extends DefaultSelection {
public function defaultConfiguration() {
return [
'filter' => [
'negate' => 0,
'allowed_ids' => NULL,
],
] + parent::defaultConfiguration();
......@@ -127,7 +128,8 @@ class ConfigEntityReferenceSelection extends DefaultSelection {
if ($target_type = $this->getTargetType()) {
$allowed = $this->getConfiguration()['filter']['allowed_ids'];
if (!empty($allowed)) {
$query->condition($target_type->getKey('id'), $allowed, 'IN');
$operator = $this->getConfiguration()['filter']['negate'] ? 'NOT IN' : 'IN';
$query->condition($target_type->getKey('id'), $allowed, $operator);
}
}
......@@ -143,9 +145,21 @@ class ConfigEntityReferenceSelection extends DefaultSelection {
if ($target_type = $this->getTargetType()) {
if ($options = $this->getOptions()) {
$plural_label = $target_type->getPluralLabel();
$form['filter']['negate'] = [
'#type' => 'radios',
'#options' => [
1 => $this->t('Exclude the selected below'),
0 => $this->t('Include the selected below'),
],
'#title' => $this->t('Which @label should be allowed?', [
'@label' => $plural_label,
]),
'#default_value' => $this->getConfiguration()['filter']['negate'],
];
$form['filter']['allowed_ids'] = [
'#type' => 'select',
'#title' => $this->t('Allowed @label', [
'#type' => 'checkboxes',
'#title' => $this->t('Select @label', [
'@label' => $plural_label,
]),
'#options' => $options,
......@@ -185,7 +199,7 @@ class ConfigEntityReferenceSelection extends DefaultSelection {
// Ensure that the allowed_ids sequence is stored without keys.
$value_path = ['settings', 'handler_settings', 'filter', 'allowed_ids'];
$with_keys = $form_state->getValue($value_path, []);
$with_keys = array_filter($form_state->getValue($value_path, []));
$without_keys = array_values($with_keys);
$form_state->setValue($value_path, $without_keys);
}
......
......@@ -59,6 +59,7 @@ class EntityReferenceSelectionTest extends BrowserTestBase {
'handler_settings' => [
'filter' => [
'allowed_ids' => [],
'negate' => 0,
],
],
],
......@@ -98,6 +99,15 @@ class EntityReferenceSelectionTest extends BrowserTestBase {
$assert->optionExists('Actions reference', 'Block the selected user(s)');
$assert->optionNotExists('Actions reference', 'Cancel the selected user account(s)');
}
// Change the filter type from allowlist to blocklist.
$settings = $this->field->getSettings();
$settings['handler_settings']['filter']['negate'] = 1;
$this->field->setSettings($settings);
$this->field->save();
// The inverted selection should be available.
$this->drupalGet('entity_test/add');
$assert->optionNotExists('Actions reference', 'Block the selected user(s)');
$assert->optionExists('Actions reference', 'Cancel the selected user account(s)');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment