1

I am working on creating a base list entity type I will call CustomList for the sake of this question. I pair it up with CustomListItem so that I have an entity type to define the list and another to define the items in the list.

I intend to create bundles underneath these to build a custom cart, a saved item list, and an item group list.

So far everything I have found in Drupal 8 either creates Node Bundles (which I don't want) or a bundle of config yaml files to define the bundle (in this case ECK did this for me). But nothing useful on how to create a bundle under a specific entity type.

Would I just declare a new entity type and do something in the annotations to make it a bundle? If so I am unsure as to what annotation to use or how to use it.

Thanks, Patrick

2

Yes, you declare a config entity type to store the bundle definitions. For example that for Custom Block Types (bundle of block_content):

Drupal\block_content\Entity\BlockContentType

namespace Drupal\block_content\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\block_content\BlockContentTypeInterface;

/**
 * Defines the custom block type entity.
 *
 * @ConfigEntityType(
 *   id = "block_content_type",
 *   label = @Translation("Custom block type"),
 *   handlers = {
 *     "form" = {
 *       "default" = "Drupal\block_content\BlockContentTypeForm",
 *       "add" = "Drupal\block_content\BlockContentTypeForm",
 *       "edit" = "Drupal\block_content\BlockContentTypeForm",
 *       "delete" = "Drupal\block_content\Form\BlockContentTypeDeleteForm"
 *     },
 *     "list_builder" = "Drupal\block_content\BlockContentTypeListBuilder"
 *   },
 *   admin_permission = "administer blocks",
 *   config_prefix = "type",
 *   bundle_of = "block_content",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label"
 *   },
 *   links = {
 *     "delete-form" = "/admin/structure/block/block-content/manage/{block_content_type}/delete",
 *     "edit-form" = "/admin/structure/block/block-content/manage/{block_content_type}",
 *     "collection" = "/admin/structure/block/block-content/types",
 *   },
 *     ...
 * )
 */
class BlockContentType extends ConfigEntityBundleBase implements BlockContentTypeInterface {

Which is then referenced in the content entity type:

Drupal\block_content\Entity\BlockContent

*   bundle_entity_type = "block_content_type",

If you use Drupal Console to generate a custom content entity type and you say yes to the question if you want bundles, this config entity type is set up for you automatically.

  • I had created a content entity and said yes to allowing bundles. Just didn't see a way to actually create the bundles. But this looks like what I was looking for. – Patrick Jun 15 '17 at 20:24
  • I will test and then mark this as the answer once I see it doing what I want to do it. – Patrick Jun 15 '17 at 20:25
  • OK, when you have the config entity then you can add bundles in ui (see the form links in the annotations) or create a bundle config entity like any other entity in code. – 4k4 Jun 15 '17 at 20:28
  • But the UI does not seem to have a way to export the changes to code. Unless I am just missing something. I need them exported as classes to modify how they work. – Patrick Jun 15 '17 at 20:57
  • You can export the bundles to yaml files. Staying with the example of custom blocks, this is the block type that the standard profile installs: /core/profiles/standard/config/install/block_content.type.basic.yml – 4k4 Jun 15 '17 at 21:06

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.