- Issue created by @vlyalko
"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).
Preview the content with the "Search result excerpt" enabled in the display and if "Search result excerpt" not generated, it shows the warning
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,
];