10. Categorizing different entities of the same entity type
Drupal provides a mechanism to distinguish content entities of the same type and attach different behavior to the entities based on this distinction. In the case of event entities, for example, it allows events to have different behavior based on the type of event they are. The nomenclature is that entity types can have bundles where each entity of that entity type belongs to a certain bundle.
Generally a configuration entity type is used to provide the bundles for a content entity type. In this case each Event type entity will be a bundle for the Event entity type.
10.1. Add the bundle field
Section titled “10.1. Add the bundle field”-
Delete the existing event(s)
Visit
/admin/content/events/manage/2/deleteand press Delete.Adding a bundle field cannot be done when there are existing entities.
-
Add the following to the
entity_keyssection of the attributes insrc/Entity/Event.php: -
Add the following to the attributes in
src/Entity/Event.php: -
Replace the
add-formlink in the attributes insrc/Entity/Event.phpwith: -
Add the following to the
linkssection of the attributes insrc/Entity/Event.phpwith: -
Add the following to the attributes in
src/Entity/EventType.php:
Like for the id and uuid fields, the field definition for the type field
is automatically generated by ContentEntityBase::baseFieldDefinitions().
10.2. Install the bundle field
Section titled “10.2. Install the bundle field”-
Run
drush entity:updates -
Note that the
typecolumn has been added to the{event}table. -
Visit
/admin/structure/event-types/addand add a Conference event type -
Visit
/admin/content/events/addNote that the event types are displayed as options.
-
Create an event