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

CommentFileSizeAuthor
#2 3240800-2.patch5.22 KBalexpott
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
5.22 KB

Originally 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.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Used to run all 3 tests and without patch it throws

  2x: mb_strtolower(): Passing null to parameter #1 ($string) of type string is deprecated
    2x in LocaleConfigurableLanguageManagerTest::testGetLanguages from Drupal\Tests\locale\Kernel

  3x: mb_strtolower(): Passing null to parameter #1 ($string) of type string is deprecated
    3x in ComplexWorkflowTypeTest::testLoadMultipleByType from Drupal\Tests\workflows\Kernel

  8x: mb_strtolower(): Passing null to parameter #1 ($string) of type string is deprecated
    6x in RequiredStatesTest::testChangeRequiredStateAPI from Drupal\Tests\workflows\Kernel
    1x in RequiredStatesTest::testGetRequiredStates from Drupal\Tests\workflows\Kernel
    1x in RequiredStatesTest::testDeleteRequiredStateAPI from Drupal\Tests\workflows\Kernel

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed fa1e288 and pushed to 9.3.x. Thanks!

  • larowlan committed fa1e288 on 9.3.x
    Issue #3240800 by alexpott: Creating an configuration entity via new...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.