Issue #2981889 by Nebel54, acbramley, timodwhit, bkosborne, fago, josephdpurcell, tim.plunkett, eiriksm, Sam152, grantkruger, shaal, gnikolovski: Performance Degradation in Layout Builder and other places likely

(cherry picked from commit 57aabd2b7b)
8.7.x
Lee Rowlands 2019-10-11 10:42:47 +10:00
parent fd1ad573e9
commit e9181e7f66
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
2 changed files with 50 additions and 1 deletions

View File

@ -2,6 +2,7 @@
namespace Drupal\Core\TypedData\Validation;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\ListInterface;
use Drupal\Core\TypedData\TypedDataInterface;
@ -136,6 +137,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface {
// constraint validators, such that they do not have to care about Typed
// Data.
$value = $typed_data_manager->getCanonicalRepresentation($data);
$constraints_given = isset($constraints);
$this->context->setNode($value, $data, $metadata, $property_path);
if (isset($constraints) || !$this->context->isGroupValidated($cache_key, Constraint::DEFAULT_GROUP)) {
@ -148,7 +150,10 @@ class RecursiveContextualValidator implements ContextualValidatorInterface {
// If the data is a list or complex data, validate the contained list items
// or properties. However, do not recurse if the data is empty.
if (($data instanceof ListInterface || $data instanceof ComplexDataInterface) && !$data->isEmpty()) {
// Next, we do not recurse if given constraints are validated against an
// entity, since we should determine whether the entity matches the
// constraints and not whether the entity validates.
if (($data instanceof ListInterface || $data instanceof ComplexDataInterface) && !$data->isEmpty() && !($data instanceof EntityAdapter && $constraints_given)) {
foreach ($data as $name => $property) {
$this->validateNode($property);
}

View File

@ -0,0 +1,44 @@
<?php
namespace Drupal\KernelTests\Core\TypedData;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
/**
* @coversDefaultClass \Drupal\Core\TypedData\Validation\RecursiveContextualValidator
* @group Validation
*/
class RecursiveContextualValidatorTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'entity_test',
'user',
];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test');
}
/**
* Tests recursive validation against given constraints against an entity.
*/
public function testRecursiveValidate() {
$entity = EntityTest::create();
$adapter = EntityAdapter::createFromEntity($entity);
// This would trigger the ValidReferenceConstraint due to EntityTest
// defaulting uid to 1, which doesn't exist. Ensure that we don't get a
// violation for that.
$this->assertCount(0, \Drupal::typedDataManager()->getValidator()->validate($adapter, $adapter->getConstraints()));
}
}