Craft CMS 5 — Extending (Plugins & Modules)
Reference for extending Craft CMS 5 through plugins and modules. Covers everything from elements and services to controllers, migrations, fields, and events.
This skill is scoped to extending Craft — building plugins, modules, custom element types, field types, and backend integrations. For site/platform development (content modeling, sections, entry types, Twig templating, plugin selection), see the craft-site skill.
Companion Skills — Always Load Together
When this skill triggers, also load:
craft-php-guidelines— PHPDoc standards, section headers, naming conventions, class organization, ECS/PHPStan, verification checklist. Required for any PHP code.ddev— All commands run through DDEV. Required for running ECS, PHPStan, scaffolding, and tests.craft-garnish— When working on CP JavaScript, asset bundles, or interactive CP components. Covers Garnish's class system, UI widgets (Modal, HUD, DisclosureMenu, Select), drag system, and the Craft.* JS class pattern.craft-cloud— When the project is hosted on Craft Cloud (detect viacraft-cloud.yamlat the repo root orcraftcms/cloudincomposer.json). Required for plugin Cloud-compatibility constraints —App::isEphemeral()guards, asset-bundle CDN publishing, 15-minute queue-job cap,csrfInput()function over raw token output, and thecloud/updeploy lifecycle events.
Documentation
- Extend guide: https://craftcms.com/docs/5.x/extend/
- Class reference: https://docs.craftcms.com/api/v5/
- Generator: https://craftcms.com/docs/5.x/extend/generator.html
Use WebFetch on specific doc pages when a reference file doesn't cover enough detail.
Common Pitfalls (Cross-Cutting)
- Always use
addSelect()inbeforePrepare()— it's the Craft convention and safely additive when multiple extensions contribute columns. - Queue workers run in primary site context — use
->site('*')for cross-site queries. - Including
idingetConfig()— project config uses UIDs, never database IDs. - Business logic in models or controllers — services are where logic belongs.
- Modules need manual template root, translation, and controllerNamespace registration — nothing is automatic.
DateTimeHelperin elements/queries,Carbonin services — never mix in the same class.- Hardcoding
/adminin CP URLs —cpTriggeris configurable. UseUrlHelper::cpUrl()in PHP,cpUrl()in Twig. - Passing
$request->getBodyParams()directly tosavePluginSettings()on split-settings pages — only submitted keys persist, other settings are silently dropped. Load the full settings model first, update properties, then save.
Reference Files
Read the relevant reference file(s) for your task. Multiple files often apply together.
Task examples:
- "Build a custom element type" → read
elements.md(Architecture section first) +element-index.md+fields.md+migrations.md+cp.md - "Build a hierarchical/tree element type" → read
elements.md(Architecture: One Element Class with Native Structure) - "Add a webhook endpoint" → read
controllers.md+events.md - "Create a queue job that syncs elements" → read
queue-jobs.md+elements.md+debugging.md - "Add a settings page with form fields" → read
controllers.md+cp.md+architecture.md - "Register a custom field type" → read
fields.md+events.md - "Fix PHPStan errors" → read
quality.md - "Add a dashboard widget" → read
cp-components.md(Dashboard Widgets) +events.md(Widget Types section) - "Expose template variables for plugin users" → read
events.md(Twig Extensions section) - "Attach custom methods to entries" → read
events.md(Behaviors section) - "Build a CP utility page" → read
cp-components.md(Utility Pages) +events.md(Utilities section) - "Set up Vite for a plugin's CP assets" → read
plugin-vite.md+ loadcraft-garnishskill - "Add drag-to-reorder or interactive JS to a CP page" → load
craft-garnishskill - "Write CP JavaScript for a custom field type" → read
fields.md+ loadcraft-garnishskill - "Build a headless Craft API" → read
graphql.md+ loadcraft-siteskill forheadless.md - "Configure preview for a Next.js front-end" → load
craft-siteskill forheadless.md - "Set up Pest tests for a plugin" → read
testing.md - "Write a test for a controller action" → read
testing.md - "Configure Redis for caching and sessions" → read
config-app.md - "Set up environment variables for production" → read
config-bootstrap.md - "Find a GeneralConfig setting" → read
config-general.md - "Read a config value in plugin code (App::env, parseEnv, GeneralConfig)" → read
config-bootstrap.md+config-general.md - "Check if allowAdminChanges is enabled in plugin code" → read
config-general.md+cp.md(Read-Only Mode) - "Resolve env vars in plugin settings ($MY_API_KEY)" → read
config-bootstrap.md(App::parseEnv) - "Understand CRAFT_* env var conventions" → read
config-bootstrap.md - "Configure mail transport / SMTP" → read
config-app.md - "Set up custom URL routes" → read
config-bootstrap.md - "Configure search to find short words" → read
config-app.md - "Set up GraphQL tokens and schemas" → read
graphql.md+config-general.md - "Set up caching for a high-traffic site" → read
caching.md - "Register custom permissions for my plugin" → read
permissions.md - "Check user permissions in templates" → read
permissions.md - "Set up plugin editions / feature gating" → read
architecture.md(Plugin Editions section) - "Upgrade a plugin from Craft 4 to 5" → read
quality.md(Rector section) - "Set up CI for a Craft plugin" → read
quality.md(CI/CD Integration section) - "Create sections or fields in a migration" → read
migrations.md(Content Migrations section) - "Set up database read replicas" → read
config-app.md(Database Replicas section) - "Register a module in app.php" → read
config-app.md(Module Registration section) - "Create a custom validator" → read
architecture.md(Custom Validators section) - "Create a custom filesystem type" → read
events.md(Filesystem Types section) - "Build a custom condition rule for an element index" → read
cp-ui-patterns.md(Condition Builders) - "Build a tri-state on/inherit/off control" → read
cp-ui-patterns.md(Tri-State Inheritance Controls) - "Add tabbed settings page to a plugin" → read
cp.md(Tabbed Settings Pages) - "Show an 'overrides global' warning on a field" → read
cp-ui-patterns.md(Field Warning Parameter) - "What CSS variables does Craft CP use?" → read
cp-ui-patterns.md(Craft CSS Custom Properties) - "Set up pre-commit hooks for code quality" → read
quality.md(Pre-Commit Hooks section) - "Restrict element access by user group" → read
element-authorization.md+permissions.md - "Scope CP element index by permission" → read
element-authorization.md(Layer 3: Query Scoping) - "Add authorization events to a custom element" → read
element-authorization.md+elements.md - "Build defense-in-depth for a security plugin" → read
element-authorization.md(Defense Patterns) - "Force-logout a user from all devices" → read
sessions-and-auth.md(Plugin Patterns) - "Understand how Craft sessions work" → read
sessions-and-auth.md - "Implement password reset required" → read
sessions-and-auth.md(passwordResetRequired Gap) - "Add a column to the Users element index" → read
element-index.md(Extending Element Indexes via Events) - "Add a bulk action to an element index" → read
element-index.md(Adding a custom bulk action) - "Add an action to the per-element edit-screen menu" → read
element-index.md(Per-Element Edit-Screen Action Menu) - "Render a status pill in a table column" → read
element-index.md(Status Pills in Table Attributes) - "Add a custom sidebar source to the element index" → read
element-index.md(Adding a sidebar source) - "Build a custom field type" → read
field-types-custom.md+