Skip to content

Commit

Permalink
ACMS-4246: Add helper method to import view on node type creation and…
Browse files Browse the repository at this point in the history
… update.
  • Loading branch information
rajeshreeputra committed Dec 30, 2024
1 parent 0c160f9 commit 9f16153
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions modules/acquia_cms_search/acquia_cms_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 9f16153

Please sign in to comment.