Error: Call to a member function __toString() on string in Drupal\crm_core_contact\Entity\Individual->saveLabel() (line 216 of modules/contrib/crm_core/modules/crm_core_contact/src/Entity/Individual.php).

Created on 12 February 2025, 3 months ago

Problem/Motivation

We receive a fatal exception in the checkout:
Error: Call to a member function __toString() on string in Drupal\crm_core_contact\Entity\Individual->saveLabel() (line 216 of modules/contrib/crm_core/modules/crm_core_contact/src/Entity/Individual.php).

Steps to reproduce

We upgraded drupal/name from 1.0.0-rc5 to 1.0.0.
Attempt to complete the checkout and have the event subscriber attempt to create an individual.

NameFormatter::format() can return either a string or FormattableMarkup.
The change to the NameFormat Parser (f9f9d9163eb4efd355282d68fabd4d4955492ac6) appear to have changed the return type in some cases

The explicit call to __toString() on L:216 in Individual::saveLabel() requires the return value of the $name_formatter->format() be an object.

```
/**
* Sets the Individual's label from the name.
*/
protected function saveLabel() {
$name_array = [];
$name_formatter = \Drupal::service('name.formatter');

$type = IndividualType::load($this->bundle());
$component_layout = 'default';
$formatted_name = NULL;

if ($this->name[0] != NULL) {
$name_array = $this->name[0]->getValue();
$formatted_name = $name_formatter->format($name_array, $component_layout)->__toString();
}
```

Proposed resolution

Cast to string which will work if the return value of the formatter is a string.

```
/**
* Sets the Individual's label from the name.
*/
protected function saveLabel() {
$name_array = [];
$name_formatter = \Drupal::service('name.formatter');

$type = IndividualType::load($this->bundle());
$component_layout = 'default';
$formatted_name = NULL;

if ($this->name[0] != NULL) {
$name_array = $this->name[0]->getValue();
$formatted_name = (string) $name_formatter->format($name_array, $component_layout)->;
}
```

Remaining tasks

Creating an MR now to fix.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡¨πŸ‡­Switzerland tcrawford

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024