Commit 506d92fc authored by larowlan's avatar larowlan

Issue #3043087 by tedbow, Sam152: Retrieving plugins with entity context...

Issue #3043087 by tedbow, Sam152: Retrieving plugins with entity context definitions from cache is expensive, which is noticeable when used with Layout Builder's FieldBlockDeriver
parent e192b14d
......@@ -12,7 +12,6 @@ class ContextDefinition implements ContextDefinitionInterface {
use DependencySerializationTrait {
__sleep as traitSleep;
__wakeup as traitWakeup;
}
use TypedDataTrait;
......@@ -243,6 +242,7 @@ public function setDefaultValue($default_value) {
*/
public function getConstraints() {
// If the backwards compatibility layer is present, delegate to that.
$this->initializeEntityContextDefinition();
if ($this->entityContextDefinition) {
return $this->entityContextDefinition->getConstraints();
}
......@@ -256,6 +256,7 @@ public function getConstraints() {
*/
public function getConstraint($constraint_name) {
// If the backwards compatibility layer is present, delegate to that.
$this->initializeEntityContextDefinition();
if ($this->entityContextDefinition) {
return $this->entityContextDefinition->getConstraint($constraint_name);
}
......@@ -269,6 +270,7 @@ public function getConstraint($constraint_name) {
*/
public function setConstraints(array $constraints) {
// If the backwards compatibility layer is present, delegate to that.
$this->initializeEntityContextDefinition();
if ($this->entityContextDefinition) {
$this->entityContextDefinition->setConstraints($constraints);
}
......@@ -282,6 +284,7 @@ public function setConstraints(array $constraints) {
*/
public function addConstraint($constraint_name, $options = NULL) {
// If the backwards compatibility layer is present, delegate to that.
$this->initializeEntityContextDefinition();
if ($this->entityContextDefinition) {
$this->entityContextDefinition->addConstraint($constraint_name, $options);
}
......@@ -351,6 +354,7 @@ public function isSatisfiedBy(ContextInterface $context) {
$values = [$context->getContextData()];
}
elseif ($definition instanceof self) {
$this->initializeEntityContextDefinition();
if ($this->entityContextDefinition) {
$values = $this->entityContextDefinition->getSampleValues();
}
......@@ -402,6 +406,7 @@ protected function getSampleValues() {
*/
protected function getConstraintObjects() {
// If the backwards compatibility layer is present, delegate to that.
$this->initializeEntityContextDefinition();
if ($this->entityContextDefinition) {
return $this->entityContextDefinition->getConstraintObjects();
}
......@@ -424,17 +429,6 @@ public function __sleep() {
return array_diff($this->traitSleep(), ['entityContextDefinition']);
}
/**
* Implements magic __wakeup() method.
*/
public function __wakeup() {
$this->traitWakeup();
if (strpos($this->getDataType(), 'entity:') === 0) {
$this->initializeEntityContextDefinition();
}
}
/**
* Initializes $this->entityContextDefinition for backwards compatibility.
*
......@@ -444,14 +438,16 @@ public function __wakeup() {
* @deprecated
*/
private function initializeEntityContextDefinition() {
$this->entityContextDefinition = EntityContextDefinition::create()
->setDataType($this->getDataType())
->setLabel($this->getLabel())
->setRequired($this->isRequired())
->setMultiple($this->isMultiple())
->setDescription($this->getDescription())
->setConstraints($this->getConstraints())
->setDefaultValue($this->getDefaultValue());
if (!$this instanceof EntityContextDefinition && strpos($this->getDataType(), 'entity:') === 0 && !$this->entityContextDefinition) {
$this->entityContextDefinition = EntityContextDefinition::create()
->setDataType($this->getDataType())
->setLabel($this->getLabel())
->setRequired($this->isRequired())
->setMultiple($this->isMultiple())
->setDescription($this->getDescription())
->setConstraints($this->constraints)
->setDefaultValue($this->getDefaultValue());
}
}
}
......@@ -79,7 +79,9 @@ protected function setUp() {
* @expectedDeprecation Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use Drupal\Core\Plugin\Context\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.
*/
public function testSerialization() {
$this->definition->addConstraint('foo');
$definition = unserialize(serialize($this->definition));
$this->assertEquals(['foo' => NULL], $definition->getConstraints());
$bc_layer = $this->compatibilityLayer->getValue($definition);
$this->assertInstanceOf(EntityContextDefinition::class, $bc_layer);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment