The Child Entity module provides a developer-focused API and toolkit for building custom content entities that exist as children (or multi-level descendants) of a parent content entity in Drupal.

Child Entities are standalone content entities with their own routes, access control, forms, listings, and revisions—automatically nested under their parent entity's URL structure (for example, /parent/{parent}/child/{child} or /grandparent/{grandparent}/parent/{parent}/child/{child}).

How Child Entity Differs from Sub-entity & Paragraphs

While modules like Sub-entity and Paragraphs also model parent–child relationships, they solve a different architectural problem:

Aspect Child Entity (child_entity) Sub-entity (subentity) / Paragraphs
Reference Direction Bottom-up: The child entity holds a required, read-only entity_reference base field pointing up to its parent (defined via entity_keys['parent']). Top-down: The parent entity holds a multi-value reference field pointing down to its sub-entities.
Routing & UI Dedicated hierarchical pages: Each child has its own canonical, add, edit, delete, collection, and revision routes nested under the parent URL (e.g., /parent/12/child/34/edit). Inline editing: Sub-entities are typically embedded and edited inline inside the parent entity's form widget.
Scalability Unbounded children per parent: Parents are never bloated by multi-value field deltas—a single parent can have thousands of child entities without impacting parent load or save performance. Best for small sets: Loading and saving a parent loads and saves all attached sub-entity field deltas.
Lifecycle & Revisions Independent lifecycle: Child entities are created, edited, and revisioned independently while enforcing parent existence and delegating access checks to the parent. Composite lifecycle: Sub-entity revisions and saves are bound to the parent entity's save cycle.

When to use Child Entity: Choose child_entity when child records are first-class domain entities (for example, Meetings or Members under a Committee) that need their own URLs, forms, Views/listings, and independent revisions under a parent context.

Features

  • Standardized Interface & Trait: Implement parent-child relationships using ChildEntityInterface and ChildEntityTrait.
  • Base Field Helper: Automatically generates a required, read-only, revision-aware entity_reference base field pointing to the parent entity type defined in entity_keys['parent'].
  • Nested Route Providers & Multi-Level Hierarchies:
    • ChildDefaultHtmlRouteProvider and ChildAdminHtmlRouteProvider automatically prefix child entity routes (canonical, add-form, add-page, edit-form, delete-form, collection) with the parent's canonical or edit path and configure entity parameter upcasting across any depth of nesting.
    • ChildRevisionHtmlRouteProvider prefixes revision routes (version-history, revision, revision-revert-form, revision-delete-form) with ancestor paths.
    • Automatically prefixes Devel entity routes (devel, devel_load, devel_render, devel_path_alias) when the Devel module is enabled.
  • Data Integrity & Form Enforcement:
    • ChildContentEntityForm automatically populates the parent entity reference from the current route on entity creation and save.
    • hook_entity_presave() recovers missing parent references from the active route match and prevents saving orphaned child entities.
    • hook_entity_type_alter() validates that non-confirm form handlers on child entities extend ChildContentEntityForm.
  • Parent-Delegated Access Control & Permissions:
    • ChildEntityAccessControlHandler combines published-status checks with parent entity access delegation ($parent->access($operation, $account, TRUE)).
    • ChildEntityPermissions provides a dynamic permission generator for entity-level and bundle-level CRUD and revision permissions.
  • Scoped List Builder & Route Contexts:
    • ChildEntityListBuilder automatically filters entity collection tables by the active parent entity in the route.
    • ChildEntityRouteContext exposes parent entities from URL parameters to Drupal's Context API (Block visibility, Page Manager, Layout Builder).
  • Views & Views Add Button Integration:
    • ChildEntityViewsData configures Views data for child entities.
    • Includes a child_entity plugin for the Views Add Button module to render context-aware "Add Child" buttons in Views.

Requirements

  • Drupal Core: ^10.3 || ^11 || ^12
  • PHP: 8.4+
  • Contributed Modules: Entity API (drupal/entity)
  • Optional: Views Add Button (drupal/views_add_button)

Installation

composer require drupal/child_entity:^2.1
drush en child_entity

Usage Guide

1. Define Your Child Entity Class

Implement ChildEntityInterface, use ChildEntityTrait, specify the parent entity type machine name under entity_keys['parent'], and configure the child entity handlers and route providers:

<?php

declare(strict_types=1);

namespace Drupal\my_module\Entity;

use Drupal\child_entity\ChildEntityAccessControlHandler;
use Drupal\child_entity\ChildEntityListBuilder;
use Drupal\child_entity\ChildEntityTrait;
use Drupal\child_entity\ChildEntityViewsData;
use Drupal\child_entity\Entity\ChildEntityInterface;
use Drupal\child_entity\Form\ChildContentEntityForm;
use Drupal\child_entity\Routing\ChildAdminHtmlRouteProvider;
use Drupal\child_entity\Routing\ChildRevisionHtmlRouteProvider;
use Drupal\Core\Entity\Attribute\ContentEntityType;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

#[ContentEntityType(
  id: 'my_child',
  label: new TranslatableMarkup('My Child Entity'),
  entity_keys: [
    'id' => 'id',
    'revision' => 'vid',
    'label' => 'name',
    'uuid' => 'uuid',
    'published' => 'status',
    'parent' => 'my_parent',
  ],
  handlers: [
    'access' => ChildEntityAccessControlHandler::class,
    'list_builder' => ChildEntityListBuilder::class,
    'views_data' => ChildEntityViewsData::class,
    'form' => [
      'default' => ChildContentEntityForm::class,
      'add' => ChildContentEntityForm::class,
      'edit' => ChildContentEntityForm::class,
      'delete' => ContentEntityDeleteForm::class,
    ],
    'route_provider' => [
      'html' => ChildAdminHtmlRouteProvider::class,
      'revision' => ChildRevisionHtmlRouteProvider::class,
    ],
  ],
  links: [
    'canonical' => '/child/{my_child}',
    'add-form' => '/child/add',
    'edit-form' => '/child/{my_child}/edit',
    'delete-form' => '/child/{my_child}/delete',
    'collection' => '/children',
    'version-history' => '/child/{my_child}/revisions',
    'revision' => '/child/{my_child}/revisions/{my_child_revision}/view',
  ],
  base_table: 'my_child',
  revision_table: 'my_child_revision',
)]
class MyChild extends EditorialContentEntityBase implements ChildEntityInterface {

  use ChildEntityTrait;

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

    // Customize the parent field or define additional base fields here.
    return $fields;
  }

}

2. Custom Entity Forms

If your child entity uses custom add/edit form classes, extend Drupal\child_entity\Form\ChildContentEntityForm rather than ContentEntityForm directly. Standard confirmation forms (such as ContentEntityDeleteForm) remain supported for the delete operation.

3. Views Add Button Integration

  1. Create a View listing your child entities.
  2. Add a Global: Views Add Button header/footer/field.
  3. Configure the button settings:
    • Plugin Type: Select Child Entity.
    • Entity Type / Bundle: Select your child entity type and bundle.
    • Context: Pass the parent entity ID (e.g., via a Contextual Filter token such as {{ raw_arguments.my_parent }}).

Upgrading from 2.0.x to 2.1.0

  • PHP 8.4+ & Strict Types: Ensure custom classes implementing ChildEntityInterface match the updated method signatures (getParentEntity(): ?EntityInterface, getParentId(): int|string|null, setParentId(int|string $id): static, and setParentEntity(EntityInterface $parent): static).
  • Route Providers: ChildContentEntityHtmlRouteProvider is deprecated in 2.1.0 and will be removed in 3.0.0. Replace it with ChildAdminHtmlRouteProvider (for admin-themed forms) or ChildDefaultHtmlRouteProvider (for frontend forms).
Supporting organizations: 
Maintain v2

Project information

Releases