Common Commands in Drupal

Guide to Common Commands in Drupal: Drush, Drupal Console, and Composer

Drupal is one of the most powerful open-source content management systems (CMS), widely used to build complex websites. To manage and develop Drupal projects efficiently, tools like Drush, Drupal Console, and Composer are indispensable. In this article, we will explore the most common commands for each tool, with detailed explanations to help you apply them easily.

1. Drush - Command-Line Tool for Drupal

Drush (Drupal Shell) is a command-line tool that helps manage Drupal quickly and effectively. Below are some common Drush commands:

a. Install Drush

  • Command:
composer require drush/drush
  • Explanation: Installs Drush globally via Composer. After installation, you can run Drush from the Drupal project directory using the command ./vendor/bin/drush.

b. Clear Cache

  • Command:
drush cache:rebuild

or shorthand:

drush cr
  • Explanation: Clears all Drupal caches, which is particularly useful when updating modules or configurations.

c. Update Database

  • Command:
drush updatedb

or shorthand:

drush updb
  • Explanation: Runs database updates from modules or the core when changes are present.

d. Enable/Disable Modules

  • Command:
drush en module_name
drush pm:uninstall module_name
  • Explanation:
    • en (enable): Enables a module.
    • pm:uninstall: Uninstalls a module. Replace module_name with the specific module name (e.g., views, pathauto).

e. Create Database Backup

  • Command:
drush sql-dump > backup.sql
  • Explanation: Exports the current database to a file named backup.sql for backup purposes.

f. Run Cron

  • Command:
drush cron
  • Explanation: Executes Drupal’s cron tasks, such as indexing content or sending bulk emails.

2. Drupal Console - Code Generation Tool

Drupal Console is another command-line tool, focused on code generation and interaction with Drupal 8/9/10. Here are some common commands:

a. Install Drupal Console

  • Command:
composer require drupal/console:~1.0 --prefer-dist
  • Explanation: Installs Drupal Console via Composer. Afterward, run it using the command ./vendor/bin/drupal.

b. Generate a New Module

  • Command:
drupal generate:module
  • Explanation: Creates a new custom module. The tool will prompt you with questions to configure the module name, description, etc.

c. Generate a Controller

  • Command:
drupal generate:controller
  • Explanation: Generates a controller within an existing module, helping define routes and handle logic.

d. Check Configuration

  • Command:
drupal check
  • Explanation: Checks Drupal source code for syntax errors or compatibility issues.

e. Generate a Custom Block

  • Command:
drupal generate:plugin:block
  • Explanation: Creates a custom block to add content to a region in the interface.

3. Composer - Package Management in Drupal

Composer is a widely used dependency management tool in Drupal, facilitating the installation of core, modules, themes, and libraries. Below are common Composer commands:

a. Install Drupal Core

  • Command:
composer create-project drupal/recommended-project my_project
  • Explanation: Creates a new Drupal project with the default configuration in the directory my_project.

b. Install a Module

  • Command:
composer require drupal/module_name
  • Explanation: Installs a specific module (replace module_name with the module name, e.g., drupal/token).

c. Update Modules/Core

  • Command:
composer update drupal/core --with-dependencies
  • Explanation: Updates Drupal core and its related dependencies.

d. Remove a Module

  • Command:
composer remove drupal/module_name
  • Explanation: Removes a module from the project.

e. Check Dependencies

  • Command:
composer show
  • Explanation: Lists all installed packages in the project, including their versions.

Notes on Using These Tools

  • Drush: Ideal for system management (cache, database, cron).
  • Drupal Console: Focused on development (code generation, debugging).
  • Composer: Manages dependencies and project structure.
  • Ensure PHP and Composer are installed on your machine before using these tools.

Conclusion

Using Drush, Drupal Console, and Composer saves time and boosts productivity when working with Drupal. The commands above provide a foundation for managing and developing Drupal projects professionally. If you’re new to Drupal, try applying these commands in a test environment to get familiar with them!

Working Contact Form with Ajax & PHP

Get a functional and working contact form with Ajax & PHP in a few minutes. Just copy and paste the files, add a little code and you’re done.

Download Now