TypeError: Cannot access offset of type string on string in core/modules/views/src/Plugin/views/area/Text.php)

Created on 2 November 2023, about 1 year ago
Updated 15 February 2024, 10 months ago

Problem/Motivation

Getting errors when viewing Recent Log Messages.

TypeError: Cannot access offset of type string on string in Drupal\views\Plugin\views\area\Text->preQuery() (line 51 of core/modules/views/src/Plugin/views/area/Text.php).

And one when deleting the messages
TypeError: Cannot access offset of type string on string in Drupal\views\Plugin\views\area\Text->render() (line 67 of core/modules/views/src/Plugin/views/area/Text.php).

πŸ› Bug report
Status

Postponed: needs info

Version

9.5

Component
ViewsΒ  β†’

Last updated about 10 hours ago

Created by

πŸ‡¦πŸ‡²Armenia alen simonyan

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

Comments & Activities

  • Issue created by @alen simonyan
  • πŸ‡¦πŸ‡²Armenia alen simonyan

    Here is the patch for this.

  • @alen-simonyan opened merge request.
  • last update about 1 year ago
    30,341 pass
  • Status changed to Postponed: needs info about 1 year ago
  • πŸ‡³πŸ‡±Netherlands Lendude Amsterdam

    Thanks for reporting this.

    Do you have some steps to reproduce this on a clean Drupal install? Usually this is a symptom of a problem further up and just adding some checks is just hiding the real problem

  • πŸ‡¦πŸ‡²Armenia alen simonyan

    Hi @Lendude,

    I diddn't manage to reproduce this on fresh install yet.

  • πŸ‡§πŸ‡ͺBelgium seutje Antwerp

    I also ran into this issue. The MR linked above fixed it for 9.x, but failed to apply when I upgraded to 10.x, so I rerolled it, but I changed it to fall back on $this->options['content']

    Unfortunately, I have no clue what the cause is, so I cannot provide any more information... I have a strong suspicion it's being caused by a control module, as I only experience this issue with 1 specific project.

  • I encountered the same error and implemented a temporary fix, which needs review. The issue arises because $this->options['content'] is expected to be an array but can sometimes be a string.

    Here’s the patch I applied to web/core/modules/views/src/Plugin/views/area/Text.php:

      /**
       * {@inheritdoc}
       */
      public function preQuery() {
        // Normalize $this->options['content'] to ensure it's an array.
        if (is_string($this->options['content'])) {
          // If it's a string, treat it as the content value with a default format.
          $this->options['content'] = [
            'value' => $this->options['content'],
            'format' => filter_default_format(),
          ];
        }
    
        // Proceed as normal with the now-normalized content array.
        $content = $this->options['content']['value'];
    
        // Check for tokens that require a total row count.
        if (str_contains($content, '[view:page-count]') || str_contains($content, '[view:total-rows]')) {
          $this->view->get_total_rows = TRUE;
        }
      }
  • πŸ‡³πŸ‡±Netherlands Lendude Amsterdam

    $this->options['content'] is expected to be an array but can sometimes be a string

    @lance lancelot Have you been able to pin down when or why this would be the case? We still need steps to reproduce this on a fresh Drupal install since as it stands currently, the error might just be the correct way to handle this because there is something wrong upstream.

  • The error occurred after upgrading from Drupal 9 to Drupal 10. To investigate, I exported the configuration files and searched for occurrences of 'content:' strings.

    For example, could the following configuration in views.view.comments_recent.yml be causing the error?

    label: 'Recent comments'
    description: 'Recent comments.'
    display:
      default:
        display_title: Default
        display_options:
          title: 'Recent comments'
          fields:
            changed:
              settings:
                future_format: '@interval hence'
                past_format: '@interval ago'
          empty:
            area_text_custom:
             content: 'No comments available.' # Could this be the source of the problem?
      block_1:
        display_title: Block
        display_options:
          block_description: 'Recent comments'
          block_category: 'Lists (Views)'
    

    I’m not entirely sure, but could the content: 'No comments available.' string format be causing the issue? Should it now be structured as:

    content:
      value: 'No comments available.'
      format: 'plain_text'
    

    Is this new format required in Drupal 10, and if so, was this change documented? I’d appreciate any clarification or guidance regarding this.

  • πŸ‡©πŸ‡ͺGermany alxn

    I just encountered this issue with Drupal 10.3.10 and Workbench 1.6 and can confirm that the error is resolved after changing the content structure in the relevant configuration to:

              content:
                value: '<h3>Example</h3>'
                format: 'basic_html' 
    

    I noticed that the following warning was issued during the configuration import with the structured content:
    [warning] Array to string conversion StatementWrapperIterator.php:113

Production build 0.71.5 2024