From 9f16153873482eec3627b9077f736b0ae202b77f Mon Sep 17 00:00:00 2001 From: rajeshreeputra Date: Mon, 30 Dec 2024 15:21:44 +0530 Subject: [PATCH] ACMS-4246: Add helper method to import view on node type creation and update. --- .../acquia_cms_search.module | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/modules/acquia_cms_search/acquia_cms_search.module b/modules/acquia_cms_search/acquia_cms_search.module index e604147c0..a1867f4b2 100644 --- a/modules/acquia_cms_search/acquia_cms_search.module +++ b/modules/acquia_cms_search/acquia_cms_search.module @@ -10,11 +10,13 @@ use Drupal\acquia_cms_search\Facade\FacetFacade; use Drupal\acquia_cms_search\Facade\SearchFacade; use Drupal\acquia_cms_search\Plugin\views\query\SearchApiQuery; use Drupal\acquia_search\Helper\Runtime; +use Drupal\Core\Config\FileStorage; use Drupal\Core\DestructableInterface; use Drupal\field\FieldConfigInterface; use Drupal\node\NodeTypeInterface; use Drupal\search_api\Entity\Index; use Drupal\search_api\ServerInterface; +use Drupal\views\Entity\View; /** * Implements hook_views_data(). @@ -68,6 +70,7 @@ function acquia_cms_search_node_type_insert(NodeTypeInterface $node_type) { if (!$node_type->isSyncing()) { _acquia_cms_search_add_category_facet(); } + _acquia_cms_search_import_content_type_views($node_type); } /** @@ -78,6 +81,7 @@ function acquia_cms_search_node_type_update(NodeTypeInterface $node_type) { if (!$node_type->isSyncing()) { _acquia_cms_search_add_category_facet(); } + _acquia_cms_search_import_content_type_views($node_type); } /** @@ -123,6 +127,27 @@ function acquia_cms_search_entity_insert() { } } +/** + * Import views for content types. + */ +function _acquia_cms_search_import_content_type_views(NodeTypeInterface $node_type){ + $content_views = [ + 'article' => 'articles', + 'event' => 'events', + 'person' => 'people', + 'place' => 'places', + ]; + // Load the view. + $view = View::load($content_views[$node_type->id()]); + if (!$view) { + // import the view. + $source = new FileStorage(__DIR__ . '/config/optional'); + $entity_storage = \Drupal::entityTypeManager()->getStorage('view'); + $entity = $entity_storage->createFromStorageRecord($source->read('views.view.' . $content_views[$node_type->id()])); + $entity->save(); + } +} + /** * Implements hook_ENTITY_TYPE_insert() for Search API servers. */