Problem/Motivation
Sometimes we index base fields, and these fields have bundle - null, because of this code in the EntityFieldManager :
// Automatically set the field name, target entity type and bundle
// for non-configurable fields.
foreach ($base_field_definitions as $field_name => $base_field_definition) {
if ($base_field_definition instanceof BaseFieldDefinition) {
$base_field_definition->setName($field_name);
$base_field_definition->setTargetEntityTypeId($entity_type_id);
$base_field_definition->setTargetBundle(NULL);
}
}
If we look at the code in the search_api.views.inc file, namely line 462, we can notice that we get the bundle through getTargetBundle(), which sometimes returns NULL if it is a base field.
// If this is a normal Field API field, dynamically retrieve the options
// list at query time.
$field_definition = $data_definition->getFieldDefinition();
$bundle = $field_definition->getTargetBundle();
$field_name = $field_definition->getName();
$entity_type = $field_definition->getTargetEntityTypeId();
$definitions['filter']['options callback'] = '_search_api_views_get_allowed_values';
$definitions['filter']['options arguments'] = [$entity_type, $bundle, $field_name];
But this can lead to unexpected problems, for example, the elementary hook_entity_bundle_field_info may have a $bundle argument of type string. Thus, a fatal error will be thrown because a string is expected, but NULL is provided. This is what happened in my case
Steps to reproduce
1) Create a search index, and add it to index several base fields
2) Create the view based on this search index
3) Add filter by indexed base field
4) Add hook_entity_bundle_field_info with string $bundle argument
5) You will get an error, like: Argument #2 ($bundle) must be type of string, null given
Proposed resolution
I suggest avoiding having the bundle be NULL. We can always check if there is a bundle, if not, then use entity_type, for example:
$entity_type = $field_definition->getTargetEntityTypeId();
$bundle = $field_definition->getTargetBundle() ?? $entity_type;