TypeError: Unsupported operand types: array + null

Created on 11 October 2022, about 2 years ago
Updated 8 August 2023, over 1 year ago

Problem/Motivation

We are on Drupal core 9.4.8 and PHP 8.0.x. We're getting the following error on the server after upgrading core from 9.3.12 to 9.4.8:

The website encountered an unexpected error. Please try again later.
TypeError: Unsupported operand types: array + null in Drupal\select2\Element\Select2::processSelect() (line 209 of modules/contrib/select2/src/Element/Select2.php).

The code involved is as follows:

    if (!$element['#multiple'] && !isset($element['#options'][$element['#empty_value']])) {
      $empty_option = [$element['#empty_value'] => ''];
      $element['#options'] = $empty_option + $element['#options'];
    }

$element['#options'] seems to be an array and $empty_options is null. There are no comments in this section so I'm not entirely sure what it's trying to do here. Normally I'd try to figure it out and start a merge request, but I must admit it's a bit vexing trying to figure out what's actually supposed to be happening here. Is it trying to set the value of the option field to the empty option? If so, what's going on with the "+"?

πŸ› Bug report
Status

Closed: works as designed

Version

1.0

Component

Field widgets

Created by

πŸ‡ΊπŸ‡ΈUnited States loopy1492

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.

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

    We finally got to the bottom of this and it was a weird situation.

    We are filling a select2 field on the fly with

    $nodeStorage = $this->entityTypeManager->getStorage('node');
    $nodeQuery = $nodeStorage->getQuery();
    

    Then it's supposed to filter based on node type...

    $custom_node_type_ids = $nodeQuery
            ->condition('type', 'custom_node_type')
            ->condition('status', 1)
            ->sort('created', 'DESC')
            ->accessCheck()
            ->execute();
    

    But for some reason, when I debug the output from this function, it returns not only the custom_node_type nodes, but also the node of the current page. The current page is of the "page" content type, but that doesn't seem to matter to the nodeQuery.

    I had to add the following:

    $my_nodes_ids = $nodeQuery
            ->condition('type', 'my_nodes')
            ->condition('status', 1)
            ->sort('created', 'DESC')
            ->condition('nid', '4856', '!=')
            ->accessCheck()
            ->execute();
    

    After that, my function that was filling the select2 field no longer had null values for the fields it was trying to reference.

    I suspect this has always been a problem and Drupal just passed over the null value before, but now it's less forgiving of sloppy code.

    At any rate, this is not a priblem with select2 at all.

  • Status changed to Closed: works as designed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States loopy1492
Production build 0.71.5 2024