Given a custom defined entity 'Foo', how can I make a bundle programmatically?

Such as Node entity has the Article and Basic page bundles (with fields) on install.

  • 1
    On my phone so I can't provide an in depth answer- but I'm hoping at least one of the answers provided shows a way to do it without using code generators. It's important to know how this is done before using them for convenience.
    – Kevin
    Sep 10 '16 at 22:54
  • 1
    I disagree - use DrupalConsole to generate the code, then look at the generated code. It'll give you the understanding of what needs to be generated, plus how it is generated, which is what the question is all about. You'll need to go through exactly the same steps as if it were written out in full, i.e. generate a content entity, a bundle, a field. You'll also then be able to use DrupalConsole, so you'll learn more ;) Sep 11 '16 at 8:00

Bundles are configuration.

You define a config entity with the name of the bundle of the custom entity:

class FooType extends ConfigEntityBundleBase

Then you can create a new bundle:

$bundle = FooType::create([
  'id' => 'newbundle',
  'label' => 'New Bundle',
  'revision' => FALSE,

Bundles usually are not created programmatically. Most times modules put bundles in /config/install as yml files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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