Problem/Motivation

Every exposed filter has a checkbox that allows it to be remembered through the session. Not only that, there's also an option to limit that functionality to certain user roles.

Even when that checkbox is off, the view still stores a list of all non-selected roles:

          expose:
            operator_id: ''
            label: Published
            description: ''
            use_operator: false
            operator: status_op
            identifier: status
            required: false
            remember: false
            multiple: false
            remember_roles:
              authenticated: authenticated
              anonymous: '0'
              ***_editor: '0'
              ***_clientadmin: '0'
              administrator: '0'

I don't think there are config dependencies for this but it does make more complicated to re-use views/put them as default config in a module because you tend to have left-over things in there from custom roles. "remember_roles:" exists almost 200x in this projects config sync folder which means 800 lines of useless yaml to parse :) And projects often have a lot more roles.

Proposed resolution

Filter out non-selected roles. Maybe even not store anything if the remember setting is off?

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

Issue fork drupal-2997393

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Berdir created an issue. See original summary.

andypost’s picture

Good idea to store much less! surely it needs update hook to clean-up a lot of core views at least

longwave’s picture

+1, I have been annoyed by this since D7 and Features exports.

andypost’s picture

Status: Active » Needs review
StatusFileSize
new874 bytes

That works for me

jibran’s picture

Issue tags: +Need tests

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

phthlaap’s picture

Hi,

I have tested this issue manually. It worked for me.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alexpott’s picture

This would be a lovely thing to land. We need a test and an update function in order to land this!

renatog’s picture

Status: Needs review » Needs work

This line has more than 80 chats. Please could you break this array in a second line? E.g.:

From:
$form_state->setValue(['options', 'expose', 'remember_roles'], array_filter($roles));

To:

$form_state->setValue([
  'options',
  'expose',
  'remember_roles'
], array_filter($roles));
nikitagupta’s picture

Status: Needs work » Needs review
StatusFileSize
new865 bytes

Rerolled the patch #4.

renatog’s picture

Status: Needs review » Reviewed & tested by the community

That's great. Thank you so much

longwave’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Need tests +Needs tests

Still needs a test and update hook as per #12.

vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new2.7 KB
new5.08 KB
new4.23 KB

Adding a test and update hook for the issue , the update hook finds all view yml files and set the updated configurations. couldn't find anything more effective to update the files , even Yml::dump() dumper has some indentation issues so , I didn't try that.

Hope this works.

Kindly review.

The last submitted patch, 17: 2997393-17-fail.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 17: views_non_selected_roles-2997393-17.patch, failed testing. View results

andypost’s picture

Issue tags: -Needs tests +Needs upgrade path tests

@vakulrai great job! needs a bit of work

+++ b/core/modules/views/views.install
@@ -18,3 +20,29 @@ function views_install() {
+function mymodule_update_9001() {

it should be post update hook and requires test

longwave’s picture

There is a helper class in Views called ViewsConfigUpdater than can be used to modify Views config entities in update hooks, instead of parsing/rewriting the YAML directly.

vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new6.29 KB
new5.91 KB

@andypost, @longwave I worked on the inputs given by you and have made the changes accordingly to the patch. The patch has following updates,

- .install code has been moved to hook_post_update_name() with some modifications.
- in post_update I have used the ViewsConfigUpdater class to modify the view
- added a test for post_update.

Kindly review.

vakulrai’s picture

StatusFileSize
new7.5 KB
new7.89 KB

I missed the tests for hook_post_update in previous patch, re uploading the patch with interdiff. Consider this for review.

Thanks.

kapilv’s picture

StatusFileSize
new7 KB
new599 bytes

Fixed Custom Commands Failed.

gauravvvv’s picture

StatusFileSize
new152.83 KB
new157.53 KB

Re-rolled the patch, Attached interdiff for same.

andypost’s picture

StatusFileSize
new3.8 KB
new6.48 KB

It could use to extend \Drupal\views\ViewsConfigUpdater::processOperatorDefaultsHandler() via \Drupal\views\ViewsConfigUpdater::updateAll() but it looks more straight to not extend it

longwave’s picture

Title: Filter out non-selected roles for the remember_roles settin in views » Filter out non-selected roles for the remember_roles setting in views
Status: Needs review » Needs work
Issue tags: -Needs upgrade path tests

Couple of little nits but this looks almost ready to go.

  1. +++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
    @@ -665,6 +665,12 @@ public static function trustedCallbacks() {
    +    $roles = $form_state->getValue(['options', 'expose', 'remember_roles']);
    +    $form_state->setValue([
    +      'options',
    +      'expose',
    +      'remember_roles',
    +    ], array_filter($roles));
    

    I think the setValue call should all be on one line if the getValue call is.

  2. +++ b/core/modules/views/tests/src/Functional/Update/UserRememberRolesFilterSettingTest.php
    @@ -0,0 +1,45 @@
    + * Tests the role expose filter re4member role settings.
    

    re4member -> remember

  3. +++ b/core/modules/views/views.post_update.php
    @@ -68,3 +68,29 @@ function views_post_update_rename_default_display_setting() {
    +            foreach (array_keys($filter_value['expose']['remember_roles'], '0', TRUE) as $role_key) {
    

    Nice! Today I learned you can search with array_keys() alone.

ankithashetty’s picture

Status: Needs work » Needs review
StatusFileSize
new6.44 KB
new2.04 KB

Updated patch in #26 addressing #27, also made a following small change(attached an interdiff):

+++ b/core/modules/views/tests/src/Functional/Wizard/BasicTest.php
@@ -230,8 +230,7 @@
    * Tests user role exposed filter options.
    */
   public function testUserRolesFilter() {
-    // Create a view for user entity and add a role filter.
-    // settings.
+    // Create a view for user entity and add a role filter settings.

Thanks!

meenakshi_j’s picture

StatusFileSize
new6.44 KB

Fixed the #28 custom commands.

renatog’s picture

StatusFileSize
new815 bytes

Interdiff between #28 and #29

renatog’s picture

Status: Needs review » Needs work

On this part

+ foreach ($display_settings as &$display) {
+      if (!empty($display['display_options']['filters'])) {
+        foreach ($display['display_options']['filters'] as &$filter_value) {
+          if (!empty($filter_value['expose']['remember_roles'])) {
+            foreach (array_keys($filter_value['expose']['remember_roles'], '0', TRUE) as $role_key) {
+              unset($filter_value['expose']['remember_roles'][$role_key]);
+              $save = TRUE;
+            }
+          }
+        }
+      }
+    }

We should apply some early return to avoid many level of if and foreach

renatog’s picture

Status: Needs work » Needs review
StatusFileSize
new4.84 KB
new2.72 KB

New patch applied early return on this method

renatog’s picture

StatusFileSize
new6.46 KB
new1.33 KB

Please discard #32

New patch #33 applied early return on this method

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, no issues remaining, this all looks good now.

andypost’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.54 KB
new6.55 KB

I think the update name and displayable title needs review

Status: Needs review » Needs work

The last submitted patch, 35: 2997393-35.patch, failed testing. View results

andypost’s picture

Status: Needs work » Needs review

Random failure

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
    @@ -665,6 +665,9 @@ public static function trustedCallbacks() {
       public function validateExposeForm($form, FormStateInterface $form_state) {
    +    $remember_roles = ['options', 'expose', 'remember_roles'];
    +    $roles = $form_state->getValue($remember_roles);
    +    $form_state->setValue($remember_roles, array_filter($roles));
         $identifier = $form_state->getValue(['options', 'expose', 'identifier']);
    

    I see the latest patch added a variable for this, seems like an edge case of whether or not that's actually needed. If we do have one, then I would propose a different variable name, actually confused me for a second. Those aren't the roles, just the form key to access them. Maybe add _keys?

  2. +++ b/core/modules/views/views.post_update.php
    @@ -68,3 +68,36 @@ function views_post_update_rename_default_display_setting() {
    +/**
    + * Clean-up empty remember_roles display settings for views filters.
    + */
    +function views_post_update_update_remember_role_empty(&$sandbox) {
    +  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'view', function ($view) {
    +    $display_settings = $view->get('display');
    +    $save = FALSE;
    +
    +    foreach ($display_settings as &$display) {
    

    Personally, I think that writing update functions like this is adding a lot of complexity for very little gain. Their config schema is still fine and nothing is broken, we just have those bogus keys in there.

    What I think we should do however is clean up all existing default views in core. Search for remember_roles:, you'll have a bunch that have non-selected entries in there. Or "anonymous: '0'" that alone finds 72 examples. Mostly test views, but also in comment, files and people views.

vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new1.62 KB
new7.15 KB

@berdir , I have updated the point #38.1, and for #38.2 I have already tried updating the yml files using the yml parser and dumping the updated data for all the files inside /core, but the dumper has some yml alignment issues, so we went for the hook_post_update approach to update only the view content entities.

Thanks

berdir’s picture

Status: Needs review » Needs work

Hm, you changed the lines below that as well now, I'd avoid making coding standard improvements on otherwise unchanged lines.

On the second point, where I put that comment was maybe a bit misleading. I'm not talking about writing an update function to change those files. Update functions must never change files, you have to expect that they might be read-only.

But that's not needed. Instead, make the change (more or less) manually, and then put it in the patch. It's going to be a much bigger patch, but that's fine.

PS: There is no such thing as view content entities. They are always config entities, whether they are in files or in the database.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new41.96 KB
new40.12 KB

Here's a fix for all core's view, also updated upgrade path test to use copy of affected views.view.who_s_online.yml

Looks ready to go, probably backportable to next 9.2.x

Sorry for interdiff bigger then patch

+++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
@@ -665,14 +665,18 @@ public static function trustedCallbacks() {
-    $limit_operators = $form_state->getValue(['options', 'expose', 'operator_limit_selection']);
-    $operators_selected = $form_state->getValue(['options', 'expose', 'operator_list']);
+    $limit_operators = $form_state->getValue([

reverted it as out of scope

berdir’s picture

+++ b/core/modules/views/tests/src/Functional/Update/UserRememberRolesFilterSettingTest.php
@@ -27,21 +28,24 @@ protected function setDatabaseDumpFiles() {
+    $this->assertEquals(SAVED_NEW, $view->save());

hm, considering that we updated several default views like comment and admin people, I would expect that we can just use those to assert this and don't need to create a test?

We did that before with the files view, nothing wrong with that? The update test is based on a database dump, that still has the old definition with : '0'?

andypost’s picture

StatusFileSize
new1.94 KB
new39.1 KB

Removed test view as "files" comes from dump also fixed test

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lendude’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/views/tests/src/Functional/Wizard/BasicTest.php
    @@ -226,4 +226,51 @@ public function testWizardDefaultValues() {
    +    $leading_slash_view = [];
    

    Confusing variable name in this context, copy/paste left over, lets find a better name

  2. +++ b/core/modules/views/tests/src/Functional/Wizard/BasicTest.php
    @@ -226,4 +226,51 @@ public function testWizardDefaultValues() {
    +    $this->assertEquals($leading_slash_view['page[path]'], $this->cssSelect('#views-page-1-path')[0]->getText());
    

    No need to check that here we have the test that this was copied from for this

  3. +++ b/core/modules/views/tests/src/Functional/Wizard/BasicTest.php
    @@ -226,4 +226,51 @@ public function testWizardDefaultValues() {
    +    $expose_settings = [
    +      'options[expose][remember]' => 1,
    +      'options[expose][remember_roles][anonymous]' => 'anonymous',
    +      'options[expose][remember_roles][authenticated]' => 'authenticated',
    +    ];
    

    We don't explicitly add a third role in this test, so if these are the only two available roles in this set up (no idea if that is the case or not...), this would be the same as selecting all the roles. I would add a new role and make sure at the end that this isn't added to the config.

lendude’s picture

+++ b/core/modules/views/tests/src/Functional/Update/UserRememberRolesFilterSettingTest.php
--- a/core/modules/views/tests/src/Functional/Wizard/BasicTest.php
+++ b/core/modules/views/tests/src/Functional/Wizard/BasicTest.php

Also, not sure this is the best place to add this test, not really related to the Wizard at all

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

altcom_neil’s picture

Hi, thanks for the work so far on this.

We have multiple sites built on the same systems but with some custom user roles. Comparing changes to views between sites has always thrown up the differences in these sections even though none have actually made any changes at all.

The latest patch didn't apply for me (D10.3.2), so I have created just a patch for the \Drupal\views\Plugin\views\filter\FilterPluginBase::validateExposeForm method from the latest D11 code.

This works stripping all of the redundant roles from the config.

altcom_neil’s picture

StatusFileSize
new826 bytes

Sorry - ignore this one - wrong patch file!

altcom_neil’s picture

altcom_neil’s picture

StatusFileSize
new863 bytes

Sorry, correct patch file for 10.3+/11.x

tobiasb made their first commit to this issue’s fork.

tobiasb’s picture

Issue summary: View changes
Status: Needs work » Needs review

I move the patch into a MR and fixes the rejected changes + typehints.

The post update uses now core/modules/views/src/ViewsConfigUpdater.php and moves the test into \Drupal\Tests\views\Functional\Plugin\FilterTest, because there are also other test for \Drupal\views\Plugin\views\filter\FilterPluginBase.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs upgrade path tests

Will probably need upgrade path tests.

Looks good!

tobiasb’s picture

Assigned: Unassigned » tobiasb
tobiasb’s picture

Assigned: tobiasb » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs upgrade path tests

The update test is in core/modules/views/tests/src/Functional/Update/UserRememberRolesFilterSettingTest.php.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Thank you, looks ready

oily’s picture

Ran the 'test-only' test. It fails as it should. Pipeline otherwise all green.

oily’s picture

Corrected one typo then remembered the etiquette so added two threads: code comment nits.

catch’s picture

Status: Reviewed & tested by the community » Needs work

One comment on the post update, overall this looks good though.

tobiasb’s picture

Status: Needs work » Reviewed & tested by the community

Rebased && add missing call to setDeprecationsEnabled.

  • catch committed b25bc52e on 11.x
    Issue #2997393 by tobiasb, andypost, vakulrai, renatog, ankithashetty,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Looks great now, really nice to see this one ready.

Committed/pushed to 11.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

dadderley’s picture

Hello,

I have just upgraded a site to 11.2.2.

When running the DB update script I get this:

 >  [notice] Update started: views_post_update_update_remember_role_empty
>  [error]  The "taxonomy_term_name_into_id" plugin does not exist. Valid plugin IDs for Drupal\views\Plugin\ViewsPluginManager are: taxonomy_term_name, user_name, entity:block, entity:block_content_type, entity:block_content, entity:captcha_point, entity:comment_type, entity:comment, entity:editor, entity:entity_subqueue, entity:entity_queue, entity:field_config, entity:field_storage_config, entity:file, entity:filter_format, entity:image_style, entity:imce_profile, entity:linkit_profile, entity:media_type, entity:media, entity:menu_link_content, entity:menu_position_rule, entity:metatag_defaults, entity:node, entity:node_type, entity:paragraphs_library_item, entity:path_alias, entity:redirect, entity:responsive_image_style, entity:search_page, entity:shortcut, entity:shortcut_set, entity:slick, entity:smart_date_format, entity:smart_date_rule, entity:smart_date_override, entity:menu, entity:action, entity:taxonomy_vocabulary, entity:taxonomy_term, entity:user, entity:user_role, entity:webform_options, entity:webform_submission, entity:webform, entity:pathauto_pattern, entity:xmlsitemap, entity:view, entity:paragraph, entity:paragraphs_type, entity:entity_view_display, entity:entity_form_mode, entity:entity_view_mode, entity:entity_form_display, entity:base_field_override, entity:date_format, none, numeric
>  [error]  Update failed: views_post_update_update_remember_role_empty
 [error]  Update aborted by: views_post_update_update_remember_role_empty
 [error]  Finished performing updates.

Pardon my ignorance, but is this related at all to this issue?

Just added...
I had a misbehaving unused view that caused this error. I deleted the view and I was able to complete the update. Interestingly enough, I could not disable or duplicate the view in the views overview page. When I tried to edit the view I got a WSOD and this error:
Drupal\Component\Plugin\Exception\PluginNotFoundException: The "taxonomy_term_name_into_id" plugin does not exist. Valid plugin IDs for Drupal\views\Plugin\ViewsPluginManager are: taxonomy_term_name, user_name, entity:block, entity:block_content_type, entity:block_content, entity:captcha_point, entity:comment_type, entity:comment, entity:editor, entity:entity_subqueue, entity:entity_queue, entity:field_config, entity:field_storage_config, entity:file, entity:filter_format, entity:image_style, entity:imce_profile, entity:linkit_profile, entity:media_type, entity:media, entity:menu_link_content, entity:menu_position_rule, entity:metatag_defaults, entity:node, entity:node_type, entity:paragraphs_library_item, entity:path_alias, entity:redirect, entity:responsive_image_style, entity:search_page, entity:shortcut, entity:shortcut_set, entity:slick, entity:smart_date_format, entity:smart_date_rule, entity:smart_date_override, entity:menu, entity:action, entity:taxonomy_vocabulary, entity:taxonomy_term, entity:user, entity:user_role, entity:webform_options, entity:webform_submission, entity:webform, entity:pathauto_pattern, entity:xmlsitemap, entity:view, entity:paragraph, entity:paragraphs_type, entity:entity_view_display, entity:entity_form_mode, entity:entity_view_mode, entity:entity_form_display, entity:base_field_override, entity:date_format, none, numeric in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 53 of /home/customer/www/ferniefix.com/public_html/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php).

tobiasb’s picture

@dadderley You have something in your view which is broken.

The plugin ID comes from https://www.drupal.org/project/views_taxonomy_term_name_into_id

dadderley’s picture

@tobiasb
Thanks, I found the error in the view.

tregonia’s picture

Doing updates today and found this in my logs. Searching for views_post_update_update_remember_role_empty leads me to this issue.

Listed as an update to execute
views update_remember_role_empty post-update Clean-up empty remember_roles display settings for views filters.

>  [notice] Update started: views_post_update_update_remember_role_empty
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [warning] Undefined array key "type" ComponentValidator.php:90
>  [notice] Update completed: views_post_update_update_remember_role_empty
 [success] Finished performing updates.

I cannot speak to the issues that cause or come from this; I am only reporting the message.