Problem/Motivation
Solve warning on PHP 8.1
Deprecated function: unserialize(): Passing null to parameter #1 ($data) of type string is deprecated in Drupal\Core\Entity\Sql\SqlContentEntityStorage->loadFromSharedTables() (line 602
Steps to reproduce
Steps to reproduce:
1. Create a new entity type
2. Install the newly added entity type
3. Create at least one entity (id: 1)
4. Add map base field to created entity type:
$fields['data'] = BaseFieldDefinition::create('map')
->setLabel(t('Data'))
->setDescription(t('A serialized array of additional data.'));
5. load entity
6. Warning will occur
Proposed resolution
In case the passed string is not unserializeable, false is returned and E_WARNING is issued.
See https://www.php.net/manual/en/function.unserialize.php
In SqlContentEntityStorage, there is more than one call to unserialize(), and all usages in it have the same issue.
So, let's introduce a utility method called safeUnserialize()
to replace the current usages of unserialize() in SqlContentEntityStorage
- It's declared as public static
, so that it can be used elsewhere.
- it returns NULL if it gets an error calling unserialize but returns FALSE if the input is FALSE.
- it returns NULL if the value is an empty string or NULL.