Warning: Undefined array key "#search_api_excerpt" in search_api_entity_view()

Created on 27 January 2025, 2 days ago

Problem/Motivation

"Search result excerpt" field is shown in the display by default and if it is null, the warning is shown
Warning: Undefined array key "#search_api_excerpt" in search_api_entity_view() (line 671 of modules/contrib/search_api/search_api.module).

Steps to reproduce

Preview the content with the "Search result excerpt" enabled in the display and if "Search result excerpt" not generated, it shows the warning

Proposed resolution

In search_api.module, there is already a check if $build['#search_api_excerpt'] is set: $excerpt = $build['#search_api_excerpt'] ?? '';
But the '#markup' value is still set to the $build['#search_api_excerpt'] instead of the value of the $excerpt variable

Current code:

/**
 * Implements hook_entity_view().
 */
function search_api_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $excerpt_component = $display->getComponent('search_api_excerpt');
  if ($excerpt_component !== NULL) {
    $excerpt = $build['#search_api_excerpt'] ?? '';
    $has_excerpt = strlen($excerpt) > 0;
    $build['search_api_excerpt'] = [
      '#theme' => 'search_api_excerpt',
      '#excerpt' => [
        '#type' => 'markup',
        '#markup' => $build['#search_api_excerpt'] ,
      ],
      '#cache' => [
        'contexts' => ['url.query_args'],
      ],
      '#markup' => $excerpt,
      '#access' => $has_excerpt,
    ];

Proposed code:

/**
 * Implements hook_entity_view().
 */
function search_api_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $excerpt_component = $display->getComponent('search_api_excerpt');
  if ($excerpt_component !== NULL) {
    $excerpt = $build['#search_api_excerpt'] ?? '';
    $has_excerpt = strlen($excerpt) > 0;
    $build['search_api_excerpt'] = [
      '#theme' => 'search_api_excerpt',
      '#excerpt' => [
        '#type' => 'markup',
        '#markup' => $excerpt,
      ],
      '#cache' => [
        'contexts' => ['url.query_args'],
      ],
      '#markup' => $excerpt,
      '#access' => $has_excerpt,
    ];

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States vlyalko

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

Comments & Activities

Production build 0.71.5 2024