Drupal Console — CLI to generate boilerplate code

Rahul Kumar
2 min readOct 8, 2020

Drupal Console is a command-line interface (CLI) tool to generate boilerplate code. It’s a powerful tool for a developer who is writing code for Drupal.

To install CLI there are two options. Either you can install globally or you can install within your project in the vendor directory. I prefer to install within the vendor because it’s easy to maintain versions wrt to Drupal version(8 or 9).

To install globally in mac or Linux-

curl https://drupalconsole.com/installer -L -o drupal.phar
mv drupal.phar /usr/local/bin/drupal
chmod +x /usr/local/bin/drupal

If you don’t have curl-

php -r “readfile(‘https://drupalconsole.com/installer');" > drupal.phar
mv drupal.phar /usr/local/bin/drupal
chmod +x /usr/local/bin/drupal

Run Drupal Console using the Launcher
drupal

To install within vendor of your project, go to the project root folder and run-

composer require drupal/console

It will download the drupal console according to your drupal version. Now just start using it.

If you have installed it globally, then just type

drupal

in your root folder and if you have installed in the vendor folder of your project then type

vendor/bin/drupal

We’ll use the last one(vendor/bin/drupal) in our example.

Now let's start creating a form in a custom module using the drupal console. Run command-

vendor/bin/drupal generate:form

It will ask for more information with default options. Choose accordingly and hit enter.

Generate Form Using Drupal Console
Generate Form Using Drupal Console

Once you are done with all the options, it will generate all the related files within that module. Likewise, the above module name is custom_page and the rest files are generated and updated by the drupal console.

And here we are -

Custom Form Using Drupal Console

A form is generated by a drupal console without writing any single line of code.

This is a very powerful tool and very thanks 🙏 to the contributors. https://drupalconsole.com/contributors

Using this tool as a practice can help us in creating a basic skeleton of your custom code with proper coding standards and structure as well as it will be helpful in cost and time reduction.

Keep Learning… Keep Sharing…
🙏

--

--

Rahul Kumar

Programmer | Freelancer | Thinker | Open Source | Tech Mantra