From 2dcd9b0b6699a9f07d105f112f2c37a19b66d2dd Mon Sep 17 00:00:00 2001 From: rajeshreeputra Date: Tue, 31 Dec 2024 14:15:58 +0530 Subject: [PATCH] ACMS-4246: Add helper method to import view on fallback view creation. --- .../acquia_cms_search.install | 5 ++- .../acquia_cms_search.module | 38 ++++++++++++++----- .../install/search_api.index.content.yml | 7 +--- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/modules/acquia_cms_search/acquia_cms_search.install b/modules/acquia_cms_search/acquia_cms_search.install index b943f1eca..3b1a07bea 100644 --- a/modules/acquia_cms_search/acquia_cms_search.install +++ b/modules/acquia_cms_search/acquia_cms_search.install @@ -24,10 +24,11 @@ function acquia_cms_search_install($is_syncing) { // Retroactively enable indexing for any content types that existed before // this module was installed. - $node_types = \Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple(); + $entity_type_manager = \Drupal::entityTypeManager(); + $node_types = $entity_type_manager->getStorage('node_type')->loadMultiple(); array_walk($node_types, 'acquia_cms_search_node_type_insert'); array_walk($node_types, '_acquia_cms_search_import_content_type_views'); - $search_category = \Drupal::entityTypeManager()->getStorage('facets_facet')->loadByProperties(['id' => 'search_category']); + $search_category = $entity_type_manager->getStorage('facets_facet')->loadByProperties(['id' => 'search_category']); if (!$search_category) { _acquia_cms_search_add_category_facet(); } diff --git a/modules/acquia_cms_search/acquia_cms_search.module b/modules/acquia_cms_search/acquia_cms_search.module index d69011bd5..877004cd4 100644 --- a/modules/acquia_cms_search/acquia_cms_search.module +++ b/modules/acquia_cms_search/acquia_cms_search.module @@ -17,6 +17,7 @@ use Drupal\node\NodeTypeInterface; use Drupal\search_api\Entity\Index; use Drupal\search_api\ServerInterface; use Drupal\views\Entity\View; +use Drupal\views\ViewEntityInterface; /** * Implements hook_views_data(). @@ -132,16 +133,21 @@ function acquia_cms_search_field_config_insert(FieldConfigInterface $field_confi } /** - * Implements hook_entity_insert(). + * Implements hook_ENTITY_TYPE_insert() for node types. */ -function acquia_cms_search_entity_insert() { - // Normally, content is indexed immediately after it is created or modified, - // at the end of the current request. But that means content created - // programmatically (i.e., in the PHPUnit tests) are not being indexed. So, - // explicitly invoke the indexer whenever an entity is created. - $indexer = Drupal::service('search_api.post_request_indexing'); - if ($indexer instanceof DestructableInterface) { - $indexer->destruct(); +function acquia_cms_search_view_insert(ViewEntityInterface $view) { + $content_views = [ + 'articles_fallback' => 'article', + 'events_fallback' => 'event', + 'people_fallback' => 'person', + 'places_fallback' => 'place', + ]; + if(!in_array($view->id(), array_keys($content_views))) { + return; + } + if (!$view->isSyncing()) { + $node_type = \Drupal::entityTypeManager()->getStorage('node_type')->load($content_views[$view->id()]); + _acquia_cms_search_import_content_type_views($node_type); } } @@ -186,6 +192,20 @@ function _acquia_cms_search_import_content_type_views(NodeTypeInterface $node_ty } } +/** + * Implements hook_entity_insert(). + */ +function acquia_cms_search_entity_insert() { + // Normally, content is indexed immediately after it is created or modified, + // at the end of the current request. But that means content created + // programmatically (i.e., in the PHPUnit tests) are not being indexed. So, + // explicitly invoke the indexer whenever an entity is created. + $indexer = Drupal::service('search_api.post_request_indexing'); + if ($indexer instanceof DestructableInterface) { + $indexer->destruct(); + } +} + /** * Check if a configuration exists. * diff --git a/modules/acquia_cms_search/config/install/search_api.index.content.yml b/modules/acquia_cms_search/config/install/search_api.index.content.yml index ab47530fd..f75575eb8 100644 --- a/modules/acquia_cms_search/config/install/search_api.index.content.yml +++ b/modules/acquia_cms_search/config/install/search_api.index.content.yml @@ -1,5 +1,5 @@ langcode: en -status: false +status: true dependencies: config: - core.entity_view_mode.node.search_index @@ -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.' @@ -191,4 +188,4 @@ tracker_settings: options: cron_limit: 50 index_directly: true -server: '' +server: 'database'