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 Fletcher05 May 2024
Best practices for configuring Twig debug settings in Drupal 10
Alright, picture this: you're knee-deep in Drupal 10 development, churning out code like a pro. But hold up, what's this? Twig debug mode is still on in production? Cue the headaches. Suddenly, your beautifully crafted HTML is drowning in unnecessary output, and innocent contact form responses are...