Problem/Motivation

When installing drupal from an existing configuration that contains this module, installation fails with the following error:

Error: Call to a member function grantPermission() on null in google_analytics_install() (line 17 of /var/www/drupal/web/modules/contrib/google_analytics/google_analytics.install) #0 [internal function]: google_analytics_install()
#1 /var/www/drupal/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(400): call_user_func_array()
#2 /var/www/drupal/web/core/lib/Drupal/Core/Extension/ModuleInstaller.php(357): Drupal\Core\Extension\ModuleHandler->invoke()
#3 /var/www/drupal/web/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php(83): Drupal\Core\Extension\ModuleInstaller->install()
#4 /var/www/drupal/web/core/lib/Drupal/Core/Config/ConfigImporter.php(872): Drupal\Core\ProxyClass\Extension\ModuleInstaller->install()
#5 /var/www/drupal/web/core/lib/Drupal/Core/Config/ConfigImporter.php(624): Drupal\Core\Config\ConfigImporter->processExtension()
#6 /var/www/drupal/web/core/lib/Drupal/Core/Config/ConfigImporter.php(561): Drupal\Core\Config\ConfigImporter->processExtensions()
#7 /var/www/drupal/web/core/lib/Drupal/Core/Config/Importer/ConfigImporterBatch.php(31): Drupal\Core\Config\ConfigImporter->doSyncStep()
#8 /var/www/drupal/web/core/includes/batch.inc(296): Drupal\Core\Config\Importer\ConfigImporterBatch::process()
#9 /var/www/drupal/web/core/includes/form.inc(955): _batch_process()
#10 /var/www/drupal/web/core/includes/install.core.inc(651): batch_process()
#11 /var/www/drupal/web/core/includes/install.core.inc(569): install_run_task()
#12 /var/www/drupal/web/core/includes/install.core.inc(119): install_run_tasks()
#13 /var/www/drupal/vendor/drush/drush/includes/drush.inc(69): install_drupal()
#14 /var/www/drupal/vendor/drush/drush/includes/drush.inc(53): drush_call_user_func_array()
#15 /var/www/drupal/vendor/drush/drush/src/Commands/core/SiteInstallCommands.php(169): drush_op()
#16 [internal function]: Drush\Commands\core\SiteInstallCommands->install()
#17 /var/www/drupal/vendor/consolidation/annotated-command/src/CommandProcessor.php(276): call_user_func_array()
#18 /var/www/drupal/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback()
#19 /var/www/drupal/vendor/consolidation/annotated-command/src/CommandProcessor.php(176): Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter()
#20 /var/www/drupal/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(391): Consolidation\AnnotatedCommand\CommandProcessor->process()
#21 /var/www/drupal/vendor/symfony/console/Command/Command.php(326): Consolidation\AnnotatedCommand\AnnotatedCommand->execute()
#22 /var/www/drupal/vendor/symfony/console/Application.php(1081): Symfony\Component\Console\Command\Command->run()
#23 /var/www/drupal/vendor/symfony/console/Application.php(320): Symfony\Component\Console\Application->doRunCommand()
#24 /var/www/drupal/vendor/symfony/console/Application.php(174): Symfony\Component\Console\Application->doRun()
#25 /var/www/drupal/vendor/drush/drush/src/Runtime/Runtime.php(110): Symfony\Component\Console\Application->run()
#26 /var/www/drupal/vendor/drush/drush/src/Runtime/Runtime.php(40): Drush\Runtime\Runtime->doRun()
#27 /var/www/drupal/vendor/drush/drush/drush.php(139): Drush\Runtime\Runtime->run()
#28 /var/www/drupal/vendor/drush/drush/drush(4): require('...')
#29 /var/www/drupal/vendor/bin/drush(120): include('...')
#30 {main}.
Error: Call to a member function grantPermission() on null in /var/www/drupal/web/modules/contrib/google_analytics/google_analytics.install on line 17 #0 [internal function]: google_analytics_install()

Steps to reproduce

1. Install this module on a blank drupal (10.1.1) site.
2. Export config: drush cex -y
3. Reinstall drupal from the configuration: drush si -y --existing-config
4. You should see the above error.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bander2 created an issue. See original summary.

bander2’s picture

Possibly related to 3340020.

bander2’s picture

I can confirm that this issue does not exist in Drupal 10.0.9. So, maybe this is due to a change in 10.1.

bander2’s picture

enrocean167’s picture

Was running into this issue as well. MR looks good to me, thanks!

eiriksm’s picture

Had a small suggestion in the MR. Otherwise, it works good and is simple enough :)

pookmish’s picture

Status: Active » Reviewed & tested by the community

I needed this with Drupal 10.1 as well. MR worked for me.

loopy1492’s picture

Thanks! Seems to be working for our site during pipelines ci run. I am adding a patch file from the MR diff for those who would rather use a flat patch file in composer.

seanb’s picture

In #3349663-7: Error: Call to a member function grantPermission() on null in userprotect_install() neclimdul found out what is happening. In short, this is caused by the change in #3112866: Remove special case of User module install. Modules are installed first and the default config for the modules is imported later. Since the authenticated user role is default config of the user module, the role doesn't exist yet in the install hook of other modules.

We can add the permissions using user_role_grant_permissions() which is also used in core by media_install() and node_install(). It basically uses the same checks as the patch, so we don't have to duplicate that code. We should probably also add a check for the user module existance (or add a dependency on the user module, but that seems a bit silly).

Since this only seems to happen when installing sites from existing config, it shouldn't really matter that the module install hook can't add the permissions to the authenticated user role.

damienmckenna’s picture

While we wait for core to fix the underlying problem there, I can also confirm that patch #10 resolves the immediate problem.

mxr576 made their first commit to this issue’s fork.

mxr576’s picture

SAML have/had the same issue, IMO in this scenario we should just check if the config sync is running or not, in other words, whether the side is installed from config or not.

https://www.drupal.org/project/simplesamlphp_auth/issues/3392203

(Hiding patch to avoid confusion)

pacproduct’s picture

MR works great, thanks!

bircher’s picture

StatusFileSize
new1.42 KB

I agree with #14 and disagree with #12, while in a deployment when this module is being added to a site, the subsequent config import will revert the change from the install hook, it is still not necessary and the latest patch is more correct and I wish more modules would do it that way.

RTBC +1 for the current code (attached patch for people with composer).

We use this in production and this patch allows us to install from config in our CI pipeline to run tests.

mxr576’s picture

RTBC +1 for the current code (attached patch for people with composer).

FTR, I confirmed that patch#15 reflects the latest state of MR#33

❯ interdiff 33.diff google_analytics-3373921-15.patch
❯ echo $?

japerry made their first commit to this issue’s fork.

  • japerry committed 302b637e on 4.x authored by bander2
    Issue #3373921 by bander2, bircher: Cannot install from existing config
    
japerry’s picture

Status: Reviewed & tested by the community » Fixed

LGTM -- Committed!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.