Drush everywhere

Imagen creador

Eduar Rivera

2022-11-21

drush-en-todos-lados

What is drush?


Drush is a command-line tool for performing everyday Drupal tasks, such as installing, updating, and uninstalling modules and themes; importing and exporting configurations; clearing the cache; running database updates; and performing other administrative tasks.

In addition, Drush allows Drupal developers to create custom scripts to perform specific tasks in an automated way, which can save time and effort on repetitive tasks.

Drush is compatible with most versions of Drupal, and there are a large number of commands and options available to aid in the development of Drupal websites.

 

Where and how is drush used?

Drush is mainly used in web development environments that use Drupal as a content management platform.

To use Drush, you must first install it on your computer and then run Drush commands from the command line.

Below are the basic steps to install Drush:

  1. Verify that the prerequisites are met: To install Drush on Drupal, it is required to have PHP and Composer installed on the server. Verify that these prerequisites are met before proceeding.
     
  2. Install Drush: You can install Drush by following the instructions in the official Drush documentation for your operating system. This usually involves first installing Composer and then running the Drush installation command using Composer with the following command.

    composer global require drush/drush

    This command will install Drush in the Composer global directory.

  3. Verify installation: To verify that the Drush installation was successful, run the following command:

    drush --version

    If it has been installed successfully, the Drush version installed will be displayed.

  4. Add Drush to PATH: To use Drush from any location in the terminal, it is necessary to add the directory where Drush is located to the system PATH. To do this, you can edit the ~/.bashrc or ~/.bash_profile file (depending on your operating system) and add the following line to the end:

    export PATH="$HOME/.composer/vendor/bin:$PATH"

    Save and close the file, then reload the terminal or run the following command to apply the changes:

    source ~/.bashrc
  5. Try Drush: To test Drush, go to the root folder of the Drupal website in the terminal and run one of the Drush commands, such as:

    drush status

    This command will display information about the Drupal website, such as the Drupal version, PHP version, database, and other details.

Here are some examples of how Drush is used:

  1. Installing modules and themes: Drush allows you to install and uninstall Drupal modules and themes via the command line, which can be faster and more convenient than doing it manually through the Drupal web interface.

    For example, to install a module on Drupal, you can run the following command:

    drush pm-install module_name
  2. Updating Drupal and Modules: Drush allows you to easily update Drupal and its modules to the latest version available, which can save time and effort when updating multiple Drupal websites using the following Drupal command.

    drush pm-update
  3. Execution of database commands: Drush allows you to execute database commands such as creating, importing, and exporting Drupal databases, performing database schema updates, and clearing the database cache.
     
  4. Configuration Export and Import: Drush allows you to import and export Drupal configuration, which can be useful for migrating configurations between websites or backing up your current configuration.

    Using the following Drush commands, you can export and import configuration:

    drush config-export, drush config-import
  5. Execution of custom tasks: Drush allows Drupal developers to create custom scripts to perform specific tasks in an automated way, which can save time and effort on repetitive tasks. These scripts can be executed using the Drush command "scr", followed by the name of the script file. For example:

    drush scr my_script.php

 

How do I create a custom module with Drush?

These are the basic steps to create a custom module in Drupal with Drush:

  1. Open a terminal and navigate to the root of your Drupal site.
     
  2. Run the following command to create an empty module:

    drush generate module
  3. You will be prompted to enter the name of the module you wish to create. For example, if you want to create a module called "my_module", enter the following:

    Enter the name of the module: mi_modulo
  4. Drush will generate the basic skeleton of the module in the "modules/custom/my_module" folder. You can customize this skeleton to suit your needs.
     
  5. Open the "my_module.info.yml" file in your code editor and add the following basic module information:

    name: Mi Módulo
    type: module
    description: Agrega nuevas funcionalidades al sitio web.
    Custom version:1.0 core:8.x
  6. Create a file called "my_module.module" in the same folder and add the code you want the module to run.
     
  7. For Drupal to detect your module, you must add a dependency in the file "my_module.info.yml" indicating the modules that your module needs to work. For example, if your module needs the "views" module, add the following line:

    dependencies: - views
  8. Finally, activate the module from the Drupal administration interface or using the following Drush command:

    drush on my_module

 

Here are some of the most commonly used Drush commands for managing a Drupal website:

  1. drush status: This command is used in drupal to display important information about the status of the site and its configuration, such as:

    ° The version of Drupal that is being used.
    ° The number of registered users on the site.
    ° The name of the database being used.
    ° The version of PHP that is being used.
    ° Information about the cache and file system.
     
  2. drush cache-clear: Drupal caches information to improve site performance and reduce page load time. However, sometimes it is necessary to clear the cache so that changes that have been made to the site are reflected immediately. When this command is executed, Drupal clears all site caches, including the page cache, menu cache, block cache, view cache, and so on. This causes the site to have to generate new versions of these caches on the next page load.
     
  3. drush pm-list: Lists all modules and themes installed on the site, including their status (enabled or disabled), version, and a brief description of each one. You can also use this command to filter the list by specific modules or themes.
     
  4. drush pm-enable: This command is useful for Drupal site administrators who need to enable new modules or themes on their site to add new functionality or features. It can also be used to enable modules or themes that were already installed but were previously disabled.
     
  5. drush pm-disable: This command is useful for Drupal site administrators who need to disable modules or themes that are no longer used or are causing problems on the site. It can also be used to disable modules or themes that are no longer needed to reduce the load on the server and improve site performance.
     
  6. drush pm-update: This command is useful for Drupal site administrators who need to keep their site up to date with the latest versions of modules, themes, and libraries to fix bugs, add new functionality, or improve site performance. It's also an easy and convenient way to keep your site up to date instead of having to manually update each module, theme, or library individually.
     
  7. drush sql-dump: Running this command creates an SQL file containing a complete copy of the Drupal site database, including all tables, columns, and data. The SQL file can be saved to the desired location and can then be used to restore the site from that backup in case something goes wrong with the site.
     
  8. drush sql-cli: Running this command opens a connection to the Drupal site's database through the MySQL console. This allows you to run SQL commands directly against the site's database, which is useful for performing advanced database administration tasks.
     
  9. drush uli: Generates a URL that includes a single sign-on token. When this URL is accessed in a web browser, a user session is automatically started on the corresponding Drupal site.
     
  10. drush config-export: This command is used to export the configuration in a YAML configuration file to a specific location on the file system. Once exported, the YAML file can be versioned in a version control system such as Git, allowing site configuration changes to be tracked and reverted.
     
  11. drush config-import: This command is used to import a Drupal site's configuration from a YAML file on the file system to the site's database. This allows you to apply configuration changes efficiently and accurately to a Drupal site.
     
  12. drush user-create: Used to create a new user on a Drupal site from the command line, allowing you to automate the user creation process and save time. Roles can also be set for the new user to ensure they have the proper permissions on the site.
     

You may also like

Alternativas a Lando
Eduar Rivera 2022-11-21
drush-en-todos-lados
Eduar Rivera 2022-11-21