Problem/Motivation

The SwiperFormatter entity's $description property is typed as string, which does not accept null. However, the description field is optional in the admin UI form. This creates an inconsistency:

  • Creating an entity via the UI without a description works fine (the form submits an empty string)
  • Creating an entity programmatically without setting description fails with an error: TypeError: Cannot assign null to property Drupal\swiper_formatter\Entity\SwiperFormatter::$description of type string

Steps to reproduce

$entity = \Drupal::entityTypeManager()
  ->getStorage('swiper_formatter')
  ->create([
    'id' => 'test_swiper',
    'label' => 'Test Swiper',
  ]);
$entity->save();
// TypeError: Cannot assign null to property ... ::$description of type string

Proposed resolution

Initialize the $description property with an empty string default value on the entity class.
This makes the programmatic behavior consistent with the UI - description remains optional in both contexts, and the property type stays strict without needing to introduce nullability.

Remaining tasks

  • Update the $description property default on SwiperFormatter
  • Add a test to verify entity creation without a description (maybe?)
  • Update existing tests to remove the descriptions that were added only because of the TypeError
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

bbu23 created an issue. See original summary.

bbu23’s picture

Assigned: Unassigned » bbu23

  • bbu23 committed cd83454c on 2.1.x
    test: #3574816 Add test for entity creation without description
    

  • bbu23 committed 20ead7f9 on 2.1.x
    refactor: #3574816 Set default empty string for SwiperFormatter...
bbu23’s picture

Assigned: bbu23 » Unassigned
Status: Active » Fixed

Will be available in 2.1.0

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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