Problem/Motivation
On a site with BAT units whose type (bundle) field is empty but unit_type_id is populated (numeric entity reference to bat_unit_type), calling bat_event_get_matching_units() with a machine name in $type_ids (e.g. ["room"]) triggers:
Warning: Undefined array key 5 in AbstractCalendar::itemizeHours() (line 295) Error: Call to a member function getDefaultValue() on null
The root cause is in bat_unit_load_multiple() (modules/bat_unit/bat_unit.module, around line 336):
if (!empty($unit_type_ids)) { $query->condition('unit_type_id', $unit_type_ids, 'IN'); }
unit_type_id is a numeric entity reference to bat_unit_type. Passing machine names (["room"]) or labels results in unit_type_id IN ("room"), which matches no rows. bat_event_get_calendar() then builds $keyed_units as an empty array, while $db_events contains rows for existing units (e.g. unit 5) whose calendar state tables have entries. itemizeHours() iterates over $db_events and calls $keyed_units[5]->getDefaultValue(), but $keyed_units[5] is null, causing a fatal error.
Steps to reproduce
- Have a BAT site where units have the
type(bundle) field empty butunit_type_idset (e.g.1forroom). - Ensure at least one unit has rows in
bat_event_availability_daily_day_state(or the equivalent state table for the event type in use). - Call:
$start = new \DateTime('+1 day'); $end = new \DateTime('+2 days'); bat_event_get_matching_units($start, $end, ['bee_daily_available'], ['room'], 'availability_daily'); - Observe the warning
Undefined array key Xand the fatalCall to a member function getDefaultValue() on null.
Proposed resolution
Normalize $unit_type_ids to numeric IDs before building the query, accepting numeric IDs, machine names, or labels. For example:
if (!empty($unit_type_ids)) { $numeric_ids = array_filter($unit_type_ids, 'is_numeric'); if (empty($numeric_ids)) { $storage = \Drupal::entityTypeManager()->getStorage('bat_unit_type'); foreach ($storage->loadMultiple() as $ut) { if (in_array($ut->label(), $unit_type_ids, TRUE) || in_array($ut->get('type')->value ?? NULL, $unit_type_ids, TRUE)) { $numeric_ids[] = $ut->id(); } } } if (!empty($numeric_ids)) { $query->condition('unit_type_id', $numeric_ids, 'IN'); } }
Alternatively, bat_event_get_calendar() could convert machine names to numeric IDs before calling bat_unit_load_multiple(), but fixing the helper is preferable because it is also used by other callers (e.g. bat_unit_entity_delete()).
Remaining tasks
- Write patch / open merge request.
- Add test coverage for
bat_unit_load_multiple()with machine name input.
User interface changes
None.
API changes
None (behavior fix only).
Data model changes
None.
Comments
Comment #2
afagioliRelated issues: #3576440: Units with no DB calendar rows are invisible to getEventsItemized(), breaking availability queries
Comment #4
afagioliCommitted to 11.1.x:
- `bat_unit_load_multiple()`: normalize `$unit_type_ids` to numeric `bat_unit_type` IDs (accepts numeric IDs, machine names, or labels); return an empty array when no valid type is provided, instead of dropping the filter and returning all units.
- `AbstractCalendar`: add defensive guards in `itemizeDays()`, `itemizeHours()`, `itemizeMinutes()` and `getEventsNormalized()` to skip units not present in the calendar; make `getUnit()` return `NULL` instead of throwing when the unit is missing.
Comment #5
afagioli