Skip to content

Commit

Permalink
ACMS-4246: Refactor Acquia Starter Kit Search to work with recipe.
Browse files Browse the repository at this point in the history
  • Loading branch information
rajeshreeputra committed Dec 27, 2024
1 parent 8cee154 commit 6921df3
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 103 deletions.
1 change: 0 additions & 1 deletion modules/acquia_cms_search/acquia_cms_search.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 5 additions & 11 deletions modules/acquia_cms_search/acquia_cms_search.install
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
60 changes: 18 additions & 42 deletions modules/acquia_cms_search/acquia_cms_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -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().
*/
Expand All @@ -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',
Expand Down
2 changes: 0 additions & 2 deletions modules/acquia_cms_search/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ dependencies:
- search_api.index.content
- views.view.search_fallback
module:
- acquia_cms_search
- search_api
- user
id: search
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
search_api.index.*.third_party.acquia_cms_common:
search_api.index.*.third_party.acquia_cms_search:
type: mapping
label: 'Acquia CMS settings'
mapping:
# @see acquia_cms_search_search_api_server_insert()
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'
9 changes: 3 additions & 6 deletions modules/acquia_cms_search/src/Facade/SearchFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 6921df3

Please sign in to comment.