Contents of Viewfield - Arguments can cause unhandled PHP Errors

Created on 26 August 2024, 3 months ago

Problem/Motivation

Viewfield - Arguments with whitespace (e.g., spaces) can cause a PHP fatal error when rendered on the frontend. This is especially problematic when using Viewfield with Inline Blocks in Layout Builder, as the error can prevent access to update Layout Builder in preview mode.

Steps to reproduce

  1. Create a View
  2. Create a Block type (/admin/structure/block-content/add) that has a Viewfield field with Allowed View.
  3. Add Content block (/block/add?destination=/admin/content/block) using the Block type containing the Viewfield. In the Advanced options - Arguments enter a value starting with a space (i.e " 45")
  4. Use Block Layout to render render the Content Block containing the Viewfield with invalid argument
  5. Load the frontend route rendering the Viewfield block to throw 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).
Drupal\views\Plugin\views\argument\ManyToOne->title() (Line: 1039)
Drupal\views\Plugin\views\argument\ArgumentPluginBase->getTitle() (Line: 1125)
Drupal\views\ViewExecutable->_buildArguments() (Line: 1282)
Drupal\views\ViewExecutable->build(NULL) (Line: 1406)
Drupal\views\ViewExecutable->execute() (Line: 167)
Drupal\viewfield\Plugin\Field\FieldFormatter\ViewfieldFormatterDefault->viewElements(Object, 'en') (Line: 91)
Drupal\Core\Field\FormatterBase->view(Object, 'en') (Line: 96)
Drupal\viewfield\Plugin\Field\FieldFormatter\ViewfieldFormatterDefault->view(Object, NULL) (Line: 268)
...

Proposed resolution

  • Prevent the input of invalid Arguments, add input validation to the Viewfield form
  • Resolve existing errors by improving ViewfieldFormatterDefault->processArguments to sanitize arguments.

Remaining tasks

Confirm the proposed resolution and implementation.

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Active

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States ian.ssu

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

Comments & Activities

  • Issue created by @ian.ssu
  • πŸ‡ΊπŸ‡ΈUnited States ian.ssu

    I’ve committed a change to address the PHP fatal error caused by Viewfield arguments with whitespace. This update sanitizes existing arguments by trimming spaces in ViewfieldFormatterDefault->processArguments. As a result, the error we were seeing when content editors added arguments with spaces in Viewfield -Arguments is resolved.

  • πŸ‡ΊπŸ‡ΈUnited States brayn7 Lexington, Ky

    I have this same type of issue where I have a term id or multiple term id's and one of my setups comes back with ['1071\t'] nothing I do in the ui will remove it. This change does solve it for me tho. @ian.ssu can you set a merge request on that change you made?

Production build 0.71.5 2024