Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: Facets 3.0.6 Minor update available for module facets (3.0.6). Release: Facets 3.0.5 Minor update available for module facets (3.0.5). Release: Flag 5.1.0 Minor update available for module flag (5.1.0). Release: Random Name 1.2.3 Minor update available for module random_name_chooser (1.2.3). Release: Text with summary 1.0.0 Initial release available for module text_with_summary (1.0.0)! Release: Ágora Transparency Theme 1.0.7 Minor update available for theme agora_theme (1.0.7). Release: Automated Revision Log 1.1.3 Minor update available for module ai_revision_log (1.1.3). Release: Views HTTP Client 1.0.4 Minor update available for module views_http_client (1.0.4). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Module Revived: ЮKassa (YooMoney) для Drupal v7 2.6.1 Module yoomoney_api updated after 7 months of inactivity (2.6.1).

Child Entity

5 sites Security covered Drupal 8–11
View on drupal.org

This module allows you to create entities that are dependent on a parent entity, meaning they can only exist in relation to a specific parent. For example, a "room" entity might always belong to a "house" entity. It provides traits and interfaces to easily implement this parent-child relationship in your custom entities.

This module helps you to create Entities which creating them depends on a parent entity.

Consider a Room in a House. The Room can only every be in one house. In this example Room is child and House is parent entity.

In order to use this module you have to:

  • Implement ChildEntityInterface or extend it
  • Use Child Entity Handlers, either directly in the Entity definition or extending them.
  • Define parent key in entity_keys section of the child entity annotation and use ChildEntityTrait
  • Add childBaseFieldDefinitions

This works in the same way as core's EntityOwnerTrait.

/**
 ************ other configuration ************
 *   handlers = {
 *     "list_builder" = "Drupal\child_entity\ChildEntityListBuilder",
 *     "views_data" = "Drupal\child_entity\ChildEntityViewsData",
 *     "form" = {
 *       "default" = "Drupal\child_entity\Form\ChildEntityForm",
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\child_entity\Routing\ChildEntityHtmlRouteProvider",
 *     },
 *     "access" = "Drupal\child_entity\ChildEntityAccessControlHandler",
 *   entity_keys = {
 *     "parent" = "house",
 *   },
 ************ other configuration ************
 */
class Room extends ContentEntityBase implements ChildEntityInterface {

  use ChildEntityTrait;

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields += static::childBaseFieldDefinitions($entity_type);

    return $fields;
  }
}

Revision-able Entities and urlRouteParameters()

If your entity type also has revisions you may be overriding the urlRouteParameters() but you need to specify to use the one from the trait:

  use ChildEntityTrait {
    urlRouteParameters as childEntityUrlRouteParameters;
  }

  /**
   * {@inheritdoc}
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function urlRouteParameters($rel) {
    $uri_route_parameters = $this->childEntityUrlRouteParameters($rel);

    if ($rel === 'revision_revert' && $this instanceof RevisionableInterface) {
      $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
    }
    elseif ($rel === 'revision_delete' && $this instanceof RevisionableInterface) {
      $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
    }

    return $uri_route_parameters;
  }

Version 1

Version 1 of this module implemented Base classes that can be extended however this has limitations and version 2 uses Traits which allows the following:

class ChildNode extends Node implements ChildEntityInterface {

  use ChildEntityTrait;
}

Depends on

Dependencies of the latest stable release

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
Jan 2026
Latest release
6 months ago
Releases (12 mo)
2 ▲ from 0
Maintenance
Slowing

Releases

Version Type Core Release date
2.1.x-dev Dev 10–11 Feb 8, 2026
2.0.0 Stable 8–11 Jan 25, 2026