Problem/Motivation
Creating a configuration entity via new ConfigEntityClass() triggers deprecations in PHP 8.1, use ConfigEntityClass::create() instead. This is because new ConfigEntityClass() does not trigger the storage create methods and hooks. Then the entity does not get a UUID and we end up triggering a deprecation via the query that looks for duplicates using the UUID.
Steps to reproduce
$ vendor/bin/drush php
Psy Shell v0.10.8 (PHP 8.0.8 — cli) by Justin Hileman
Drush Site-Install (Drupal 9.3.0-dev)
>>> use \Drupal\filter\Entity\FilterFormat;
>>> $format = new FilterFormat(['format' => 'test', 'name' => 'Test', 'filters' => []], 'filter_format');
=> Drupal\filter\Entity\FilterFormat {#6105
#uuid: null,
#langcode: "en",
#status: true,
#dependencies: [],
#name: "Test",
#format: "test",
#weight: 0,
#filters: [],
}
>>> $format->isNew();
=> false
>>> $format->save();
=> 2
>>> $format->uuid();
=> null
>>>
>>> $format = FilterFormat::create(['format' => 'test2', 'name' => 'Test 2', 'filters' => []]);
=> Drupal\filter\Entity\FilterFormat {#5871
#uuid: "6126f844-6abf-4630-a7db-c5377e140ae1",
#langcode: "en",
#status: true,
#dependencies: [],
#name: "Test 2",
#format: "test2",
#weight: 0,
#filters: [],
}
>>> $format->isNew();
=> true
>>> $format->save();
=> 1
>>> $format->uuid();
=> "6126f844-6abf-4630-a7db-c5377e140ae1"
Run
- core/modules/locale/tests/src/Kernel/LocaleConfigurableLanguageManagerTest.php
- core/modules/workflows/tests/src/Kernel/ComplexWorkflowTypeTest.php
- core/modules/workflows/tests/src/Kernel/RequiredStatesTest.php
on PHP 8.1
Proposed resolution
If you create an entity via new EntityClass()
the UUID is missing, isNew()
returns the wrong info, and save() returns the a value indicating an update when in reality it is a creation. Use ::create()
instead.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#2 | 3240800-2.patch | 5.22 KB | alexpott |
Comments
Comment #2
alexpottOriginally I thought we might want a follow-up to make it harder to have this problem but given this has only occurred in 3 kernel tests perhaps it is not worth the effort.
Comment #3
andypostUsed to run all 3 tests and without patch it throws
Comment #4
larowlanCommitted fa1e288 and pushed to 9.3.x. Thanks!