1

I've got a custom content type (generated by drush) that extends ContentEntityBase and a bundle class (also generated by drush) that extends my content type. I've registered my bundle with hook_entity_bundle_info() in my_module.module, and the entity type bundle service shows my bundle belonging to my content type:

    >>> $bundleInfo = \Drupal::service('entity_type.bundle.info')
=> Drupal\Core\Entity\EntityTypeBundleInfo {#1475
     +"_serviceId": "entity_type.bundle.info",
   }

>>> $bundles = $bundleInfo->getBundleInfo('my_content_type')
=> [
     "my_content_type" => [],
     "dc_thing" => [
       "label" => Drupal\Core\StringTranslation\TranslatableMarkup {#5566},
       "class" => "Drupal\my_module\Entity\Bundle\DcThing",
     ],
   ]

But when I try creating an entity using my dc_thing bundle, Drupal complains that my content type is missing a bundle

 >>> $etm = \Drupal::service('entity_type.manager')
=> Drupal\Core\Entity\EntityTypeManager {#996
     +"_serviceId": "entity_type.manager",
   }

>>> $thing = $etm->getStorage('my_content_type')->create(['type' => 'dc_thing', 'label' => 'Particle Mang'])
Drupal\Core\Entity\EntityStorageException with message 'Missing bundle for entity type my_content_type'

All of the example code I've been able to find for bundle classes extend Node, but as far as I'm aware, the only bundles for Node in core are for Article and Basic Page, I don't see where there's a bundle defined for the Node entity type.

>>> $article = $etm->getStorage('node')->create(['type' => 'article', 'label' => 'test article'])

works just fine.

7
  • Normally you have a config entity for the bundles of a content entity and for each bundle you create a bundle config entry. In theory it should be possible to define bundles in code, but this never worked in the past and I'm not sure if this has changed with the introduction of bundle classes.
    – 4uk4
    Jul 11, 2022 at 14:52
  • Bundle classes are a new feature introduced in 9.3. drupal.org/node/3191609 Unless I'm completely misunderstanding, it really looks like this would let us get around having to use the UI to create content types. Jul 11, 2022 at 14:56
  • If you don't want to use the UI you can put them in the module /config/install folder like core is providing the node content types in the standard profile: /core/profiles/standard/config/install/node.type.article.yml
    – 4uk4
    Jul 11, 2022 at 15:04
  • After introduction of bundle classes I tried to create bundles by code. But seems that is not working. git.drupalcode.org/project/omdb_api/-/blob/1.x-dev/src/Entity/… Jul 11, 2022 at 15:10
  • @miststudent2011 Did you register your bundle in hook_entity_bundle_info()? Jul 11, 2022 at 17:13

1 Answer 1

0

My problem was in my create params and content entity annotation.

In my entity_keys annotation, I had "bundle" = "bundle",

Changing 'type' => 'dc_thing' to 'bundle' => 'dc_thing' got me unstuck. Then changing my annotation to "bundle" = "type", got things working as expected (passing my bundle as type in my create statement.

Big ups to John Pitcairn on d.o.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.