Drush Documentation
repository·14.x·Indexed 25 days ago
https://github.com/drush-ops/drushA command line shell and Unix scripting interface designed specifically for Drupal. Drush allows developers to manage Drupal sites, automate administrative tasks, and interact with the Drupal environment via a terminal-based interface. Documentation covers the bootstrap process, creating custom commands using PHP 8 Attributes or annotations, PSR-4 auto-discovery, and configuration management commands like config:export, config:pull, and config:import.
What's inside Drush
- Drush is a command line shell and Unix scripting interface for Drupal. It is used to automate tasks, manage Drupal sites, and interact with the Drupal environment via the terminal.
Deprecated dependency injection methods
14.xThe following patterns are deprecated and should be replaced with modern approaches:
drush.services.yml: Drush 11 and prior used service files for dependency injection. This is deprecated in Drush 12+ in favor of Autowiring.CustomEventAwareInterface: Commands should now fire their own events instead of implementing this interface.StdinAwareInterface: To read fromstdin, use the approach implemented inConfigSetCommandto allow for better testability.
How auto-discovered (global) generators work
14.xGlobal generators are those that do not ship inside a Drupal module. To use a global generator, your class must follow these PSR4 requirements:
- Namespace: The class namespace must be
Drush\Generatorsrelative to your library's base namespace. For example, if your library's PSR4 mapping isMy\Custom\Library\ => src, your generator class must beMy\Custom\Library\Drush\Generators. - File Location: The file must be located in the
Drush/Generatorsdirectory within your source folder (e.g.,src/Drush/Generators). - Naming Convention: The filename must follow the pattern
[Name]Generator.php(e.g.,FooGenerator.php).
"autoload": { "psr-4": { "My\\Custom\\Library\\": "src" } }- Namespace: The class namespace must be
Access Drupal entities without namespaces in php:cli
14.xWhen using thephp:cliREPL, Drupal entity classes are automatically available without requiring their full namespaces. This allows for much faster interaction when performing common tasks like loading entities.How Drush configuration files are discovered
14.xDrush searches for
drush.ymlfiles in a specific order of precedence. When multiple files are found, they are merged. To see all discovered configuration files, rundrush status --fields=drush-conf.Discovery Order:
- Drupal site folder (e.g.
sites/{example.com}/drush.yml). sites/all/drush,WEBROOT/drush, orPROJECTROOT/drush.- Any location specified via the
--configoption. - User's
.drushfolder (e.g.~/.drush/drush.yml). - System-wide configuration folder (e.g.
/etc/drush/drush.ymlorC:\ProgramData\Drush\drush.yml).
- Drupal site folder (e.g.
Avoid running cron during Maintenance mode
14.xBefore running the
croncommand, you should verify the site's maintenance status. If a site is in maintenance mode,cronwill not run and the command will return a failure. It is not safe to run cron while the site is in maintenance.You can check the status using the
maint:statuscommand.Use the Drupal configuration override system for environment-specific values
14.xFor simple value changes that vary by environment (e.g., changing the site name on a local development server), use Drupal's configuration override system instead of modifying configuration files. This is done by setting variables in the site's
settings.phpfile.When to use overrides:
- Use overrides for environment-specific values like site names, API keys, or connection settings.
- Limitation: Overrides do not work for certain structural changes, such as enabling or disabling modules.
When to use Drush config commands:
- Use
config:export,config:pull, orconfig:importfor structural changes or when you need the changes to be permanent in the configuration files.
$config['system.site']['name'] = 'Local Install of Awesome Widgets, Inc.';Use wildcard aliases for multiple environments
14.xIf multiple environments share identical configurations except for their names, you can use a wildcard alias
*. Use the${env-name}variable to inject the environment name into values likehost,root, oruri.Warning: Wildcards will match any name provided, even typos. It is recommended to use an alias alter hook to validate environment names.
# File: remote-example.site.yml '*': host: ${env-name}.server.domain.com user: www-admin root: /path/to/${env-name} uri: http://${env-name}.remote-example.comUnderstand SiteAlias objects
14.xThe Site Alias Manager (SAM) works withSiteAliasobjects to represent individual site aliases. These objects contain methods to inspect the alias, such as determining whether the alias points to a local host or a remote host.Auto-discover global Drush commands via PSR-4
14.xDrush can automatically discover commands from third-party libraries or site-wide packages if they follow specific PSR-4 naming conventions.
Discovery Requirements:
- The class must be PSR-4 auto-loadable.
- The namespace must include
Drush\Commandsrelative to the base namespace. For example, if your library isMy\Custom\Library\, the command class must be inMy\Custom\Library\Drush\Commands. - The class and file name must end with
Commands(e.g.,FooCommands). - The file must be located in a
Drush/Commandssubdirectory.
Important Notes:
- Symlinked Packages: Drush will not discover packages installed via Composer's
pathrepository type. To use these during development, you must explicitly specify the package path in yourdrush.ymlconfiguration. - Version Compatibility: It is recommended to declare Drush version compatibility in your package's
composer.jsonusing aconflictdirective rather than arequiredirective.
"conflict": { "drush/drush": "<11.0" }Understanding Drush test suites: Unit, Integration, and Functional
14.xDrush uses three distinct types of test suites, each with different isolation levels and requirements:
- Unit tests: Operate on pure functions. They require no database connection and no Drupal site setup. They have no side effects.
- Integration tests: Set up a test dependency injection container and call Symfony Application APIs directly. They use a Drupal site called the System Under Test (SUT). The SUT is installed once and reused for all tests, meaning tests cannot make destructive changes to the SUT database. Drupal is bootstrapped using the standard kernel (not install/update kernels), so all commands run at
@bootstrap full. Argument/option parsing and shutdown/error handlers are not tested here. - Functional tests: Execute the Drush binary via
exec. Each test runs in its own separate process. The Drupal SUT is set up for every functional test, so it is safe to make destructive changes to the SUT state. However, the codebase itself should not be modified.
Understand Drush help output structure
14.xWhen you run a Drush command with help (e.g.,
drush help <command>), the output is formatted to provide a structured overview of how to use that command. TheHelpCLIFormatterorganizes this information into the following sections:- Description: A brief summary of what the command does.
- Help: Detailed documentation if provided (distinct from the short description).
- Examples: Concrete usage patterns, either from annotated commands or derived from command usages.
- Arguments: A table listing the command's arguments, their requirement status (e.g.,
[argument]for optional,argument...for arrays), and default values. - Options: A table of command-specific flags and options, including their shortcuts (e.g.,
-v, --verbose) and whether they require values (e.g.,--option=VALUEor--option[=VALUE]). - Global options: A subset of important global options like
uri,verbose, andyes. - Topics: Related command topics that you can explore.
- Aliases: Any alternative names for the command.