Skip to main content

I'm getting the following error

TypeError: Cannot assign null to property Drupal\views\Plugin\views\argument\ArgumentPluginBase::$operator of type string in Drupal\views\Plugin\views\argument\ArgumentPluginBase->unpackArgumentValue() (line 1302 of core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php).

As noted above, the relevant function is unpackArgumentValue

**
   * Splits an argument into value and operator properties on this instance.
   *
   * @param bool $force_int
   *   Enforce that values should be numeric.
   */
  protected function unpackArgumentValue($force_int = FALSE) {
    $break = static::breakString($this->argument, $force_int);
    $this->value = $break->value;
    $this->operator = $break->operator;
  }

This function is responsible for parsing and assigning values to the value and operator properties of an instance based on the passed argument. It appears to be causing issues due to assigning null to a property (operator) that expects a string type, as indicated by the TypeError you've encountered.

 

Understanding the Problem

The issue arises because the breakString method (called within unpackArgumentValue) is expected to return an object with both value and operator properties. However, if the implementation of breakString does not properly initialise operator (especially when the input doesn't explicitly include an operator), it might return null for the operator. Since the operator property of the ArgumentPluginBase instance is declared as a non-nullable string, trying to assign null to it leads to a TypeError.

 

Steps to Address the Issue

First look at how breakString is implemented and ensure it always returns an object with both value and operator initialised. The method should handle cases where no operator is discernible in the input argument and default to a reasonable operator (like an empty string or a specific default operator).

protected static function breakString($argument, $force_int = FALSE) {
   // Initialisation of return object
   $result = new \stdClass();
   $result->value = $argument;  // Default value
   $result->operator = '';      // Default operator as empty string
   // Logic to split argument and detect operator
   // ...
   return $result;
}

Ensure that unpackArgumentValue can handle cases where breakString might still return unexpected nulls, although ideally, breakString should handle this.

protected function unpackArgumentValue($force_int = FALSE) {
   $break = static::breakString($this->argument, $force_int);
   $this->value = $break->value;
   $this->operator = $break->operator ?? '';
}

After making changes, test thoroughly to ensure that the function correctly handles all expected input scenarios, including those that previously resulted in errors. Maybe create unit tests for unpackArgumentValue to check various types of input, ensuring they are split and assigned correctly.

 

Instead of directly modifying core files, apply the patch using the composer.json file, ensuring that your changes are reapplied each time the affected code is updated.

 

Download the Patch

Start by downloading the patch file containing your changes. In your case, it's named argument-plugin-base-operator.patch.
Define Patches in composer.json: Inside your Drupal project's composer.json, define the patches you want to apply, specifying the file paths and the patches to apply.

"patchLevel": {
    "drupal/core": "-p2"
},
"patches": {
    "drupal/core": {
          "views unpackArgumentValue": "patches/0001-Issue-01-ArgumentPluginBase-php-operator-of-type-str.patch"
    },
    "drupal/facets": {
        "deprecated function": "patches/3336646-function-is-deprecated.patch"
    }
},

 

Run Composer Update

After defining the patches, run composer update to apply them to the Drupal core codebase.

 

Test the Changes

Thoroughly test your Drupal site to ensure that the issue is resolved and that no new issues have been introduced.

 

Contribute the Patch (Optional)

If the changes are successful and beneficial to the Drupal community, consider contributing the patch back to the Drupal project by submitting it to the appropriate issue queue on Drupal.org for review and inclusion in future releases.

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...