Quick guide to enable a module programmatically in Drupal and streamline workflows. Our Drupal Support team is ready to assist you.
Enable a Module Programmatically in Drupal
In Drupal, enabling modules manually works for small projects, but it doesn’t scale well. Programmatic enabling makes deployments faster, more consistent, and less prone to errors. It also fits perfectly into CI/CD pipelines, saving time and ensuring smooth automation. In this article, we’ll show you why this approach is smarter and how to implement it the right way.
Why Enable a Module Programmatically?
Enabling modules straight from the admin panel is the standard procedure in Drupal projects. However, programmatically enabling them is significantly more dependable and efficient in many real-world settings. Let’s examine the main reasons that lead developers and teams to favor this strategy.
- Using CI/CD for seamless automation
Automation pipelines are essential to modern development. You can incorporate the procedure into your CI/CD processes by using code to enable modules. This eliminates the need for human interaction during production updates, guarantees deployments go smoothly, and minimizes downtime.
- Consistent Configuration in Every Environment
The same modules are always active due to programmatic enabling, whether it’s in development, staging, or production. This consistency facilitates debugging and helps avoid unforeseen mistakes brought on by environment mismatches.
Check out our complete guide on the Drupal Devel Generate Module to learn more.
- Time Saving and Error Prevention
It might be time-consuming and prone to mistakes to manually enable several modules across various sites. In contrast, programmatic approaches automate the entire process, reducing the possibility of forgetting a dependency or enabling the incorrect module while simultaneously saving significant time.
- Simple Dependency Management
For proper operation, modules frequently depend on one another. Drupal handles dependencies automatically by expressing the enabling logic in code, making sure nothing is missed. Compared to manual handling, this results in a configuration that is far more dependable.
- Breaking Through Restricted Access
Developers might not have direct access to the production site in large enterprises. In these situations, the only workable method to enable additional modules is to deploy changes via code. By doing this, changes are ensured to reach production without the need for human user interface access.
- Simplified Installations Using Personalized Profiles
Programmatic enabling assures that the necessary modules are turned on from the beginning if your project makes use of installation profiles. As a result, new site setups are quicker, more reliable, and require less manual post-install procedures.
Solution 1: Using the Module Installer Service
// Get the service.
$installer = \Drupal::service('module_installer');
// Install a single module.
$installer->install(['module_name']);
// Install multiple modules at once.
$installer->install(['module_one', 'module_two']);
To uninstall a module, you can use the same service:
$installer->uninstall(['module_name']);
Solution 2: Enabling in an Install File
Using an install file is another popular method for programmatically enabling modules in Drupal. This method is particularly helpful for large projects when several modules need to be enabled simultaneously because it is frequently a component of a custom deployment or site installation strategy.
The module_enable() function can be used; it takes an array of module names as input. It also automatically activates dependencies by default.
// Enable multiple modules.
module_enable(['module_one', 'module_two']);
// Disable automatic dependency enabling.
module_enable(['module_one', 'module_two'], FALSE);
It’s best practice to place this inside an update hook in your deployment or custom install module:
function site_deploy_update_7001() {
module_enable(['entity_view_mode', 'maxlength']);
}
To disable a module, you can use:
module_disable(['module_one', 'module_two']);
And if you need to uninstall it completely:
module_disable(['module_one']);
drupal_uninstall_modules(['module_one']);
Don’t miss our step-by-step guide for easy setup.
Best Practices for Enabling Modules Programmatically in Drupal
Enabling modules with code keeps your Drupal projects faster, safer, and consistent. To do it right, follow these best practices.
Key Principles
- Use Drupal’s built-in functions like module_enable() or module_installer.
- Handle dependencies so supporting modules load correctly.
- Add to deployment scripts for smooth CI/CD workflows.
- Track in version control to keep history and rollbacks simple.
- Document clearly so future developers know why modules were enabled.
Implementation Tips
Place your code in update hooks or install files, add error handling, and test in staging before pushing to production. Always plan around performance to avoid downtime.
Want to remove a module faster? Read our quick Drupal uninstall guide.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
Choosing to enable a module programmatically in Drupal gives you speed, consistency, and reliability across all environments. By automating this process, you reduce manual errors, simplify deployments, and ensure smoother workflows for any project size.
In brief, our Support Experts demonstrated how to fix the “554 5.7.1 : Relay access denied” error.
0 Comments