Problem/Motivation

Creating a SwiperFormatter config entity programmatically without providing all properties (e.g. swiper_options) causes a fatal error (HTTP 502) when loading the edit form. This is because the entity class does not define default values for its properties, and the form class expects them to be populated.
Currently, SwiperFormatterForm::prepareEntity() handles this for the add form by copying options from the default entity, but this only covers the form context and not programmatic creation (e.g. from code, Drush, or migrations, tests).
There is no preCreate() implementation on the entity class to ensure sensible defaults are set whenever create() is called, regardless of context.

Steps to reproduce

$entity = \Drupal::entityTypeManager()
  ->getStorage('swiper_formatter')
  ->create([
    'id' => 'test_swiper',
    'label' => 'Test Swiper',
    'description' => 'Test Swiper Description',
  ]);
$entity->save();

// Visit /admin/config/content/swiper-formatter/test_swiper → 502 fatal error.

Proposed resolution

Add a preCreate() method to SwiperFormatter that loads the default entity and uses its swiper_options and status as defaults for newly created entities:
This covers both programmatic creation and the add form, replacing the form-level logic currently in prepareEntity().

Remaining tasks

  • Implement preCreate() on SwiperFormatter
  • Remove prepareEntity() from SwiperFormatterForm (no longer needed)

This was discovered while writing tests for #3573757, where programmatic entity creation without full properties caused a fatal error. Resolving this is a prerequisite for completing the test coverage in that issue.

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: bbu23 » Unassigned
Status: Active » Needs review

  • bbu23 committed 0bc34723 on 2.1.x
    test: #3574694 Add tests for default values in SwiperFormatter entity...

  • bbu23 committed b12e95bb on 2.1.x
    fix: #3574694 Ensure default values are always set for new...
bbu23’s picture

Status: Needs review » Fixed

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.

bbu23’s picture

Will be available in 2.1.0

Status: Fixed » Closed (fixed)

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