Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
Screenshot 2017-02-14 11.50.11.png | 968.55 KB | richard.c.allen2386 |
Comments
Comment #2
richard.c.allen2386 CreditAttribution: richard.c.allen2386 at Bixal commentedIf 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.
Comment #4
LendudeThis 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.