Error: Call to a member function get() on null

Created on 31 May 2023, almost 2 years ago

Problem/Motivation

I get an error when submitting an exposed form in the Views UI.

Error: Call to a member function get() on null in Drupal\views\Plugin\views\filter\FilterPluginBase->storeExposedInput() (line 1579 of /var/www/html/web/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php)
#0 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1375): Drupal\views\Plugin\views\filter\FilterPluginBase->storeExposedInput(Array, true)
#1 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1270): Drupal\views\ViewExecutable->_build('filter')
#2 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1399): Drupal\views\ViewExecutable->build(NULL)
#3 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1462): Drupal\views\ViewExecutable->execute(NULL)
#4 /var/www/html/web/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php(2459): Drupal\views\ViewExecutable->render()
#5 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1670): Drupal\views\Plugin\views\display\DisplayPluginBase->preview()
#6 /var/www/html/web/core/modules/views_ui/src/ViewUI.php(611): Drupal\views\ViewExecutable->preview('default', Array)
#7 /var/www/html/web/core/modules/views_ui/src/ViewPreviewForm.php(62): Drupal\views_ui\ViewUI->renderPreview('default', Array)

Steps to reproduce

Create a view with an exposed filter form
Visit the view at /admin/structure/views/view/VIEW_ID
Note that this automatically goes to the "default" display, even though I didn't go to /admin/structure/views/view/VIEW_ID/edit/default
Submit a value in the exposed filter form
Check the logs and see the error

The error occurs in FilterPluginBase::storeExposedInput at this code:

    $session = $this->view->getRequest()->getSession();
    $views_session = $session->get('views', []);

The error can also occur at FilterPluginBase::storeGroupInput, as it does the same thing.

In the described scenario, $session will be null.

This needs to be fixed, because in Symfony 5 it will throw an error. See these lines in Request::getSession:

        if (null === $session) {
            @trigger_error(sprintf('Calling "%s()" when no session has been set is deprecated since Symfony 4.1 and will throw an exception in 5.0. Use "hasSession()" instead.', __METHOD__), \E_USER_DEPRECATED);
            // throw new \BadMethodCallException('Session has not been set.');
        }

Proposed resolution

It's not enough to check if hasSession() and if not then set $views_session = [];. I tried this, and gets down to

$session->set('views', $views_session);

Which has the same error, because $session is still null.

You could return early if there's no session. But it might be better to create a session or redirect to the /edit/default URL. I'm not really sure what a good solution would be.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

5.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024