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-4246: Recipe - Acquia Drupal Starter Kit Search #1910

Closed
wants to merge 2 commits into from
Closed
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
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
20 changes: 4 additions & 16 deletions modules/acquia_cms_search/acquia_cms_search.install
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,17 @@ function acquia_cms_search_install($is_syncing) {
// Add permission to the following roles,
// so that search autocomplete functionality is accessible.
// This will also fix search box design issue if site studio being use.
user_role_grant_permissions('anonymous', [
'use search_api_autocomplete for search',
]);
user_role_grant_permissions('authenticated', [
'use search_api_autocomplete for search',
]);

// Retroactively enable indexing for any content types that existed before
// this module was installed.
$node_types = NodeType::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);
foreach ($node_types as $nodes) {
_acquia_cms_search_add_category_facet($nodes);
}
}
}

/**
* 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
101 changes: 74 additions & 27 deletions modules/acquia_cms_search/acquia_cms_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
*/

use Drupal\acquia_cms_search\Facade\AcquiaSearchFacade;
use Drupal\acquia_cms_search\Facade\FacetFacade;

use Drupal\acquia_cms_search\Facade\SearchFacade;
use Drupal\acquia_cms_search\Facade\FacetFacade;
use Drupal\acquia_cms_search\Plugin\views\query\SearchApiQuery;
use Drupal\acquia_search\Helper\Runtime;
use Drupal\block\Entity\Block;
use Drupal\Core\DestructableInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldConfigInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\ServerInterface;
use Drupal\node\Entity\NodeType;

/**
* Implements hook_views_data().
Expand Down Expand Up @@ -63,24 +67,45 @@ function acquia_cms_search_views_plugins_cache_alter(&$definitions) {
/**
* Implements hook_ENTITY_TYPE_insert() for node types.
*/
function acquia_cms_search_node_type_insert(NodeTypeInterface $node_type) {
function acquia_cms_search_node_type_insert(NodeType $node_type) {
Drupal::classResolver(SearchFacade::class)->addNodeType($node_type);
}

/**
* Get the datasources of a search index.
*
* @param string $index_id
* The ID of the search index.
*
* @return array
* An array of datasource plugin IDs.
*/
function getSearchIndexDatasources($index_id) {
$index = Index::load($index_id);
if ($index) {
$datasources = $index->getDatasources();
$datasource_ids = [];
foreach ($datasources as $datasource) {
$datasource_ids[] = $datasource->getPluginId();
}
return $datasource_ids;
}
return [];
}

/**
* Implements hook_ENTITY_TYPE_insert() for configurable field.
*/
function acquia_cms_search_field_config_insert(FieldConfigInterface $field_config) {
/** @var \Drupal\Core\Field\FieldConfigBase $field_storage */
$field_storage = $field_config->getFieldStorageDefinition();

// 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() === 'text_with_summary' && $field_storage->id() === 'node.body') {
// $field_storage->setThirdPartySetting('acquia_cms_search', 'search_index', 'content')
// ->setThirdPartySetting('acquia_cms_search', '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,12 +161,12 @@ 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();
}
}
Expand All @@ -151,7 +176,7 @@ function acquia_cms_search_search_api_server_insert(ServerInterface $server) {
* Implements hook_config_schema_info_alter().
*/
function acquia_cms_search_config_schema_info_alter(array &$definitions) {
$key = 'node.type.*.third_party.acquia_cms_common';
$key = 'node.type.*.third_party.acquia_cms_search';
// 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()
Expand Down Expand Up @@ -180,31 +205,53 @@ 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
* @param NodeType $nodes
* 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)) {
\Drupal::classResolver(FacetFacade::class)->addFacet([
function _acquia_cms_search_add_category_facet(NodeType $nodes) {
if (!FieldStorageConfig::loadByName('node', 'field_categories')) {
return;
}

// Check if search_category facet already exists.
$existing_facets = \Drupal::service('entity_type.manager')->getStorage('facets_facet')->load('search_category');
if (!empty($existing_facets)) {
// Add search_category facet if it doesn't exist.
Drupal::classResolver(FacetFacade::class)->addFacet([
'id' => 'search_category',
'name' => 'Category',
'url_alias' => 'category',
'field_identifier' => 'field_categories',
]);

// Add search_category block if it doesn't exist.
$block = Block::load('search_category');
if (empty($block)) {
$block = Block::create([
'id' => 'search_category',
'theme' => 'olivero',
'region' => 'sidebar',
'weight' => 0,
'plugin' => 'facet_block:search_category',
'settings' => [
'id' => 'facet_block:search_category',
'label' => 'Category',
'label_display' => 'visible',
'provider' => 'facets',
'block_id' => 'search_category',
],
'visibility' => [
'request_path' => [
'id' => 'request_path',
'negate' => false,
'pages' => '/search',
],
],
]);
$block->save();
}
}
}
33 changes: 0 additions & 33 deletions modules/acquia_cms_search/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,11 @@
"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",
"drupal/facets_pretty_paths": "^2.0",
"drupal/search_api": "^1.32",
"drupal/search_api_autocomplete": "^1.7"
},
"require-dev": {
"drupal/acquia_cms_tour": "^2"
},
"repositories": {
"assets": {
"type": "composer",
"url": "https://asset-packagist.org"
},
"drupal": {
"type": "composer",
"url": "https://packages.drupal.org/8"
}
},
"config": {
"allow-plugins": {
"composer/installers": true,
"cweagans/composer-patches": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"drupal/core-composer-scaffold": true,
"ergebnis/composer-normalize": true,
"oomphinc/composer-installers-extender": true,
"phpro/grumphp-shim": true,
"webdriver-binary/binary-chromedriver": true
}
},
"extra": {
"branch-alias": {
"dev-develop": "1.x-dev"
},
"enable-patching": true,
"patches": {}
}
}
Loading
Loading