Warning: htmlspecialchars() expects parameter 1 to be string

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

Problem/Motivation

Error message:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in Drupal\Component\Utility\Html::escape() (line 424 of core/lib/Drupal/Component/Utility/Html.php).
Drupal\Component\Utility\Html::escape(Array) (Line: 213)
Drupal\views\Plugin\views\HandlerBase->sanitizeValue(Array) (Line: 1129)
Drupal\views\Plugin\views\field\FieldPluginBase->render(Object) (Line: 1158)
Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender(Object) (Line: 238)
template_preprocess_views_view_field(Array, 'views_view_field', Array)

Steps to reproduce

Add a tally field to a view and for "Multiple field settings" select something other than "Simple aggregate count" or "Individual count per item".

Proposed resolution

Allow for other multiple field settings

The ::getValue function for AggregateField handles "Simple aggregate count" and "Individual count per item". For the purposes of these options we didn't need an advanced render. This function should have a passthrough though to allow rendering for other options like "Ordered list".

Render items properly for the other multiple field settings

If something like "Ordered list" is selected, then the return from ::getValue will be an array, not a string. The AggregateField::render() method should check to see if it's an array, and pass it to $this->renderItems, (which is what advancedRender basically would do), or sanitize the value. (though this could be unnecessary)

Make sure the "count" is used and not the "target_id"

The base of the field is the target_id, so when parent::getValue($values, $field) is called, it will use that instead of the count. The easiest way to ensure we use the count is to just force parent::getValue($values, 'count') as the passthrough for the ::getValue override.

Refactor ::getValue to be a little less confusing

  • It initializes "$values" as an array which is misleading because if there is an actual value it will end up returning a string.
  • $values is also the name of the ResultRow argument, so that's completely lost. $values should just be changed to $value to be more consistent with the function name.
  • There are two foreach loops that need to create an array, and then calculate a value from that array, so those two foreach loops should just create a separate array.

Remaining tasks

Testing

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States asherry

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.

Production build 0.71.5 2024