How to use Craft Generator to scaffold Craft CMS plugins, modules, and other components.
Craft Generator is a utility for Craft CMS that scaffolds Craft CMS plugins, modules, and other components. In addition to creating new plugins or modules, you can also use Craft Generator to add new functionality (via new controllers, models, widget-types, Twig extensions) to an existing plugin or module.
Like to learn while watching? Watch the video version of the article!
Craft Generator is a Composer package we need to require as part of our Craft CMS project.
Here are some important notes on installing Generator:
composer create-project craftcms/craft
, then you do not need to require Craft Generator in the project. For all projects already created, you will need to install it. Here’s how to install Craft Generator:
composer require craftcms/generator --dev
If we’re using the official DDEV support for Craft CMS, then you’d do:
ddev composer require craftcms/generator --dev
We pass in the --dev
option, so Composer sets the package as the dev dependency. This means that the Generator package won’t be installed when you install your dependencies in production via composer install —no-dev.
To test that Generator is working and to see all of the commands available with Generator, run (for DDEV):
php craft make
Again, if you’re using DDEV for Craft (highly recommended, see our learning materials on the topic), then you should run the craft
command right on the container:
ddev craft make
Running craft make
is how we access all of the subcommands in Craft Generator.
The most common entry point for using Craft Generator is scaffolding a Craft plugin. Here’s how we create one.
The first command is to start the generator wizard to help Generator know the details about your plugin.
php craft make plugin
If you’re new to Craft plugin development, append the --with-docblocks
option to get DocBlock comments to the generated code that help explain the different classes, methods, etc., in the plugin.
php craft make plugin --with-docblocks
Generator will step you through an interactive setup process, where you input your desired naming and settings for the plugin.
plugins
directory at the root of your project. Plugin
name, but you should customize it to something specific to your plugin using PascalCase. E.g. MagicButtonyes
; otherwise, accept the default of no
. If unsure, it won’t hurt to indicate yes and then leave the settings page blank.Generator will now create your plugin scaffold using the settings you inputted. If Generator throws an error for some reason, you will want to clean up your plugins
directory before retrying. Generator doesn’t clean up after itself after a failed session.
Your plugin is created and ready to be required and installed. Craft Generator made this easier for you by adding the plugin path as a new repository in the project’s composer.json
file.
"repositories": [{
"type": "path",
"url": "plugins/magic-button"
}]
To install the plugin, we need to require it using the dev-main
version, and then install it.
ddev composer require ryan-irelan/magic-button:dev-main
ddev craft plugin/install magic-button
And now you’re ready to work on your plugin! The files are symlinked from the plugins/magic-button
, so any changes you make to the files in that location will be run as if they are in the vendor
directory.
If you need to create custom functionality for your Craft CMS project and don’t need the extra functionality offered via Craft plugins, then a module is the way to go.
The first command is to start the generator wizard to help Generator know the details about your module.
ddev craft make module
Generator will step you through an interactive setup process, where you input your desired naming and settings for the plugin.
site-logic
.modules/
in the project root. You might get an error if you already have a module (like the default Module.php file) in the modules directory. Remove the default Module.php
file (if you’re not using it!), and then continue.Now Generator will create the module file and update the config/app.php
file to load the module class and bootstrap it.
Important note: The Craft starter project no longer includes the default modules/Module.php
module since Generator will better set up a new module for you.
After you’ve already created a new plugin or module, you might need an additional component, like a widget type in your module.
Use one of Generator’s commands, and pass in the --module
or --plugin
option, passing in the module ID or plugin handle.
ddev craft make widget-type --module=site-logic
LatestInfo
.modules/widgets
is good enough for me.In modules/widgets
, there is now a LatestInfo.php
class file with some boilerplate code to get a module up and running. Generator also added the event to register the new widget type to the modules/Module.php
file.
If you log into the project’s control panel, you should be able to add the widget to the control panel dashboard.
Now that you have your scaffolded plugin or module, you are ready to start the work of writing the code that will make do what you wish! This is the fun part where you start to see your idea come to life.
Your implementation details will be specific to your needs, so we can’t write a tutorial covering every single scenario out there. However, here are some helpful learning materials here on CraftQuest for learning how to extend Craft CMS.
Once you get rolling with plugin and module devlopment, you might want to improve your local development environment. Here are some resources that will aid you in just that!
Do you have a resource that you think should be on this page? Email and let us know.