From 6921df3a35e3a8f874748110a4c53964cdcfb9d3 Mon Sep 17 00:00:00 2001 From: rajeshreeputra Date: Fri, 27 Dec 2024 17:27:52 +0530 Subject: [PATCH] ACMS-4246: Refactor Acquia Starter Kit Search to work with recipe. --- .../acquia_cms_search.info.yml | 1 - .../acquia_cms_search.install | 16 ++--- .../acquia_cms_search.module | 60 ++++++------------- modules/acquia_cms_search/composer.json | 2 - .../block.block.clear_facet_filters.yml | 16 ++--- .../block.block.exposed_form_search.yml | 13 ++-- .../optional/block.block.search_category.yml | 13 ++-- .../block.block.search_content_type.yml | 13 ++-- .../search_api.index.content.yml | 3 - .../search_api_autocomplete.search.search.yml | 0 .../optional/views.view.acquia_search.yml | 2 +- .../views.view.search.yml | 1 - .../views.view.search_fallback.yml | 0 .../schema/acquia_cms_search.schema.yml | 10 +++- .../src/Facade/SearchFacade.php | 9 +-- 15 files changed, 56 insertions(+), 103 deletions(-) rename modules/acquia_cms_search/config/{install => optional}/search_api.index.content.yml (97%) rename modules/acquia_cms_search/config/{install => optional}/search_api_autocomplete.search.search.yml (100%) rename modules/acquia_cms_search/config/{install => optional}/views.view.search.yml (99%) rename modules/acquia_cms_search/config/{install => optional}/views.view.search_fallback.yml (100%) diff --git a/modules/acquia_cms_search/acquia_cms_search.info.yml b/modules/acquia_cms_search/acquia_cms_search.info.yml index 74ddb9f01..236651b4f 100644 --- a/modules/acquia_cms_search/acquia_cms_search.info.yml +++ b/modules/acquia_cms_search/acquia_cms_search.info.yml @@ -4,7 +4,6 @@ description: "Provides powerful search capabilities to the site" type: module core_version_requirement: ^10.1 || ^11 dependencies: - - acquia_cms_common:acquia_cms_common - collapsiblock:collapsiblock - facets:facets - facets_pretty_paths:facets_pretty_paths diff --git a/modules/acquia_cms_search/acquia_cms_search.install b/modules/acquia_cms_search/acquia_cms_search.install index 355a01258..563fa3352 100644 --- a/modules/acquia_cms_search/acquia_cms_search.install +++ b/modules/acquia_cms_search/acquia_cms_search.install @@ -24,21 +24,15 @@ function acquia_cms_search_install($is_syncing) { // Retroactively enable indexing for any content types that existed before // this module was installed. - $node_types = NodeType::loadMultiple(); + $node_types = \Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple(); array_walk($node_types, 'acquia_cms_search_node_type_insert'); - $enabled_modules = \Drupal::service('module_handler')->getModuleList(); - $enabled_modules = array_keys($enabled_modules); - _acquia_cms_search_add_category_facet($enabled_modules); + $search_category = \Drupal::entityTypeManager()->getStorage('facets_facet')->loadByProperties(['id' => 'search_category']); + if (!$search_category) { + _acquia_cms_search_add_category_facet(); + } } } -/** - * Implements hook_module_preinstall(). - */ -function acquia_cms_search_module_preinstall($module) { - \Drupal::service('acquia_cms_common.utility')->setModulePreinstallTriggered($module); -} - /** * Added enforced dependency in site studio templates for Acquia CMS Search. * diff --git a/modules/acquia_cms_search/acquia_cms_search.module b/modules/acquia_cms_search/acquia_cms_search.module index 06f3a9de6..e604147c0 100644 --- a/modules/acquia_cms_search/acquia_cms_search.module +++ b/modules/acquia_cms_search/acquia_cms_search.module @@ -65,6 +65,19 @@ function acquia_cms_search_views_plugins_cache_alter(&$definitions) { */ function acquia_cms_search_node_type_insert(NodeTypeInterface $node_type) { Drupal::classResolver(SearchFacade::class)->addNodeType($node_type); + if (!$node_type->isSyncing()) { + _acquia_cms_search_add_category_facet(); + } +} + +/** + * Implements hook_ENTITY_TYPE_insert() for node types. + */ +function acquia_cms_search_node_type_update(NodeTypeInterface $node_type) { + Drupal::classResolver(SearchFacade::class)->addNodeType($node_type); + if (!$node_type->isSyncing()) { + _acquia_cms_search_add_category_facet(); + } } /** @@ -76,12 +89,6 @@ function acquia_cms_search_field_config_insert(FieldConfigInterface $field_confi // Adding the third party settings in the node.body storage configuration to // index the body field for searching. - if ($field_storage->getType() === 'text_with_summary' && $field_storage->id() === 'node.body') { - $field_storage->setThirdPartySetting('acquia_cms_common', 'search_index', 'content') - ->setThirdPartySetting('acquia_cms_common', 'search_label', 'Body') - ->save(); - } - if ($field_storage->getType() === 'entity_reference' && $field_storage->getSetting('target_type') === 'taxonomy_term') { Drupal::classResolver(SearchFacade::class)->addTaxonomyField($field_storage); } @@ -136,34 +143,17 @@ function acquia_cms_search_search_api_server_insert(ServerInterface $server) { } // If the index wants to opt into using this server, grant its wish. - $server_name = $index->getThirdPartySetting('acquia_cms_common', 'search_server'); + $server_name = $index->getThirdPartySetting('acquia_cms_search', 'search_server'); if ($server_name && $server->id() === $server_name) { $index->setServer($server) ->enable() // The third-party setting is only needed once. - ->unsetThirdPartySetting('acquia_cms_common', 'search_server') + ->unsetThirdPartySetting('acquia_cms_search', 'search_server') ->save(); } } } -/** - * Implements hook_config_schema_info_alter(). - */ -function acquia_cms_search_config_schema_info_alter(array &$definitions) { - $key = 'node.type.*.third_party.acquia_cms_common'; - // Allow node types to carry a 'search_index' setting. This is used by our - // facade to passively opt the node type into a particular index. - // @see acquia_cms_search_node_type_insert() - // @see \Drupal\acquia_cms_search\Facade\SearchFacade::addNodeType() - if (array_key_exists($key, $definitions)) { - $definitions[$key]['mapping']['search_index'] = [ - 'type' => 'string', - 'label' => 'The machine name of the search index to which this content type should be added', - ]; - } -} - /** * Implements hook_ENTITY_TYPE_update(). */ @@ -180,26 +170,12 @@ function acquia_cms_search_form_acquia_cms_search_form_alter(array &$form) { $form['#submit'][] = AcquiaSearchFacade::class . '::submitSettingsForm'; } -/** - * Implements hook_modules_installed(). - */ -function acquia_cms_search_modules_installed(array $modules, $is_syncing) { - if (!$is_syncing) { - _acquia_cms_search_add_category_facet($modules); - } -} - /** * Adds search_category facet, if any acms search dependent module is installed. - * - * @param array $modules - * An array of installed modules. */ -function _acquia_cms_search_add_category_facet(array $modules) { - $acquiaCmsSearchModules = ["acquia_cms_person", "acquia_cms_place", - "acquia_cms_article", "acquia_cms_event", "acquia_cms_page", - ]; - if (array_intersect($acquiaCmsSearchModules, $modules)) { +function _acquia_cms_search_add_category_facet() { + $search_category = \Drupal::entityTypeManager()->getStorage('facets_facet')->loadByProperties(['id' => 'search_category']); + if (!$search_category) { \Drupal::classResolver(FacetFacade::class)->addFacet([ 'id' => 'search_category', 'name' => 'Category', diff --git a/modules/acquia_cms_search/composer.json b/modules/acquia_cms_search/composer.json index a83874e2e..5dcfaf4d5 100644 --- a/modules/acquia_cms_search/composer.json +++ b/modules/acquia_cms_search/composer.json @@ -4,8 +4,6 @@ "license": "GPL-2.0-or-later", "type": "drupal-module", "require": { - "cweagans/composer-patches": "^1.7", - "drupal/acquia_cms_common": "^1.9 || ^2.1 || ^3.1", "drupal/acquia_search": "^3.1", "drupal/collapsiblock": "^4.0", "drupal/facets": "^2.0", diff --git a/modules/acquia_cms_search/config/optional/block.block.clear_facet_filters.yml b/modules/acquia_cms_search/config/optional/block.block.clear_facet_filters.yml index 90b47eb82..87f3ab9fb 100644 --- a/modules/acquia_cms_search/config/optional/block.block.clear_facet_filters.yml +++ b/modules/acquia_cms_search/config/optional/block.block.clear_facet_filters.yml @@ -3,16 +3,10 @@ status: true dependencies: module: - acquia_cms_search - - cohesion - collapsiblock - theme: - - cohesion_theme -third_party_settings: - collapsiblock: - collapse_action: 0 id: clear_facet_filters -theme: cohesion_theme -region: dx8_hidden +theme: olivero +region: sidebar weight: 0 provider: null plugin: clear_facet_filters @@ -22,7 +16,7 @@ settings: label_display: '0' provider: acquia_cms_search visibility: - cohesion_master_template: - id: cohesion_master_template + request_path: + id: request_path negate: false - using_master_template: 0 + pages: "/articles\r\n/events\r\n/places\r\n/people" diff --git a/modules/acquia_cms_search/config/optional/block.block.exposed_form_search.yml b/modules/acquia_cms_search/config/optional/block.block.exposed_form_search.yml index 1694dd340..d7bc3b847 100644 --- a/modules/acquia_cms_search/config/optional/block.block.exposed_form_search.yml +++ b/modules/acquia_cms_search/config/optional/block.block.exposed_form_search.yml @@ -4,13 +4,10 @@ dependencies: config: - views.view.search module: - - cohesion - views - theme: - - cohesion_theme id: exposed_form_search -theme: cohesion_theme -region: dx8_hidden +theme: olivero +region: sidebar weight: 0 provider: null plugin: 'views_exposed_filter_block:search-search' @@ -21,7 +18,7 @@ settings: provider: views views_label: '' visibility: - cohesion_master_template: - id: cohesion_master_template + request_path: + id: request_path negate: false - using_master_template: 0 + pages: "/search" diff --git a/modules/acquia_cms_search/config/optional/block.block.search_category.yml b/modules/acquia_cms_search/config/optional/block.block.search_category.yml index 3cfd5a2cd..7d9d46b3e 100644 --- a/modules/acquia_cms_search/config/optional/block.block.search_category.yml +++ b/modules/acquia_cms_search/config/optional/block.block.search_category.yml @@ -4,17 +4,14 @@ dependencies: config: - facets.facet.search_category module: - - cohesion - collapsiblock - facets - theme: - - cohesion_theme third_party_settings: collapsiblock: collapse_action: 2 id: search_category -theme: cohesion_theme -region: dx8_hidden +theme: olivero +region: sidebar weight: 0 provider: null plugin: 'facet_block:search_category' @@ -25,7 +22,7 @@ settings: provider: facets block_id: search_category visibility: - cohesion_master_template: - id: cohesion_master_template + request_path: + id: request_path negate: false - using_master_template: 0 + pages: "/search" diff --git a/modules/acquia_cms_search/config/optional/block.block.search_content_type.yml b/modules/acquia_cms_search/config/optional/block.block.search_content_type.yml index 41370f512..b0a84d10f 100644 --- a/modules/acquia_cms_search/config/optional/block.block.search_content_type.yml +++ b/modules/acquia_cms_search/config/optional/block.block.search_content_type.yml @@ -4,17 +4,14 @@ dependencies: config: - facets.facet.search_content_type module: - - cohesion - collapsiblock - facets - theme: - - cohesion_theme third_party_settings: collapsiblock: collapse_action: 2 id: search_content_type -theme: cohesion_theme -region: dx8_hidden +theme: olivero +region: sidebar weight: 0 provider: null plugin: 'facet_block:search_content_type' @@ -25,7 +22,7 @@ settings: provider: facets block_id: search_content_type visibility: - cohesion_master_template: - id: cohesion_master_template + request_path: + id: request_path negate: false - using_master_template: 0 + pages: "/search" diff --git a/modules/acquia_cms_search/config/install/search_api.index.content.yml b/modules/acquia_cms_search/config/optional/search_api.index.content.yml similarity index 97% rename from modules/acquia_cms_search/config/install/search_api.index.content.yml rename to modules/acquia_cms_search/config/optional/search_api.index.content.yml index ab47530fd..5bff5dcdf 100644 --- a/modules/acquia_cms_search/config/install/search_api.index.content.yml +++ b/modules/acquia_cms_search/config/optional/search_api.index.content.yml @@ -9,9 +9,6 @@ dependencies: enforced: module: - acquia_cms_search -third_party_settings: - acquia_cms_common: - search_server: database id: content name: Content description: 'An index of all content in your site.' diff --git a/modules/acquia_cms_search/config/install/search_api_autocomplete.search.search.yml b/modules/acquia_cms_search/config/optional/search_api_autocomplete.search.search.yml similarity index 100% rename from modules/acquia_cms_search/config/install/search_api_autocomplete.search.search.yml rename to modules/acquia_cms_search/config/optional/search_api_autocomplete.search.search.yml diff --git a/modules/acquia_cms_search/config/optional/views.view.acquia_search.yml b/modules/acquia_cms_search/config/optional/views.view.acquia_search.yml index b892b7c9c..5f44dab07 100644 --- a/modules/acquia_cms_search/config/optional/views.view.acquia_search.yml +++ b/modules/acquia_cms_search/config/optional/views.view.acquia_search.yml @@ -6,10 +6,10 @@ dependencies: - search_api.index.acquia_search_index - system.menu.main module: + - acquia_search - search_api - text - user - - acquia_cms_development id: acquia_search label: 'Acquia Search Solr' module: views diff --git a/modules/acquia_cms_search/config/install/views.view.search.yml b/modules/acquia_cms_search/config/optional/views.view.search.yml similarity index 99% rename from modules/acquia_cms_search/config/install/views.view.search.yml rename to modules/acquia_cms_search/config/optional/views.view.search.yml index 438d9f737..b8654a8a1 100644 --- a/modules/acquia_cms_search/config/install/views.view.search.yml +++ b/modules/acquia_cms_search/config/optional/views.view.search.yml @@ -5,7 +5,6 @@ dependencies: - search_api.index.content - views.view.search_fallback module: - - acquia_cms_search - search_api - user id: search diff --git a/modules/acquia_cms_search/config/install/views.view.search_fallback.yml b/modules/acquia_cms_search/config/optional/views.view.search_fallback.yml similarity index 100% rename from modules/acquia_cms_search/config/install/views.view.search_fallback.yml rename to modules/acquia_cms_search/config/optional/views.view.search_fallback.yml diff --git a/modules/acquia_cms_search/config/schema/acquia_cms_search.schema.yml b/modules/acquia_cms_search/config/schema/acquia_cms_search.schema.yml index 649f43c31..239c899fe 100644 --- a/modules/acquia_cms_search/config/schema/acquia_cms_search.schema.yml +++ b/modules/acquia_cms_search/config/schema/acquia_cms_search.schema.yml @@ -1,4 +1,4 @@ -search_api.index.*.third_party.acquia_cms_common: +search_api.index.*.third_party.acquia_cms_search: type: mapping label: 'Acquia CMS settings' mapping: @@ -6,3 +6,11 @@ search_api.index.*.third_party.acquia_cms_common: search_server: type: string label: 'Machine name of Search API server to which the index will be attached' + +node.type.*.third_party.acquia_cms_search: + type: mapping + label: 'Search settings' + mapping: + search_index: + type: string + label: 'Machine name of Search API index to which the index will be attached' diff --git a/modules/acquia_cms_search/src/Facade/SearchFacade.php b/modules/acquia_cms_search/src/Facade/SearchFacade.php index 6e37ab75f..1ecac4566 100644 --- a/modules/acquia_cms_search/src/Facade/SearchFacade.php +++ b/modules/acquia_cms_search/src/Facade/SearchFacade.php @@ -179,8 +179,7 @@ public function addTaxonomyField(FieldStorageConfigInterface $field_storage) { // Field storages don't normally have a human-readable label, so allow it to // provide one in its third-party settings. - $field_label = $field_storage->getThirdPartySetting('acquia_cms_common', 'search_label') ?: $field_storage->getLabel(); - + $field_label = $field_storage->getThirdPartySetting('acquia_cms_search', 'search_label') ?: $field_storage->getLabel(); $data_source_id = 'entity:' . $field_storage->getTargetEntityTypeId(); // This will throw an exception if the data source doesn't exist, so this // is really just a way to prevent the field from using an invalid data @@ -223,9 +222,7 @@ public function addFields(FieldStorageConfigInterface $field_storage) { if (empty($index)) { return; } - $field_name = $field_storage->getName(); - // Bail out if the field already exists on the index. if ($index->getField($field_name)) { return; @@ -234,7 +231,7 @@ public function addFields(FieldStorageConfigInterface $field_storage) { $field_type = $field_storage->getType(); // Field storages don't normally have a human-readable label, so allow it to // provide one in its third-party settings. - $field_label = $field_storage->getThirdPartySetting('acquia_cms_common', 'search_label') ?: $field_storage->getLabel(); + $field_label = $field_storage->getThirdPartySetting('acquia_cms_search', 'search_label') ?: $field_storage->getLabel(); $data_source_id = 'entity:' . $field_storage->getTargetEntityTypeId(); // This will throw an exception if the data source doesn't exist, so this @@ -286,7 +283,7 @@ private function loadIndexFromSettings(ThirdPartySettingsInterface $object) : ?I if ($this->configInstaller->isSyncing()) { return NULL; } - $index = $object->getThirdPartySetting('acquia_cms_common', 'search_index'); + $index = $object->getThirdPartySetting('acquia_cms_search', 'search_index'); return $index ? $this->indexStorage->load($index) : NULL; }