drupal-console icon indicating copy to clipboard operation
drupal-console copied to clipboard

[generate:entity:config] SchemaIncompleteException on Drupal 9 when using generated config entities

Open cboyden opened this issue 3 years ago • 1 comments

[generate:entity:config] SchemaIncompleteException on Drupal 9 when using generated config entities

Problem/Motivation

Drupal 9 expects the annotation of a config entity to include a config_export key. See Change Record where this was initially introduced.

If you use the generate:entity:config command, the annotation in the generated class file doesn't include the config_export key. This causes a SchemaIncompleteException when you try to add a configuration entity in the site admin UI.

How to reproduce

  • Drupal 9.0.7
  • Drupal Console version 1.9.5
  • Drupal Console Launcher 1.9.4
  1. Create a Drupal site using a Composer template.
  2. Add Drupal Console using composer.
  3. Initialize the site and install Drupal.
  4. Create a sample module using generate:module and accepting all the defaults.
  5. Create a config entity for the sample module using generate:entity:config and accepting all the defaults.
  6. Enable the sample module and rebuild caches.
  7. In the site's admin UI, visit /admin/structure/default_entity (or the base path plus config entity name that you chose)
  8. Click the Add button.
  9. Fill in text for the Label field and click Save.
  10. The error message "The website encountered an unexpected error. Please try again later." is displayed.
  11. Visit /admin/reports/dblog and follow the link for the latest PHP error.
  12. Note the error message: Drupal\Core\Config\Schema\SchemaIncompleteException: Entity type 'Drupal\Core\Config\Entity\ConfigEntityType' is missing 'config_export' definition in its annotation in Drupal\Core\Config\Entity\ConfigEntityBase->toArray() (line 251 of /web/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php).

Solution

Add the following to the annotation in /modules/modulename/src/Entity/DefaultEntity.php:

 *   config_export = {
 *     "id",
 *     "label"
 *   },

The file template at /templates/module/src/Entity/entity.twig.php can be updated to include the config_export key in the annotation.

cboyden avatar Nov 04 '20 23:11 cboyden

Hi, the solution works fine. Thank you!

finex avatar Sep 24 '21 07:09 finex

Thank you for your contribution

LOBsTerr avatar Sep 16 '22 15:09 LOBsTerr