Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACMS-4234: Add config action to import core site studio packages. #1918

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@
"drupal/core": "-p2"
},
"patches": {
"acquia/cohesion": {
"3340269 - Site Studio Drupal 11.1 compatibility": "https://www.drupal.org/files/issues/2025-01-09/3340269-site-studio-drupal-11-1-compatibility.patch"
},
"drupal/core": {
"3328187 - PHP Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in docroot/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php on line 112": "https://git.drupalcode.org/project/drupal/-/merge_requests/3142.patch",
"Fix failing test for site studio due to missing file": "https://gist.githubusercontent.com/chandan-singh7929/978c8c3c8b6f1e2de23492e7e562c0c3/raw/f0e7770d94be862e5495ca25662a0a0d5672b785/bypass-library-version-core.patch"
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

declare(strict_types=1);

namespace Drupal\acquia_cms_site_studio\Plugin\ConfigAction;

use Drupal\cohesion\Controller\AdministrationController;
use Drupal\cohesion_sync\Services\PackageImportHandler;
use Drupal\Core\Config\Action\Attribute\ConfigAction;
use Drupal\Core\Config\Action\ConfigActionPluginInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* The action to import base Site Studio packages.
*/
#[ConfigAction(
id: 'basePackageImport',
admin_label: new TranslatableMarkup('Import base Site Studio packages.'),
)]
final class BasePackageImport implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {

/**
* Constructs a SimpleConfigUpdate object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
* @param \Drupal\Core\Site\Settings $settings
* The settings.
* @param \Drupal\cohesion_sync\Services\PackageImportHandler $packageImportHandler
* The package import handler.
* @param \Drupal\Core\Extension\ModuleHandler $moduleHandler
* The module handler.
*/
public function __construct(
protected readonly ConfigFactoryInterface $configFactory,
protected readonly Settings $settings,
protected readonly PackageImportHandler $packageImportHandler,
protected readonly ModuleHandler $moduleHandler,
) {
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static(
$container->get('config.factory'),
$container->get('settings'),
$container->get('cohesion_sync.package_import_handler'),
$container->get('module_handler'),
);
}

/**
* {@inheritdoc}
*/
public function apply(string $configName, mixed $value): void {
if ($configName === 'cohesion.settings' && $value) {
// Update the configuration with the API and organization keys.
$this->updateConfig($configName);
}
}

/**
* Updates the configuration with API and organization keys.
*
* @param string $configName
* The name of the configuration.
*/
private function updateConfig(string $configName): void {
$config = $this->configFactory->get($configName);
$apiKey = $config->get('api_key');
$orgKey = $config->get('organization_key');

if (!($apiKey && $orgKey)) {
$apiKey = getenv('SITESTUDIO_API_KEY') ?? $this->settings->get('cohesion.settings')->get('api_key');
$orgKey = getenv('SITESTUDIO_ORG_KEY') ?? $this->settings->get('cohesion.settings')->get('organization_key');
if (!($apiKey && $orgKey)) {
return;
}
}

$this->configFactory->getEditable($configName)
->set('api_key', $apiKey)
->set('organization_key', $orgKey)
->save(TRUE);

// Import the base packages.
$this->importBasePackages();
}

/**
* Imports the base Site Studio packages.
*/
private function importBasePackages(): void {
$this->initializeBatch();
$package_list_path = $this->getPackageListPath();
$this->importPackages($package_list_path);
$this->processBatchIfCli();
}

/**
* Initializes the batch process for importing packages.
*/
private function initializeBatch(): void {
batch_set(AdministrationController::batchAction(TRUE));
}

/**
* Gets the path to the package list file.
*
* @return string
* The path to the package list file.
*/
private function getPackageListPath(): string {
$module_path = $this->moduleHandler->getModule('acquia_cms_site_studio')->getPath();
return $module_path . '/config/site_studio/site_studio.packages.yml';
}

/**
* Imports packages from the specified path.
*
* @param string $package_list_path
* The path to the package list file.
*/
private function importPackages(string $package_list_path): void {
$this->packageImportHandler->importPackagesFromPath($package_list_path);
}

/**
* Processes the batch if running in CLI mode.
*/
private function processBatchIfCli(): void {
if (PHP_SAPI === 'cli') {
drush_backend_batch_process();
}
}

}
Loading