agentleFS
AnyOrgNameAnyOrgName/php-skills/skills/drupal/entity-api/SKILL.md

entity-api

Skill4 starsChanged 4 months agoMITView on GitHub

Model data with the Drupal Entity API — define content and configuration entity types, add base and configurable fields, and read or write through entity storage. Use when the user needs a Drupal configuration entity to store admin-defined settings that ship as exportable config; the user wants to define a custom Drupal content entity type to store repeatable, fieldable data; the user wants to load, query, save, or delete Drupal entities through the entity type manager and storage. Keywords: drupal, entity, entity-api, content-entity, config-entity, fields, storage, php81.

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Do not create and query a raw custom database table when a content entity would model the same fieldable data.

    line 64
  2. Very specific:

    Do not define an entity type for one flat setting.

    line 65
  3. Very specific:

    Create the entity class under src/Entity/ extending ContentEntityBase with a #[ContentEntityType] attribute defining id, base_table, …

    line 73
  4. Very specific:

    Use $storage->create([...])->save() to write and $storage->getQuery()->accessCheck(TRUE)->execute() to read.

    line 75
  5. Distinctive:

    Not applicable — no I/O, auth, or untrusted input.

    line 106

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- generated: do not edit — source: knowledge/atoms/drupal/entity-api/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->

Drupal Entity API

When to use

  • The user needs a Drupal configuration entity to store admin-defined settings that ship as exportable config.
  • The user wants to define a custom Drupal content entity type to store repeatable, fieldable data.
  • The user wants to load, query, save, or delete Drupal entities through the entity type manager and storage.

When NOT to use

  • Do not create and query a raw custom database table when a content entity would model the same fieldable data.
  • Do not define an entity type for one flat setting; use simple configuration (config.factory) instead.

Core guidance

  • Choose content entities for data, config entities for settings — Use a content entity when instances are user-created data (many rows, fieldable); use a config entity when instances are site-builder settings that must deploy through config sync.

Quick recipe

  1. Create the entity class under src/Entity/ extending ContentEntityBase with a #[ContentEntityType] attribute defining id, base_table, entity_keys, and handlers. (verify: The entity type appears in drush entity:types.)
  2. Implement baseFieldDefinitions() returning each field via BaseFieldDefinition::create(...), then install the entity schema. (verify: The base table and its columns exist after install.)
  3. Use $storage->create([...])->save() to write and $storage->getQuery()->accessCheck(TRUE)->execute() to read. (verify: Created entities are found by the access-checked query.)

Best practices

  • Define the entity type via a #[ContentEntityType] attribute (or @ContentEntityType annotation) with id, base_table, entity_keys, and handlers for list builder, form, and access
  • Declare fields common to every bundle in baseFieldDefinitions() using BaseFieldDefinition::create(...); reserve configurable fields for per-bundle needs added through the UI or config
  • On every entity query call ->accessCheck(TRUE) for user-facing listings and ->accessCheck(FALSE) only for deliberate system queries; never leave it unset
  • Load and query entities via $entityTypeManager->getStorage('my_entity'), using an injected EntityTypeManagerInterface, not by writing SQL against the base table

Anti-patterns

Anti-patternWhy it failsDo instead
Using a content entity for deployable settingsContent entities are data in the database and are not exported by config sync, so the settings never travel between environments.Model deployable settings as a config entity or simple configuration so config sync carries them.
Instantiating entities with new instead of storageDirectly instantiated entities miss default values, ids, and storage wiring, so saving them corrupts data.Create entities via $storage->create([...]) and persist with ->save().
Writing SQL against the entity base tableRaw SQL bypasses hooks, access control, translation, and cache invalidation, producing stale caches and security holes.Use the entity query and storage APIs through the injected entity type manager instead.

See references/anti-patterns.md.

Common mistakes

  1. Config entity fails validation without a schema — A config entity throws schema errors or fails config export when its config/schema/*.schema.yml is missing. Fix: Add a config/schema/MODULE.schema.yml describing every property of the config entity type.
  2. Entity query throws without accessCheck — An entity query without accessCheck() throws a QueryException on Drupal 10+. Fix: Call ->accessCheck(TRUE) or an explicit ->accessCheck(FALSE) on every entity query.
  3. Adding a base field on an installed entity does nothing — Adding a new base field to an already-installed entity type has no effect until the schema is updated. Fix: Install the field in a hook_update_N() via \Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition(...).

Version notes

  • PHP attribute entity definitions arrived in Drupal 10.2 (PHP >=8.1) — From Drupal 10.2 entity types can be declared with PHP 8 attributes (#[ContentEntityType]); earlier versions require the doctrine-style @ContentEntityType annotation.

Security

  • Not applicable — no I/O, auth, or untrusted input.

Testing

  • Activation and decision behavior are verified in eval/scenarios/drupal/entity-api.yaml.

Additional resources

  • references/anti-patterns.md
  • references/core.md
  • references/examples.md
  • references/pitfalls.md
  • references/recipes.md
  • references/version-differences.md

Knowledge graph

Related: drupal/module-development

<!-- graph-fragment: graph/fragments/drupal/entity-api.yaml -->

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.