Skip to content

Commit

Permalink
Allow projects to specify which user id to pass to drush for login. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
markdorison authored Sep 27, 2023
1 parent 0503956 commit 6806bb7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions .sites.config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
default:
database_s3_bucket: S3_BUCKET_NAME
database_s3_key_prefix_string: prod-db-prefix
drupal_user_login_uid: 108
theme_build:
- theme_path: web/themes/custom/theme1
theme_build_commands:
Expand Down
2 changes: 2 additions & 0 deletions src/Robo/Plugin/Commands/DevelopmentModeBaseCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ public function drupalLoginLink(
string $siteDir = 'default',
): Result {
$this->io()->section("create login link.");
$uid = $this->getDrupalSiteAdminUid(siteName: $siteDir);
return $this->taskExec($environmentType)
->arg('drush')
->arg("@$siteDir.$environmentType")
->arg('user:login')
->option("--uid=$uid")
->dir("$this->drupalRoot/sites/$siteDir")
->run();
}
Expand Down
29 changes: 27 additions & 2 deletions src/Robo/Plugin/Traits/SitesConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ protected function getSiteConfig(string $siteName = 'default'): array
* The site configuration key to load.
* @param string $siteName
* The site name.
* @param bool $required
* Whether the config item is expected to always be present.
*/
public function getSiteConfigItem(string $key, string $siteName = 'default'): mixed
public function getSiteConfigItem(string $key, string $siteName = 'default', bool $required = true): mixed
{
$siteConfig = $this->getSiteConfig(siteName: $siteName);
if (!isset($siteConfig[$key])) {
throw new TaskException($this, "Key $key not found for '$siteName' in $this->sitesConfigFile.");
if ($required) {
throw new TaskException($this, "Key $key not found for '$siteName' in $this->sitesConfigFile.");
}
return null;
}
return $siteConfig[$key];
}
Expand All @@ -99,4 +104,24 @@ protected function writeSitesConfig(array $sitesConfig): void
ksort($sitesConfig);
file_put_contents($this->sitesConfigFile, Yaml::dump($sitesConfig));
}

/**
* Get the Drupal site admin user ID.
*
* @param string $siteName
* The site name.
*
* @return int
* The Drupal admin user ID.
*/
protected function getDrupalSiteAdminUid(string $siteName = 'default'): int
{
return $this->getSiteConfigItem(
key: 'drupal_user_login_uid',
siteName: $siteName,
required: false,
// @todo: Replace the use of '1' with a constant once we drop PHP
// 8.1 support.
) ?? 1;
}
}

0 comments on commit 6806bb7

Please sign in to comment.