Allow an install hook in profiles installing from configuration

Created on 27 June 2018, over 6 years ago
Updated 30 January 2023, almost 2 years ago

Problem/Motivation

In #2788777: Allow a site-specific profile to be installed from existing config โ†’ we excluded profiles from being installed that contain hook_install implementations.

The reasons for this was to exclude complexity, limit the scope of the issue and getting it done.
The complexity can be summarized as:
Install hooks in profiles were so far only called when a site was installed and after all the listed modules were installed and all the configuration was imported. Thus hook_install implementations were able to make a lot of assumptions about the state of the site when the code was executed.
When installing from configuration the configuration is synchronized and the install hooks are called as the extensions are installed and then the configuration is imported. In addition a profile can list modules to install which are not dependencies and, therefore, these modules can be uninstalled. The install hook can therefore also not rely on all the modules being installed.
The canonical example being standard expecting the contact module to be installed.

Right now if a profile wants to be able to be installed from config and still do something extra it has to not implement hook_install and instead define a custom install step.

Proposed resolution

to be defined

Temporary Workaround

See @slucero's comment in #25. There are differences with hook_install, see hook_install_tasks for details. This is an example of how it can be used, replacing "PROFILE" with your profile's machine name:

/**
 * Implements hook_install_tasks().
 */
function PROFILE_install_tasks(&$install_state) {
  $tasks = [
    'PROFILE_install_content' => [
      'display_name' => t('Install default content'),
      'type' => 'normal',
    ]
  ];

  return $tasks;
}


/**
 * Callback function to install default profile content.
 *
 * @see PROFILE_install_tasks()
 */
function PROFILE_install_content() {
  ...
}

Remaining tasks

find solution
implement it
test it
commit it

User interface changes

none probably.

API changes

to be seen.

Data model changes

none.

๐Ÿ› Bug report
Status

Needs work

Version

10.1 โœจ

Component
Installย  โ†’

Last updated 3 days ago

No maintainer
Created by

๐Ÿ‡จ๐Ÿ‡ญSwitzerland bircher ๐Ÿ‡จ๐Ÿ‡ฟ

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • The Needs Review Queue Bot โ†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide โ†’ to find step-by-step guides for working with issues.

  • for those using Drupal 9.x, the solution of changing profile from standard to minimal in this article worked for me; hope it'll work for you too (:

  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Yes, that's a great tip in Drupal 8: Install Site From Existing Configuration from @philipnorton42, and it works well in Drupal 10 as well. Make sure you replace both instances.

    If none of your modules or themes contain the string "standard", you can use this to replace via the command line:

    sed -i 's|standard|minimal|g' core.extension.yml

  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan tyler36 Osaka

    Came across this today with Drupal 10.1.

    MR needs rebasing but I get error: "Something went wrong. Please try again."

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia bhanu951

    @tyler36 I will rebase it to 11.x , I have edit access wait for some time, if you are planning to work on it.

  • last update over 1 year ago
    Build Successful
  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany luenemann Sรผdbaden, Germany

    More then 3000 tests are failing, for example:

    1) Drupal\Tests\system\Functional\Module\InstallUninstallTest::testInstallUninstall
    Array to string conversion
    
    /var/www/html/core/lib/Drupal/Core/Extension/ModuleHandler.php:399
    /var/www/html/core/lib/Drupal/Core/Extension/ModuleInstaller.php:385      <<<<<<<<<<< Changed by MR !4330
    /var/www/html/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php:83
    /var/www/html/core/includes/install.inc:557
    /var/www/html/core/includes/install.core.inc:1114
    /var/www/html/core/includes/install.core.inc:707
    /var/www/html/core/includes/install.core.inc:578
    /var/www/html/core/includes/install.core.inc:121
    /var/www/html/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php:290
    /var/www/html/core/tests/Drupal/Tests/BrowserTestBase.php:553
    /var/www/html/core/tests/Drupal/Tests/BrowserTestBase.php:366
    /var/www/html/core/modules/system/tests/src/Functional/Module/ModuleTestBase.php:31
    /var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany donquixote

    The existing MR is weird.

    Problem recap

    Let's summarize what we need:

    • We want to replicate the setup of the original developer who installed the site from an install profile, and who exported the configuration.
    • Configuration from the install profile should eventually be ignored, because all config is coming from config/sync.
    • Other changes from a profile install hook, which are not covered by config, should still be applied: E.g. to assign 'administrator' role to user 1.
    • Non-config changes in hook_install() from other modules that were installed _after_ the install profile should get priority over the changes from the install profile. E.g. if another module wants to remove the 'administrator' role from user 1, that should be be allowed.
    • The hook_install() in the install profile might require some modules that the profile declares as dependencies, but which are no longer installed in the exported config.

    Proposed solution

    The site-install from existing config should do two separate steps:

    1. Step 1 installs from the install profile.
    2. Step 2 imports the configuration.

    Probably this needs two separate processes.

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland bircher ๐Ÿ‡จ๐Ÿ‡ฟ

    I am not particularly happy with the current MR, for one it changes things in node_install and user_install that seem unrelated. Also if we allow an install hook we should just add it to one of the test profiles, no need to create it in a test only.

    But more importantly I don't think the proposed solution from #101 would work. If we install normally and then in a second step import the config some install profiles will still not work and we just find out later and have no way to predict it. The problem is that you can uninstall modules, but only sometimes! There are a bunch of uninstall validators that prevent you from uninstalling a module if there is content for it still. So if a install profiles adds something that prevents one of the optional modules to be uninstalled without any manual interaction then the "import config" step will fail.
    This is in a way the same problem that prevented us from allowing install hooks in the first place. Install hooks were allowed to make assumptions that are not true in the "install from config" scenario, even if we don't run the install hook of the profile not when the extensions are installed but only at the end of the profile installation process. (ie a profile can assume that optional modules are installed in the normal installation, but not in the "from existing config" step, and of course the same is true for the default config in the profile)

    It has now been a while that these profiles are missing out on a key feature of Drupal core. So maybe it is now ok to just call the profiles install hooks at the end of the install process and skip them in the normal extension installation. This would work in both cases install from config or not. Then if the hook makes assumptions about what is available then it is a bug in the profile and it should add checks around the assumptions.

  • ๐Ÿ‡ฆ๐Ÿ‡ฑAlbania elvin - albania drupal developer

    Today i came accross this: https://www.drupal.org/node/3432357 โ†’

    I was able to uninstall the "Standard" profile, export cofig, and then i was able to do a
    drush site:install --existing-config
    successfully.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    @elvin - albania drupal developer yep I think that that change makes this change now unnecessary to pursue.

  • ๐Ÿ‡น๐Ÿ‡ทTurkey Kartagis Istanbul

    Corrected a grammatical typo in title.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium ericvl

    @elvin
    Is there a way to install Drupal and getting the choise of selecting the "existing configuration" without making use of Drush? Just by the user interface.
    I don't have Drush on my site. therefor one needs access to the shell and I haven't.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    @ericvl yes if you the exported config in a directory and $settings['config_sync_directory'] in your settings.php already and you visit the interactive installer you'll get the option to install from existing configuration via the UI.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium ericvl

    @alexpott
    Thank you for the quick response but it doesn't work for me. I do not get the selectbutton for existing config.
    I'm using Drupal 10.3.5. Maybe I should use the dev version?

    What I do:
    - export the configuration and import it back in so that the contents of the config_sync_directory mirrors what is in the database.
    - remove the standard profile
    - remove all tables in yhe database. Remark: the specs of the database are still in the settings.php
    - I'm visiting the interactive installer by accessing the root of site (this redict to the installer)
    - I can not select the existing config.

    If you can spare a few minutes to look into this, thanks

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    @ericvl you need to uninstall the standard profile prior to exporting the configuration. Then on the select install profile screen you will see an option during the installer - where you select the install profile. See below:

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium ericvl

    @alexpott
    Thank you for helping me out. It works.
    Sorry to bother you with such a stupid problem.
    Thnx

Production build 0.71.5 2024