Autocomplete breaking on core search block form for derived classes

Created on 5 November 2024, 20 days ago

Problem/Motivation

I have created a derived class from the VertexAISearch plugin class.

When I use this as the default search page and as the search page to be used by the core search block form, the autocomplete stops working on the custom search block.

Look at the following code in the vertex_ai_search.module file:

/**
 * Implements hook_form_search_block_form_alter().
 */
function vertex_ai_search_form_search_block_form_alter(&$form, &$form_state) {

  // Get the active search pages.
  $pages = \Drupal::service('search.search_page_repository')->getActiveSearchPages();
  $action = str_replace("/search/", "", $form['#action']);

  foreach ($pages as $key => $page) {

    // Only applies to vertex_ai_search plugin.
    if ($page->get('plugin') !== 'vertex_ai_search') {
      continue;
    }

    // Compare search page path to form action.
    if ($page->getPath() == $action) {
      $configuration = $page->get('configuration');

      // Determine if Autocomplete should be enabled or not.
      if (!empty($configuration['autocomplete_enable']) &&
        !empty($configuration['autocomplete_enable_block']) &&
        !empty($configuration['autocomplete_source'])) {
        $form['keys']['#autocomplete_route_name'] = 'vertex_ai_search.autocomplete';
        $form['keys']['#autocomplete_route_parameters'] = ['search_page_id' => $key];
      }

    }

  }

}

... there is a conditional in this method that checks to see if the plugin is of type, 'vertex_ai_search'. The problem is that a plugin that is derived from vertex_ai_search will not be able to use the autocomplete.

Proposed resolution

Remove the conditional that is checking for the content type.
The logic later in the method that checks for the existence of 'autocomplete_enable', 'autocomplete_enable_block', and 'autocomplete_source' will provide an ample check as to whether or not the autocomplete functionality should be added.

πŸ› Bug report
Status

Active

Version

1.3

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