Code generators in Drupal are great as a productivity tool. If you need to create a module, you could easily run a few commands and have a module generated. Then if you need to create a custom block, you could run another command which will generate all the boilerplate code and add the block into a module.
If you want to create a new event subscriber, form, service, etc… There’s always a bit of boilerplate code required to get things going. For example, making sure you extend the right class and inject the correct services. A code generator makes this process quick and easy.
Most of the popular frameworks, Laravel, Symfony, Rails just to name a few, utilize code generators which create scaffolding code.
In this tutorial, you’ll learn three ways you can generate code in Drupal 8 using Drupal Console, Drush and Module Builder.
Drupal Console
Drupal Console is a command line tool (CLI) tool for Drupal 8.
It’s used to interact with a Drupal site using a command line interface. You can use Drupal Console to rebuild site cache, import configuration changes and generate code, this is what we’re interested in.
Check out this list of commands.
It’s similar to Drush, which is another CLI tool, but when it was first released the biggest benefit was its ability to generate code (Drush can now generate code as of version 9).
To see all the available generate commands just run:
./vendor/bin/drupal generate or drupal generate
Drupal Console version 1.9.1 Available commands for the "generate" namespace: generate:ajax:command (gac) Generate & Register a custom ajax command generate:authentication:provider (gap) Generate an Authentication Provider generate:breakpoint (gb) Generate breakpoint generate:cache:context (gcc) Generate a cache context ...
Generate a Module using Drupal Console
Now let’s go ahead and generate a module using Drupal Console. When you’re working on a Drupal site you’re always creating custom modules, so let’s create one.
Just run:
drupal generate:module
Then fill out the prompts and you’re good to go.
Generate a Block using Drupal Console
In the last section, we created a module using Drupal Console. Now let’s look at generating a block in the module.
Run:
drupal generate:plugin:block
In the first prompt, you’ll be asked which module you want to add the block into. Just start typing in the name of the module and it’ll autocomplete on the name.
Enter the module name [webform_access]: > dc_demo
The next three prompts are important: plugin class name, plugin label and plugin id.
Enter the plugin class name [DefaultBlock]: > Enter the plugin label [Default block]: > Enter the plugin id [default_block]: >
So that’s how you use Drupal Console to create a module and implement a custom block in the module.
Drush
Drush was the original CLI tool for Drupal. In my opinion, it was a game-changer for Drupal.
You’d use Drush to rebuild the site cache, install or uninstall a module, install a full site and so much more. Drush never had the ability to generate code until version 9 (correct me if I’m wrong about this). It uses the Drupal Code Generator project to generate the code. Drush 9 integrates with the project so you won’t have to set anything up.
To see all the commands, just run the following:
drush generate
Drush generate 9.7.1 Run `drush generate [command]` and answer a few questions in order to write starter code to your project. Available commands: _global: composer (composer.json) Generates a composer.json file controller Generates a controller field Generates a field ...
Generate a Module using Drush
Similar to what we did in the previous section let’s generate a module.
Run:
drush generate module-standard
Same as Drupal Console, you get prompted to enter in things such as the module name, machine name, etc…
Module name: ➤ Drush demo Module machine name [drush_demo]: ➤ Module description [The description.]: ➤
The difference between creating a module using Drush or Drupal Console is that when you use Drush you get asked if you want to create a libraries.yml, permissions.yml, block, controller, etc…
Would you like to create install file? [Yes]: ➤ Would you like to create libraries.yml file? [Yes]: ➤ no Would you like to create permissions.yml file? [Yes]: ➤ no Would you like to create event subscriber? [Yes]: ➤ no Would you like to create block plugin? [Yes]: ➤ no Would you like to create a controller? [Yes]: ➤ no Would you like to create settings form? [Yes]: ➤ no
With Drupal Console, you can’t create other plugins while creating the module. First, create the module then add other components into the module such as a form or block.
Generate a Form using Drush
Let’s now use Drush to generate a form which will be added to the module we just created.
Run the following:
drush generate form-simple
This will create a form class as well as a route for the form.
Similar to Drupal Console, the first prompt is to know which module you want to add the form into. Just note, the module needs to be installed for it to be found by Drush. So if you just created a module make sure you install it first before generating a form.
Module machine name: ➤ drush_demo
Once complete you should have a form in src/Form
.
Module Builder
So far we’ve looked at using CLI tools to generate code; Drupal Console and Drush. Now let’s look at another way you can generate scaffolding code and that’s using Module Builder.
Module Builder is an actual module which needs to be downloaded and installed into your Drupal site.
Install Module Builder
To get started, run the following Composer command:
composer require drupal/module_builder
NOTE: This module should NEVER be installed on a production site. Only use it locally.
1. Go to Extend, find “Module Builder” and install it.
2. Go to Configuration, then click on Module Builder in the Development section.
3. When you first install the module, it’ll need to analyse your Drupal site and collect information about components and hooks.
4. Click on the Analyse code tab, then “Perform code analysis”.
5. After the code analysis is complete, you can see the results.
Generate Module using Module Builder
Now that we have everything set up, let’s go ahead and create a module.
1. Go to Configuration, Module Builder and click on “Add module”.
2. On the “Add module” screen, enter in a readable name for your module and define its dependencies if required. Then click on “Save basic information”.
3. Now you should see extra tabs such as Hooks, Plugins, “Entity types”, etc…
Generating Plugins and Hooks
The UI for Module Builder can be a little intimidating at first, however, after you spend a bit of time using it, it starts to make sense. So I recommend that you spend time playing around with the different tabs and getting accustomed to the interface.
Hooks
Click on the Hooks tab to see all the available hooks in your Drupal site.
If you want to see hooks for a particular module, just add the module name to “Filter by hook name”.
Plugins
In Drupal 8 a lot of its components are Plugins. Click on the Plugins tab to generate them.
If you want to learn more about Drupal’s plugin system click here.
Generate Block using Module Builder
Let’s now create a block using Module Builder.
1. Click on Plugins, then “Add another plugins (annotated class) item”.
2. Select Block from the Plugin type drop-down and add a Plugin ID. If you want to inject a service you can do so by searching for it using the “Injected services” autocomplete.
3. Then scroll down and click on Save.
Generate Module
At this point, we are yet to generate the actual module. To do so, click on the “Generate code” tab.
From here you can review the code which will be generated. If you’re happy with everything click on “Write all files”.
Now you should see the module in your Drupal codebase.
Summary
I hope now you have a better understanding of how to generate code in Drupal.
Which method should you use?
I prefer to use Drush or Drupal Console because more often than not they are already installed. Especially if you manage Drupal using drupal-project composer template.
Module Builder is a great module but I’d prefer to set up Module Builder on a sandbox site, generate the required code and manually copy it into my modules.
Which method do you prefer? Let me know in the comments below.
I like Drush mainly because it’s what I am accustomed to. Now that Drush generates code as well is there any benefit to using Drupal Console over Drush?
Drush is more actively supported than Drupal Console. Drupal Console was not actively maintained, but people have stepped up and started to tag new releases. @see https://github.com/hechoendrupal/drupal-console/releases
I have found that Drush is really good at generating simple-to-use and understand boilerplate code.
I am getting “The Drupal Code Builder library could not be found. Check README.txt for instructions.” error while installing Module Builder.
Did you check the README.txt file?