From 42059ff90bdc4415204ab10b2928b5598a89c508 Mon Sep 17 00:00:00 2001 From: David Mignot Date: Fri, 29 Jun 2018 09:50:29 +0200 Subject: [PATCH 1/4] Check if key is defined befory loading the entity, fix array_flip warning --- src/Plugin/views/filter/Selective.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Plugin/views/filter/Selective.php b/src/Plugin/views/filter/Selective.php index 66e3523..b4491d2 100644 --- a/src/Plugin/views/filter/Selective.php +++ b/src/Plugin/views/filter/Selective.php @@ -457,10 +457,12 @@ protected function getOids() { $value = NULL; if (NULL !== $entityTypeStorage) { - $entity = $entityTypeStorage->load($key); + if ($key) { + $entity = $entityTypeStorage->load($key); - if ($entity) { - $value = $entity->label(); + if ($entity) { + $value = $entity->label(); + } } } else { From e09928a4721d70dffcd1cf55641b186d8dfba537 Mon Sep 17 00:00:00 2001 From: David Mignot Date: Wed, 18 Jul 2018 08:57:49 +0200 Subject: [PATCH 2/4] Move key condition above --- src/Plugin/views/filter/Selective.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Plugin/views/filter/Selective.php b/src/Plugin/views/filter/Selective.php index b4491d2..eac9e05 100644 --- a/src/Plugin/views/filter/Selective.php +++ b/src/Plugin/views/filter/Selective.php @@ -456,13 +456,11 @@ protected function getOids() { foreach ((array) $keys as $key) { $value = NULL; - if (NULL !== $entityTypeStorage) { - if ($key) { - $entity = $entityTypeStorage->load($key); + if (NULL !== $entityTypeStorage && $key) { + $entity = $entityTypeStorage->load($key); - if ($entity) { - $value = $entity->label(); - } + if ($entity) { + $value = $entity->label(); } } else { From d0b865f9a5e7374aec5d5029a3234909a753011f Mon Sep 17 00:00:00 2001 From: David Mignot Date: Wed, 20 Oct 2021 16:49:25 +0200 Subject: [PATCH 3/4] Compatibility with drupal 9 --- src/Plugin/views/filter/Selective.php | 26 ++++++++++++++------------ views_selective_filters.info.yml | 3 ++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/Plugin/views/filter/Selective.php b/src/Plugin/views/filter/Selective.php index 66e3523..b23e3e4 100644 --- a/src/Plugin/views/filter/Selective.php +++ b/src/Plugin/views/filter/Selective.php @@ -7,7 +7,6 @@ namespace Drupal\views_selective_filters\Plugin\views\filter; -use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormStateInterface; @@ -15,6 +14,7 @@ use Drupal\views\Plugin\views\filter\InOperator; use Drupal\views\ViewExecutable; use Drupal\views\Views; +use Drupal\Component\Utility\Html; /** * Views filter handler for selective values. @@ -172,9 +172,9 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#theme' => 'status_messages', '#message_list' => ['warning' => [$this->t('This filter is always exposed to users.')]], '#status_headings' => [ - 'status' => t('Status message'), - 'error' => t('Error message'), - 'warning' => t('Warning message'), + 'status' => $this->t('Status message'), + 'error' => $this->t('Error message'), + 'warning' => $this->t('Warning message'), ], ))); // Remove option to unexpose filter. Tried to disable, but did not work. @@ -374,10 +374,11 @@ protected function getOids() { // Check to see if the user remembered to add the field. if (empty($fields)) { - drupal_set_message(t('Selective query filter must have corresponding field added to view with Administrative Name set to "@name" and Base Type "@type"', - array( + \Drupal::messenger()->addMessage('Selective query filter must have corresponding field added to view with Administrative Name set to "@name" and Base Type "@type"', + [ '@name' => $ui_name, - '@type' => $base_field)), + '@type' => $base_field + ], 'error'); return []; } @@ -399,10 +400,11 @@ protected function getOids() { ($field_options['relationship'] === $this->options['relationship']); if (!$equal) { - drupal_set_message(t('Selective filter "@name": relationship of field and filter must match.', - array( + \Drupal::messenger()->addMessage('Selective filter "@name": relationship of field and filter must match.', + [ '@name' => $ui_name, - '@type' => $base_field)), + '@type' => $base_field, + ], 'error'); return []; } @@ -469,7 +471,7 @@ protected function getOids() { } if (NULL !== $value) { - $oids[$key] = SafeMarkup::checkPlain($value); + $oids[$key] = Html::escape($value); } } } @@ -500,7 +502,7 @@ protected function getOids() { // If limit exceeded this field is not good for being "selective". if (!empty($max_items) && count($oids) === $max_items) { - drupal_set_message(t('Selective filter "@field" has limited the amount of total results. Please, review you query configuration.', array('@field' => $ui_name)), 'warning'); + \Drupal::messenger()->addMessage('Selective filter "@field" has limited the amount of total results. Please, review you query configuration.', ['@field' => $ui_name], 'warning'); } static::$results[$signature] = $oids; diff --git a/views_selective_filters.info.yml b/views_selective_filters.info.yml index db5a88b..cc4ee27 100644 --- a/views_selective_filters.info.yml +++ b/views_selective_filters.info.yml @@ -3,5 +3,6 @@ type: module description: Restrict exposed filter values to those present in the result set. package: Views core: 8.x +core_version_requirement: ^8 || ^9 dependencies: - - views + - drupal:views From b6dd618cf7b895502a4c2b4c774498d0338435f0 Mon Sep 17 00:00:00 2001 From: David Mignot <197418+idflood@users.noreply.github.com> Date: Thu, 10 Aug 2023 16:40:40 +0200 Subject: [PATCH 4/4] Drupal 10 compatibility --- views_selective_filters.info.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views_selective_filters.info.yml b/views_selective_filters.info.yml index cc4ee27..96898c5 100644 --- a/views_selective_filters.info.yml +++ b/views_selective_filters.info.yml @@ -3,6 +3,6 @@ type: module description: Restrict exposed filter values to those present in the result set. package: Views core: 8.x -core_version_requirement: ^8 || ^9 +core_version_requirement: ^8 || ^9 || ^10 dependencies: - drupal:views