Prevent error on views session retrieving

Created on 29 July 2022, almost 2 years ago
Updated 15 January 2024, 5 months ago

Problem/Motivation

In the FilterPluginBase abstract class we are trying to retrieve the session from the views entity in both storeGroupInput() and storeExposedInput() methods.

I had a specific use case where, in storeExposedInput() this led to the following 500 error:
Error : Call to a member function get() on null in Drupal\views\Plugin\views\filter\FilterPluginBase->storeExposedInput() (/var/www/html/web/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php ligne 1563)

These methods try to store all exposed filters in the user session if the views is configured to remember the user inputs.
This error occurs because we are not defensive enough on wether the previously called getSession() method retrieved the session properly or not. If the retrieved session is null (which occured in my use case), then we have a major failure that prevent any further execution of the code.

Steps to reproduce

We had this issue on a specific setup with search_api, the elasticsearch_connector and facets modules.
The issue happens when trying to select a specific boolean facet option on a search_api index view with ajax submission and exposed filters values to be stored in the session.
But regarding the issue I think it can happen in other configurations where the session is not properly retrieved from the views entity.

Proposed resolution

Be more defensive when trying to reach the views session object.

I propose to modify this implementation:
$views_session = $session->get('views', []);

With the following:
$views_session = NULL !== $session ? $session->get('views', []) : [];

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

None

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
User systemΒ  β†’

Last updated 2 days ago

Created by

πŸ‡«πŸ‡·France nicoloye

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States AaronBauman Philadelphia

    setting latest patch to NR and hiding duplicate and #3

    patch #4 is working for me, thanks

  • Status changed to Needs work 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks for reporting.

    Will need a test case showing the issue as a next step.

  • πŸ‡¬πŸ‡§United Kingdom james.williams

    Thanks for this - I found the patch from #4 fixed my issue, where the views UI preview was failing, with a "SessionNotFoundException: Session has not been set" exception. I didn't quite get to the very bottom of the issue, but it might have been to do with my page display having exposed filters, which would remember their last setting (i.e. using the session), plus several other attached displays which were set to inherit exposed filters whilst also using contextual filters that would always use a default argument. So a bit complicated - and it's entirely possible that's not relevant. But I did debug to the point of confirming that the last request on the request stack had a null session property. That's despite me being logged in as an admin, using the views UI. The views preview works by creating a new request object and then pushing it to the stack - perhaps that needs to include copying any session object over?

    For the patch itself, it seems odd to spin up an empty session object, which won't be backed by an actual session, just so that there's an array to fall back on. Especially if $session->set() ends up getting called, as the data set won't end up in the actual saved session.

Production build 0.69.0 2024