To Reproduce
- Configure a multisite setup with at least one extension (module/theme) in the
sites/[sitename]/[modules|themes]
directory. - Ensure the extension is enabled.
- In the site-specific module, place some code that you can be sure will run. For example, in the .module file:
print __FILE__;
- Observe that the code is running.
- Using drush, rebuild the cache (
drush cr
) - Refresh the page.
- The module code is no longer running.
Expected Result
All enabled modules, including those in multisite directories, are registered and loaded after a cache rebuild or cache clear.
Background
When the cache is cleared from the UI, (Config > Development > Performance), the module data is discovered and registered from the system_rebuild_module_data()
function first. This properly discovers the available modules in all directories and makes that data available to the Kernel as it adds the container.modules parameter to the Container.
When the cache is rebuilt via Drush, this function isn't called until the very end of the request, meaning the module data is discovered from the DrupalKernel::moduleData()
method instead. Within this method, the ExtensionDiscovery
class is invoked to find all modules, but it is never given the sitePath
parameter, and thus ignores any multisite directories.
Solution
We can easily provide the sitePath
parameter to the ExtensionDiscovery
object, then all necessary directories are scanned.