TypeError: Cannot access offset of type string on string in Drupal\views\Plugin\views\filter\GroupByNumeric->opSimple()

Created on 14 April 2023, over 1 year ago
Updated 13 September 2024, 2 months ago

Overview

On migrating from PHP 7.4 to PHP 8, we receive the following error for pages leveraging the Core module Views filtering:

TypeError: Cannot access offset of type string on string in Drupal\views\Plugin\views\filter\GroupByNumeric->opSimple() (line 38 of /code/web/core/modules/views/src/Plugin/views/filter/GroupByNumeric.php)

This is a well-documented backwards incompatibility for PHP 8 (see here)

What ChatGPT helped explain to me

Based on the error message provided, it seems like the issue is occurring within the opSimple() method of the GroupByNumeric filter plugin in Drupal's Views module.

  protected function opSimple($field) {
    $placeholder = $this->placeholder();
    $this->query->addHavingExpression($this->options['group'], "$field $this->operator $placeholder", [$placeholder => $this->value['value']]);
  }

The most likely cause of this error is that the $this->value variable is a string instead of an array or object. In PHP 8, accessing an offset of a string with square brackets like $string[0] will cause a TypeError.

To fix this issue, you should check the type of $this->value before accessing it as an array. If $this->value is a string, you'll need to handle it differently than if it were an array.

Environment info

Drupal Version: 9.5.7
Web Server: nginx/1.21.6
PHP Version: 8.1.14
Database Version: 10.4.25-MariaDB-log

What I'm guessing needs to happen

There are various other issues open as bug fixes for different View filters in /code/web/core/modules/views/src/Plugin/views/filter/.. I'm guessing the core maintainers need to fix for all as patches won't really fix each View filter. Either that or I'm totally misunderstanding the inputs for these filters!

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component
ViewsΒ  β†’

Last updated about 1 hour ago

Created by

Live updates comments and jobs are added and updated live.
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.

  • Issue created by @etiennejacquot
  • Status changed to Postponed: needs info over 1 year ago
  • πŸ‡³πŸ‡±Netherlands Lendude Amsterdam

    If you look at the parent method \Drupal\views\Plugin\views\filter\NumericFilter::opSimple there $this->value['value'] is also used, it seems to work there, since that filter is used much more often than GroupByNumeric I trust that is is what is expected at that point.

    So for me the question is, how come this is a string in your case? Do you have steps to reproduce this on a clean Drupal install? Or (less useful but might help) the config file for the View that is giving this error?

  • πŸ‡ΊπŸ‡ΈUnited States rickward

    I am a colleague of Etienne who is trying to assist with this issue.

    Interestingly, if we try to edit this view on the php 8 install, we get a similar but different error:

    TypeError: Cannot access offset of type string on string in Drupal\views\Plugin\views\filter\NumericFilter->adminSummary() (line 394 of /code/web/core/modules/views/src/Plugin/views/filter/NumericFilter.php)

    I have attached the config file.

  • πŸ‡ΊπŸ‡ΈUnited States rickward

    Also, this view is using Better Exposed Filters, so I wonder if that could be a contributing factor.

  • πŸ‡ΊπŸ‡ΈUnited States rickward

    OK - This view is using a filter criterion that I've not seen before: COUNT(DISTINCT Content: Published)
    I'm not sure what that is accomplishing in this context, but removing that filter criterion fixes the view. Any insight anyone could offer would be greatly appreciated.

  • πŸ‡¦πŸ‡ΊAustralia pameeela

    @rickward were you able to resolve this issue?

  • πŸ‡΅πŸ‡°Pakistan hamza_niazi

    I m facing the same issue with view using Exposed form style: basic

Production build 0.71.5 2024