Problem/Motivation
In \Drupal\hal\Normalizer\EntityReferenceItemNormalizer::normalize() it's possible that we're dealing with an entity that does not exist. I.e $target_entity = $field_item->get('entity')->getValue();
returns NULL. In PHP versions prior to 7.4 we've soldiered on and returned an array anyways.
#3086374: Make Drupal 8 & 9 compatible with PHP 7.4 fixes it to continue the same behaviour as before but this is maintaining a broken code path. Once $target_entity
is null there is no point calling $embedded = $this->serializer->normalize($target_entity, $format, $context);
and the return value of
return [
'_links' => [
$field_uri => [$link],
],
'_embedded' => [
$field_uri => [$embedded],
],
];
Is probably meaningless because $link is an array with 1 item - a langocde and $embedded is NULL.
To reproduce the error remove the fix from #3086374: Make Drupal 8 & 9 compatible with PHP 7.4 and run Drupal\Tests\node\Functional\Hal\NodeHalJsonAnonTest
Proposed resolution
Fix more appropriately.