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.
In this repository119 other files it gives its agents.
- strategy-bitrix-d7-incrementalSkill
- strategy-explicit-over-magicSkill
- strategy-framework-conventionsSkill
- strategy-legacy-safe-editsSkill
- strategy-minimal-diffSkill
- strategy-no-untested-refactorSkill
- strategy-preserve-public-apiSkill
- strategy-readonly-immutabilitySkill
- strategy-security-firstSkill
- strategy-test-drivenSkill
- openapiSkill
- paginationSkill
- rest-designSkill
- versioningSkill
- cqrsSkill
- dependency-inversionSkill
- hexagonalSkill
- repository-patternSkill
- service-layerSkill
- bitrixSkill
- findings-reportSkill
- laravelSkill
- performanceSkill
- phpSkill
- project-analysisSkill
- refactoringSkill
- roi-prioritizationSkill
- securitySkill
- stack-detectionSkill
- cache-layersSkill
- component-engineSkill
- d7-eventsSkill
- d7-loaderSkill
- d7-orm-queriesSkill
- iblock-elementsSkill
- kernel-main-moduleSkill
- localizationSkill
- controllersSkill
- ormSkill
- validationSkill
- controllersSkill
- query-builderSkill
- routingSkill
- autoload-psr4Skill
- dependency-managementSkill
- scriptsSkill
- connection-poolingSkill
- mysql-indexingSkill
- redisSkill
- transactionsSkill
- ci-github-actionsSkill
- deployment-strategiesSkill
- docker-phpSkill
- env-configSkill
- associationsSkill
- entities-mappingSkill
- orm-basicsSkill
- module-developmentSkill
- routing-controllersSkill
- formsSkill
- The first 60 shown.
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
- Distinctive: line 64
Do not create and query a raw custom database table when a content entity would model the same fieldable data.
- Very specific: line 65
Do not define an entity type for one flat setting.
- Very specific: line 73
Create the entity class under src/Entity/ extending ContentEntityBase with a #[ContentEntityType] attribute defining id, base_table, …
- Very specific: line 75
Use $storage->create([...])->save() to write and $storage->getQuery()->accessCheck(TRUE)->execute() to read.
- Distinctive: line 106
Not applicable — no I/O, auth, or untrusted input.
≈ “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.
Files
<!-- 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
- Create the entity class under
src/Entity/extendingContentEntityBasewith a#[ContentEntityType]attribute defining id, base_table, entity_keys, and handlers. (verify: The entity type appears indrush entity:types.) - Implement
baseFieldDefinitions()returning each field viaBaseFieldDefinition::create(...), then install the entity schema. (verify: The base table and its columns exist after install.) - 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@ContentEntityTypeannotation) withid,base_table,entity_keys, andhandlersfor list builder, form, and access - Declare fields common to every bundle in
baseFieldDefinitions()usingBaseFieldDefinition::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 injectedEntityTypeManagerInterface, not by writing SQL against the base table
Anti-patterns
| Anti-pattern | Why it fails | Do instead |
| Using a content entity for deployable settings | Content 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 storage | Directly 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 table | Raw 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
- Config entity fails validation without a schema — A config entity throws schema errors or fails config export when its
config/schema/*.schema.ymlis missing. Fix: Add aconfig/schema/MODULE.schema.ymldescribing every property of the config entity type. - 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. - 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@ContentEntityTypeannotation.
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 -->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
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.

