preprocess function unavailable for derived classes

Created on 25 October 2024, 2 months ago

Problem/Motivation

Ideally, extending the VertexAISearch plugin should be supported and be easy.

There is a preprocess function used to display a message on the search page if no keywords are specified or if no results are returned from a search. This preprocess function does not execute for derived classes. This preprocess function is specific to the VertexAISearch plugin, but should also be executed by classes that extend the VertexAISearch class.

In the vertex_ai_search.module file, the following hook is implemented:

/**
 * Implements hook_preprocess_item_list__search_results__vertex_ai_search().
 *
 * Overwrite the message for when there are no search results.
 */
function vertex_ai_search_preprocess_item_list__search_results__vertex_ai_search(&$variables) {

This hook is specific to the VertexAISearch plugin.

Also in the vertex_ai_search.module is this helper function:

function _vertex_ai_search_get_configuration(array &$variables) {

  $entity = \Drupal::service('current_route_match')->getParameter('entity');
  $configuration = [];

  if ($entity instanceof SearchPage) {

    $plugin = $entity->getPlugin()->getPluginId();
    
    if ($plugin == 'vertex_ai_search') {
      $configuration = $entity->getPlugin()->getConfiguration();
    }

  }

  return $configuration;
}

...this method includes logic that is specific to the 'vertex_ai_search' plugin.

Steps to reproduce

  1. Create a new plugin class that extends VertexAISearch plugin.
  2. Create a custom search page that uses the new plugin class. Make sure to configure it with custom messages for when there are no search results or when a search was performed with no keywords.
  3. Perform a search using this custom search page without keywords - the custom message does not appear.
  4. Perform a search that will return no results - the custom message does not appear.

Proposed resolution

Two things need to be modified:
1. Instead of implementing the 'vertex_ai_search_preprocess_item_list__search_results__vertex_ai_search' hook, implement the 'vertex_ai_search_preprocess_item_list__search_results' hook. This is not specific to a plugin.
2. Modify the '_vertex_ai_search_get_configuration' helper function so that it does not depend on the plugin being of type 'vertex_ai_search'. Instead, try checking to see if the plugin is an instance of the 'VertexAISearch' class.

πŸ› Bug report
Status

Active

Version

1.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States tzura

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024