Invoke hook after a site install is complete

Created on 18 November 2017, about 7 years ago
Updated 13 April 2023, over 1 year ago

Problem/Motivation

Currently, there is no way for modules to respond to a Drupal site being installed. Modules can implement hook_modules_installed to check if a profile has been installed, but there are many site install tasks that happen after that - specifically optional configuration is installed and the default theme is changed. It would be nice if there was a more explicit hook that ran as the last site install task, so that modules could execute code at the very end of the install process.

Possible implementations of this hook:

  1. Enable an optional module feature if a specific profile is installed.
  2. Show a status message to the user once the install is finished.
  3. Act on optional config installed at the end of the profile installation process (see install_install_profile). This is really hard to do without this hook.

Proposed resolution

Add a new hook, hook_site_install_finished(), which allows modules to respond to a Drupal site install. Modules are also provided the $install_state, which has useful contextual data about the install like the profile and whether or not the install was interactive.

Remaining tasks

Review the patch.

User interface changes

None.

API changes

Adds a new hook, hook_site_install_finished, which is is documented in module.api.php.

Data model changes

None.

Feature request
Status

Needs work

Version

10.1

Component
Install 

Last updated 3 days ago

No maintainer
Created by

🇺🇸United States samuel.mortenson

Live updates comments and jobs are added and updated live.
  • GiftofOpenSource

    Take part in Pantheon's 2021 Gift of Open Source - all contributions and expertise levels welcome!

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇵🇱Poland sandboxpl Poland 🇵🇱

    Needs rerolls for drupal 9.5.x, when testing previous patches with Drupal 9.5.7 I get following errors:

    When installing profile via UI:

    The website encountered an unexpected error. Please try again later.
    Error: Unknown named parameter $interactive in call_user_func_array() (line 426 of core/lib/Drupal/Core/Extension/ModuleHandler.php).
    call_user_func_array(Object, Array) (Line: 426)
    Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}(Object, 'myprofile') (Line: 405)
    Drupal\Core\Extension\ModuleHandler->invokeAllWith('site_install_finished', Object) (Line: 433)
    Drupal\Core\Extension\ModuleHandler->invokeAll('site_install_finished', Array) (Line: 1895)
    install_invoke_finished_hooks(Array) (Line: 700)
    install_run_task(Array, Array) (Line: 571)
    install_run_tasks(Array, NULL) (Line: 119)
    install_drupal(Object) (Line: 48)
    

    When installing width drush si ( drush 11.5):

    Error: Unknown named parameter $parameters in /var/www/html/docroot/core/lib/Drupal/Core/Extension/ModuleHandler.php on line 426 #0 /var/www/html/docroot/core/lib/Drupal/Core/Extension/ModuleHandler.php(426): call_user_func_array(Object(Closure), Array)
    #1 /var/www/html/docroot/core/lib/Drupal/Core/Extension/ModuleHandler.php(405): Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}(Object(Closure), 'myprofile...')
    #2 /var/www/html/docroot/core/lib/Drupal/Core/Extension/ModuleHandler.php(433): Drupal\Core\Extension\ModuleHandler->invokeAllWith('site_install_fi...', Object(Closure))
    #3 /var/www/html/docroot/core/includes/install.core.inc(1895): Drupal\Core\Extension\ModuleHandler->invokeAll('site_install_fi...', Array)
    #4 /var/www/html/docroot/core/includes/install.core.inc(700): install_invoke_finished_hooks(Array)
    #5 /var/www/html/docroot/core/includes/install.core.inc(571): install_run_task(Array, Array)
    #6 /var/www/html/docroot/core/includes/install.core.inc(119): install_run_tasks(Array, Array)
    #7 /var/www/html/vendor/drush/drush/includes/drush.inc(129): install_drupal(Object(Composer\Autoload\ClassLoader), Array, Array)
    #8 /var/www/html/vendor/drush/drush/includes/drush.inc(113): drush_call_user_func_array('install_drupal', Array)
    #9 /var/www/html/vendor/drush/drush/src/Commands/core/SiteInstallCommands.php(152): drush_op('install_drupal', Object(Composer\Autoload\ClassLoader), Array, Array)
    #10 [internal function]: Drush\Commands\core\SiteInstallCommands->install('myprofile...', Array)
    #11 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(257): call_user_func_array(Array, Array)
    #12 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback(Array, Object(Consolidation\AnnotatedCommand\CommandData))
    #13 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(176): Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter(Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
    #14 /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(390): Consolidation\AnnotatedCommand\CommandProcessor->process(Object(Symfony\Component\Console\Output\ConsoleOutput), Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
    #15 /var/www/html/vendor/symfony/console/Command/Command.php(255): Consolidation\AnnotatedCommand\AnnotatedCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #16 /var/www/html/vendor/symfony/console/Application.php(1039): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #17 /var/www/html/vendor/symfony/console/Application.php(275): Symfony\Component\Console\Application->doRunCommand(Object(Consolidation\AnnotatedCommand\AnnotatedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #18 /var/www/html/vendor/symfony/console/Application.php(149): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #19 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(124): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #20 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(51): Drush\Runtime\Runtime->doRun(Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
    #21 /var/www/html/vendor/drush/drush/drush.php(77): Drush\Runtime\Runtime->run(Array)
    #22 /var/www/html/vendor/drush/drush/includes/preflight.inc(18): require('/var/www/html/v...')
    #23 phar:///usr/local/bin/drush/bin/drush.php(143): drush_main()
    #24 /usr/local/bin/drush(14): require('phar:///usr/loc...')
    #25 {main}
    

    basically this:

    function install_invoke_finished_hooks(&$install_state) {
      \Drupal::moduleHandler()->invokeAll('site_install_finished', $install_state);
    }
    

    should become this for php8.1:

    function install_invoke_finished_hooks(&$install_state) {
      \Drupal::moduleHandler()->invokeAll('site_install_finished', array_values($install_state));
    }
    
  • 🇵🇱Poland sandboxpl Poland 🇵🇱

    Reroll for 9.5.x and PHP 8.1

  • Assigned to aneek
Production build 0.71.5 2024