Consider the following code:

if ($entity->__isset('field_related_country')) {
      $countries = $entity->get('field_related_country')->getIterator();
      $count = $countries->count();
      for ($i = 0; $i < $count; $i++) {
        $urlString = $build['field_related_country'][$i]['#url']->toString();

and look at the resulting dump of objects in the attached screenshot. A null value is I believe counted as an item resulting in a call to member function on null.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

richard.c.allen2386 created an issue. See original summary.

richard.c.allen2386’s picture

If this IS a bug and not incorrect usage, and the proposed fix would to just add validation to each key on that method, I'm happy to propose a patch. I'd just like to know I'm not the issue or there is another issue in the works.

Version: 8.2.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Lendude’s picture

Status: Active » Closed (works as designed)
Issue tags: +Bug Smash Initiative

This came up as the daily triage target of the bug smash initiative.

Discussed this with @marcvangent on slack and we agree that as far as we can tell the handling of NULL values should be the responsibility of the implementing code. And if we would filter away NULL values this should not be done by the iterator but as a change to the Itemlist otherwise the iterator and itemlist would not have the same number of results.

Closing this as works as designed for now, feel free to re-open this if you feel that this does require a fix.