Skip to content
Snippets Groups Projects
Commit 73fe8ab8 authored by Timothy Zura's avatar Timothy Zura
Browse files

Issue #3450494 by TimoZura: Provide support for promoted results

parent 8b4aa914
No related branches found
No related tags found
1 merge request!9Issue #3450494 by TimoZura: Provide support for promoted results
Pipeline #184640 passed
<?php
namespace Drupal\vertex_ai_search\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a VertexSearchResultsPlugin type annotation object.
*
* VertexSearchResultsPlugin classes define plugins that can
* manipulate a page of results on a Vertex AI Search page.
*
* @see VertexSearchResultsPluginBase
*
* @ingroup vertex_ai_search
*
* @Annotation
*/
class VertexSearchResultsPlugin extends Plugin {
/**
* A unique identifier for the vertex search results plugin.
*
* @var string
*/
public $id;
/**
* The title for the vertex search results plugin.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $title;
/**
* Plugin weight.
*
* @var int
*/
public $weight;
}
...@@ -11,6 +11,7 @@ use Drupal\Core\Utility\Token; ...@@ -11,6 +11,7 @@ use Drupal\Core\Utility\Token;
use Drupal\search\Plugin\ConfigurableSearchPluginBase; use Drupal\search\Plugin\ConfigurableSearchPluginBase;
use Drupal\search\SearchPageRepositoryInterface; use Drupal\search\SearchPageRepositoryInterface;
use Drupal\vertex_ai_search\VertexAutocompletePluginManager; use Drupal\vertex_ai_search\VertexAutocompletePluginManager;
use Drupal\vertex_ai_search\VertexSearchResultsPluginManager;
use Google\ApiCore\ApiException; use Google\ApiCore\ApiException;
use Google\Cloud\DiscoveryEngine\V1\Client\SearchServiceClient; use Google\Cloud\DiscoveryEngine\V1\Client\SearchServiceClient;
use Google\Cloud\DiscoveryEngine\V1\SearchRequest; use Google\Cloud\DiscoveryEngine\V1\SearchRequest;
...@@ -42,6 +43,13 @@ class VertexAISearch extends ConfigurableSearchPluginBase { ...@@ -42,6 +43,13 @@ class VertexAISearch extends ConfigurableSearchPluginBase {
*/ */
protected $autoPluginManager; protected $autoPluginManager;
/**
* Vertex Search Results Plugin Manager.
*
* @var \Drupal\vertex_ai_search\VertexSearchResultsPluginManager
*/
protected $resultsPluginManager;
/** /**
* The token service. * The token service.
* *
...@@ -76,6 +84,8 @@ class VertexAISearch extends ConfigurableSearchPluginBase { ...@@ -76,6 +84,8 @@ class VertexAISearch extends ConfigurableSearchPluginBase {
* Repository for the search page. * Repository for the search page.
* @param \Drupal\vertex_ai_search\VertexAutocompletePluginManager $autoPluginManager * @param \Drupal\vertex_ai_search\VertexAutocompletePluginManager $autoPluginManager
* Vertex Autocomplete Plugin Manager. * Vertex Autocomplete Plugin Manager.
* @param \Drupal\vertex_ai_search\VertexSearchResultsPluginManager $resultsPluginManager
* Vertex Search Results Plugin Manager.
* @param \Drupal\Core\Utility\Token $tokenManager * @param \Drupal\Core\Utility\Token $tokenManager
* For managing Tokens. * For managing Tokens.
* @param \Drupal\Core\Pager\PagerManagerInterface $pagerManager * @param \Drupal\Core\Pager\PagerManagerInterface $pagerManager
...@@ -89,6 +99,7 @@ class VertexAISearch extends ConfigurableSearchPluginBase { ...@@ -89,6 +99,7 @@ class VertexAISearch extends ConfigurableSearchPluginBase {
array $plugin_definition, array $plugin_definition,
SearchPageRepositoryInterface $searchPageRepository, SearchPageRepositoryInterface $searchPageRepository,
VertexAutocompletePluginManager $autoPluginManager, VertexAutocompletePluginManager $autoPluginManager,
VertexSearchResultsPluginManager $resultsPluginManager,
Token $tokenManager, Token $tokenManager,
PagerManagerInterface $pagerManager, PagerManagerInterface $pagerManager,
ModuleExtensionList $moduleExtensionList, ModuleExtensionList $moduleExtensionList,
...@@ -96,6 +107,7 @@ class VertexAISearch extends ConfigurableSearchPluginBase { ...@@ -96,6 +107,7 @@ class VertexAISearch extends ConfigurableSearchPluginBase {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->searchPageRepository = $searchPageRepository; $this->searchPageRepository = $searchPageRepository;
$this->autoPluginManager = $autoPluginManager; $this->autoPluginManager = $autoPluginManager;
$this->resultsPluginManager = $resultsPluginManager;
$this->tokenManager = $tokenManager; $this->tokenManager = $tokenManager;
$this->pagerManager = $pagerManager; $this->pagerManager = $pagerManager;
$this->moduleExtensionList = $moduleExtensionList; $this->moduleExtensionList = $moduleExtensionList;
...@@ -112,6 +124,7 @@ class VertexAISearch extends ConfigurableSearchPluginBase { ...@@ -112,6 +124,7 @@ class VertexAISearch extends ConfigurableSearchPluginBase {
$plugin_definition, $plugin_definition,
$container->get('search.search_page_repository'), $container->get('search.search_page_repository'),
$container->get('plugin.manager.vertex_autocomplete'), $container->get('plugin.manager.vertex_autocomplete'),
$container->get('plugin.manager.vertex_search_results'),
$container->get('token'), $container->get('token'),
$container->get('pager.manager'), $container->get('pager.manager'),
$container->get('extension.list.module') $container->get('extension.list.module')
...@@ -275,6 +288,19 @@ class VertexAISearch extends ConfigurableSearchPluginBase { ...@@ -275,6 +288,19 @@ class VertexAISearch extends ConfigurableSearchPluginBase {
]; ];
} }
// Retrieve any VertexSearchResultPlugins and modified page results.
$resultsPluginDefinitions = $this->resultsPluginManager->getDefinitions();
foreach ($resultsPluginDefinitions as $pluginKey => $pluginDefinition) {
$resultsPlugin = $this->resultsPluginManager->createInstance(
$pluginKey
);
$output = $resultsPlugin->modifyPageResults($this->getKeywords(), $output, $this->configuration['id']);
}
// Use the lesser of total results and total results limit. // Use the lesser of total results and total results limit.
$totResults = $totResults =
($results['totalResults'] < $this->configuration['totalResultsLimit']) ($results['totalResults'] < $this->configuration['totalResultsLimit'])
...@@ -301,9 +327,9 @@ class VertexAISearch extends ConfigurableSearchPluginBase { ...@@ -301,9 +327,9 @@ class VertexAISearch extends ConfigurableSearchPluginBase {
return; return;
} }
$form['#attributes']['class'][] = 'vertex-ai-search-box-form'; $form['#attributes']['class'][] = 'vertex-ai-search-search-box-form';
$form['#attributes']['id'] = 'vertex-ai-search-box-form'; $form['#attributes']['id'] = 'vertex-ai-search-search-box-form';
$form['#theme'] = 'vertex_ai_search_page_form'; $form['#theme'] = 'vertex_ai_search_search_page_form';
// Search term element. // Search term element.
$form['keys'] = [ $form['keys'] = [
......
<?php
namespace Drupal\vertex_ai_search\Plugin;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
/**
* Provides base implementation for a Vertex Search Results plugin.
*/
abstract class VertexSearchResultsPluginBase extends PluginBase implements VertexSearchResultsPluginInterface, ContainerFactoryPluginInterface, RefinableCacheableDependencyInterface {
use RefinableCacheableDependencyTrait;
/**
* {@inheritdoc}
*/
public function modifyPageResults(string $keyword, array $searchResults, string $search_page_id) {
return $searchResults;
}
}
<?php
namespace Drupal\vertex_ai_search\Plugin;
/**
* Provides an interface for a Vertex Search Results plugin.
*/
interface VertexSearchResultsPluginInterface {
/**
* Returns a modified array of search results.
*
* @param string $keyword
* String used for search query.
* @param array $searchResults
* The search results of a search page.
* @param string $search_page_id
* The id of the custom search page.
*
* @return array
* An array of manipulated results.
*/
public function modifyPageResults(string $keyword, array $searchResults, string $search_page_id);
}
<?php
namespace Drupal\vertex_ai_search;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* VertexSearchResults plugin manager.
*/
class VertexSearchResultsPluginManager extends DefaultPluginManager {
/**
* Constructs VertexSearchResultsPluginManager.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct(
'Plugin/SearchResults',
$namespaces,
$module_handler,
'Drupal\vertex_ai_search\Plugin\VertexSearchResultsPluginInterface',
'Drupal\vertex_ai_search\Annotation\VertexSearchResultsPlugin'
);
$this->setCacheBackend($cache_backend, 'vertex_search_results_plugins');
$this->alterInfo('vertex_search_results_plugin');
}
}
...@@ -131,10 +131,10 @@ function vertex_ai_search_theme($existing, $type, $theme, $path) { ...@@ -131,10 +131,10 @@ function vertex_ai_search_theme($existing, $type, $theme, $path) {
'file' => 'vertex_ai_search.theme.inc', 'file' => 'vertex_ai_search.theme.inc',
'template' => 'vertex_ai_search_spelling_correction', 'template' => 'vertex_ai_search_spelling_correction',
], ],
'vertex_ai_search_page_form' => [ 'vertex_ai_search_search_page_form' => [
'render element' => 'form', 'render element' => 'form',
'file' => 'vertex_ai_search.theme.inc', 'file' => 'vertex_ai_search.theme.inc',
'template' => 'vertex_ai_search_page_form', 'template' => 'vertex_ai_search_search_page_form',
], ],
]; ];
......
...@@ -2,3 +2,6 @@ services: ...@@ -2,3 +2,6 @@ services:
plugin.manager.vertex_autocomplete: plugin.manager.vertex_autocomplete:
class: Drupal\vertex_ai_search\VertexAutocompletePluginManager class: Drupal\vertex_ai_search\VertexAutocompletePluginManager
parent: default_plugin_manager parent: default_plugin_manager
plugin.manager.vertex_search_results:
class: Drupal\vertex_ai_search\VertexSearchResultsPluginManager
parent: default_plugin_manager
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment