Skip to content

Commit

Permalink
ACMS-4403: Add filter to import search related config for content rec…
Browse files Browse the repository at this point in the history
…ipes only.
  • Loading branch information
rajeshreeputra committed Jan 14, 2025
1 parent b955a17 commit 48a633f
Showing 1 changed file with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\acquia_cms_search\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ConfigInstaller;
use Drupal\Core\Config\ConfigInstallerInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Recipe\RecipeAppliedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -16,6 +16,13 @@
*/
class RecipeConfigImporter implements EventSubscriberInterface {

const CONTENT_MODEL_RECIPES = [
'acquia_starterkit_article',
'acquia_starterkit_event',
'acquia_starterkit_person',
'acquia_starterkit_place',
];

/**
* The config factory service.
*
Expand All @@ -35,15 +42,15 @@ class RecipeConfigImporter implements EventSubscriberInterface {
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
// * @param \Drupal\Core\Config\ConfigInstaller $config_installer
// * The config installer service.
* @param \Drupal\Core\Config\ConfigInstallerInterface $config_installer
* The config installer service.
*/
public function __construct(
ConfigFactoryInterface $config_factory,
// ConfigInstaller $config_installer,
ConfigInstallerInterface $config_installer,
) {
$this->configFactory = $config_factory;
// $this->configInstaller = $config_installer;
$this->configInstaller = $config_installer;
}

/**
Expand All @@ -64,13 +71,15 @@ public static function getSubscribedEvents(): array {
public function onRecipeApply(RecipeAppliedEvent $event): void {
$recipe_name = $this->getRecipeName($event->recipe->path);
if ($recipe_name == 'acquia_starterkit_search') {
// $this->configInstaller->installOptionalConfig(NULL, ['module' => 'acquia_cms_search']);
\Drupal::service('config.installer')->installOptionalConfig(NULL, [
$this->configInstaller->installOptionalConfig(NULL, [
'module' => 'acquia_cms_search'
]);
$this->importSearchConfig();
}
elseif (str_starts_with($recipe_name, 'acquia_starterkit_')) {
elseif (
str_starts_with($recipe_name, 'acquia_starterkit_') &&
in_array($recipe_name, self::CONTENT_MODEL_RECIPES)
) {
$this->importConfigFromRecipe($event->recipe->path);
}
}
Expand All @@ -93,7 +102,9 @@ private function getRecipeName(string $path): string {
* Imports search configuration if it exists.
*/
private function importSearchConfig(): void {
$recipes = $this->configFactory->get('acquia_starterkit_core.settings')->get('recipes_applied') ?? [];
$recipes_applied = $this->configFactory->get('acquia_starterkit_core.settings')->get('recipes_applied') ?? [];
// Filter recipes that are search related.
$recipes = array_intersect($recipes_applied, self::CONTENT_MODEL_RECIPES);
foreach ($recipes as $recipe) {
$recipe_path = $this->getRecipePathByName($recipe);
$this->importConfigFromRecipe($recipe_path . '/' . $recipe);
Expand Down Expand Up @@ -123,8 +134,7 @@ private function importConfigFromRecipe(string $recipe_path): void {
*/
private function importConfigFromPath(string $config_path): void {
$config_source = new FileStorage($config_path);
// $this->configInstaller->installOptionalConfig($config_source);
\Drupal::service('config.installer')->installOptionalConfig($config_source);
$this->configInstaller->installOptionalConfig($config_source);
}

/**
Expand Down

0 comments on commit 48a633f

Please sign in to comment.