Problem/Motivation

The TextFirstPara field formatter causes an InvalidArgumentException: "0" is an invalid render array key error when a text field contains content without a tag. This occurs because the formatter returns a scalar string value ('') instead of a proper render array when no paragraph element is found.

The error manifests during rendering: InvalidArgumentException: "0" is an invalid render array key. Value should be an array but got a string. in Drupal\Core\Render\Element::children()

This prevents pages from rendering when nodes with empty or non-paragraph-wrapped text fields are displayed using the "First Paragraph" formatter.

Steps to reproduce

Create a content type with a text field (text_long or text_with_summary)
Configure the field display to use the "First Paragraph" formatter
Create a node where the text field either: Is empty, or Contains text without tags (e.g., plain text or other HTML elements)
Attempt to render the node in the configured view mode
Observe the InvalidArgumentException error

Proposed resolution

Change line 93 in TextFirstPara.php from: $elements[$delta] = '';

To: $elements[$delta] = [ '#markup' => '', ];

This ensures that even when no paragraph is found, a valid render array structure is returned instead of a scalar string value.

Remaining tasks

Review and commit the patch
Add test coverage for edge cases (empty fields, non-paragraph content)
Create a new release with the fix

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jv24 created an issue. See original summary.

jv24’s picture

Title: Invalid render array key error when text field has no paragraph tags » InvalidArgumentException: "0" is an invalid render array key
nicholasthompson’s picture

Thanks - this should be fixed in dev and the upcoming 2.1.0 release.

nicholasthompson’s picture

Version: 2.0.0 » 2.0.x-dev
Assigned: Unassigned » nicholasthompson
Status: Needs review » Reviewed & tested by the community

The commit ahead of this (found during D12 upgrades) made it an empty array. Your fix is more explicit, so adopting that instead + adapting some new test coverage:
https://git.drupalcode.org/project/first_paragraph/-/merge_requests/3