vertex_ai_search-1.0.0-beta4/vertex_ai_search.install
vertex_ai_search.install
<?php
/**
* @file
* Vertex AI Search installation file.
*/
/**
* Implements hook_requirements().
*/
function vertex_ai_search_requirements($phase) {
if ($phase == 'install') {
// Check if the Google Cloud Discovery Engine Search Service Client Exists.
// This will attempt to autoload class, so we can bail out ASAP.
if (!class_exists('Google\Cloud\DiscoveryEngine\V1\Client\SearchServiceClient')) {
return [
'upgrade_status' => [
'title' => t('Vertex AI Search dependencies'),
'description' => t('Google DiscoveryEngine Client Libraries are not available.'),
'severity' => REQUIREMENT_ERROR,
'value' => t('Not available'),
],
];
}
}
}
/**
* Granting Vertex search page access for anonymous and authenticated roles.
*/
function vertex_ai_search_update_10001() {
$permissions = [];
$searchPageRepository = \Drupal::service('search.search_page_repository');
// Generate permissions for each Vertex AI Search Plugin page.
$searchPages = $searchPageRepository->getActiveSearchPages();
/** @var \Drupal\user\Entity\Role $anonymousRole **/
$anonymousRole = \Drupal::entityTypeManager()->getStorage('user_role')->load('anonymous');
/** @var \Drupal\user\Entity\Role $authRole **/
$authRole = \Drupal::entityTypeManager()->getStorage('user_role')->load('authenticated');
foreach ($searchPages as $searchPage) {
if ($searchPage->get('plugin') === 'vertex_ai_search') {
$id = $searchPage->id();
$permission = "use $id custom search page";
$anonymousRole->grantPermission($permission);
$authRole->grantPermission($permission);
$anonymousRole->save();
$authRole->save();
}
}
}
