Problem/Motivation
The EntityAdapter class now assumes that all entities have a getIterator() method, which is not documented in the EntityInterface and not provided for Config Entities.
As as result, many Drupal pages (e.g. configure block) return a fatal error due to a missing method on a Config Entity that interacts with that page.
The website encountered an unexpected error. Please try again later.
Error: Call to undefined method Drupal\domain\Entity\Domain::getIterator() in Drupal\Core\Entity\Plugin\DataType\EntityAdapter->getIterator() (line 170 of core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php).
Drupal\Core\Entity\Plugin\DataType\EntityAdapter->getIterator() (Line: 146)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object, Array, 1) (Line: 99)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate(Object, Array, NULL) (Line: 90)
Drupal\Core\TypedData\Validation\RecursiveValidator->validate(Object, Array) (Line: 285)
Drupal\Core\Plugin\Context\ContextDefinition->isSatisfiedBy(Object) (Line: 46)
Drupal\Core\Plugin\Context\ContextHandler->Drupal\Core\Plugin\Context\{closure}(Object)
Proposed resolution
Rewrite the method to understand that getIterator() is not required.
/**
* {@inheritdoc}
*/
public function getIterator() {
return isset($this->entity) ? $this->entity->getIterator() : new \ArrayIterator([]);
}
If we don't make this change, then the EntityInterface and base classes must be updated.
Remaining tasks
Provide a patch.
User interface changes
None.
API changes
None.
Data model changes
None
Comments
Comment #2
agentrickardComment #3
agentrickardAnd a patch.
Comment #4
agentrickardComment #5
agentrickardBumping to critical since this is a BC break from 8.4 to 8.5, likely introduced by #2671964: ContextHandler cannot validate constraints
Comment #6
tim.plunkettEven simpler:
PHPStorm flags the code in HEAD:
#1818574: Support config entities in typed data EntityAdapter seems to suggest that config entities aren't even supported?
And EntityAdapter::get/set link to that issue and have explicit checks for FieldableEntityInterface...
Yet EntityDeriver happily returns all entity types?
That's confusing.
Ideally this would be testable.
Currently \Drupal\Tests\Core\Entity\TypedData\EntityAdapterUnitTest cheats and does
Comment #7
agentrickardTim's version of the code also fixes the issue.
I'll see about a test case in a bit.
Comment #8
agentrickardPatch for failing test case.
Comment #9
agentrickardPatch for working test case.
Comment #10
tim.plunkettNice! Assuming #8 fails and #9 passes as expected, this looks good to go.
@agentrickard mentioned that https://www.drupal.org/u/hbensalem is the one who found this first, can a committer add credit for them?
Comment #13
alexpottAdding issue credit
Comment #14
alexpottCommitted and pushed 32157c7c5d to 8.6.x and 8830ce72bb to 8.5.x. Thanks!