Unverified Commit 6d5382e4 authored by alexpott's avatar alexpott

Issue #2982183 by tim.plunkett, phenaproxima, Berdir, Wim Leers, agentrickard:...

Issue #2982183 by tim.plunkett, phenaproxima, Berdir, Wim Leers, agentrickard: Conflict between TypedDataManager and TypedConfigManager in ConfigEntityAdapter

(cherry picked from commit 34f1a358)
parent e721b52c
......@@ -2,7 +2,9 @@
namespace Drupal\Core\Entity\Plugin\DataType;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\Core\TypedData\TypedDataManagerInterface;
/**
* Enhances EntityAdapter for config entities.
......@@ -16,6 +18,13 @@ class ConfigEntityAdapter extends EntityAdapter {
*/
protected $entity;
/**
* The typed config manager.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected $typedConfigManager;
/**
* {@inheritdoc}
*/
......@@ -68,10 +77,31 @@ public function getIterator() {
}
/**
* Gets the typed data manager.
* Gets the typed config manager.
*
* @return \Drupal\Core\Config\TypedConfigManagerInterface
* The typed data manager.
* The typed config manager.
*/
protected function getTypedConfigManager() {
if (empty($this->typedConfigManager)) {
// Use the typed data manager if it is also the typed config manager.
// @todo Remove this in https://www.drupal.org/node/3011137.
$typed_data_manager = $this->getTypedDataManager();
if ($typed_data_manager instanceof TypedConfigManagerInterface) {
$this->typedConfigManager = $typed_data_manager;
}
else {
$this->typedConfigManager = \Drupal::service('config.typed');
}
}
return $this->typedConfigManager;
}
/**
* {@inheritdoc}
*
* @todo Remove this in https://www.drupal.org/node/3011137.
*/
public function getTypedDataManager() {
if (empty($this->typedDataManager)) {
......@@ -81,6 +111,19 @@ public function getTypedDataManager() {
return $this->typedDataManager;
}
/**
* {@inheritdoc}
*
* @todo Remove this in https://www.drupal.org/node/3011137.
*/
public function setTypedDataManager(TypedDataManagerInterface $typed_data_manager) {
$this->typedDataManager = $typed_data_manager;
if ($typed_data_manager instanceof TypedConfigManagerInterface) {
$this->typedConfigManager = $typed_data_manager;
}
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -97,7 +140,7 @@ public function applyDefaultValue($notify = TRUE) {
* The typed data.
*/
protected function getConfigTypedData() {
return $this->getTypedDataManager()->createFromNameAndData($this->entity->getConfigDependencyName(), $this->entity->toArray());
return $this->getTypedConfigManager()->createFromNameAndData($this->entity->getConfigDependencyName(), $this->entity->toArray());
}
}
......@@ -126,10 +126,16 @@ protected function validateNode(TypedDataInterface $data, $constraints = NULL, $
$metadata = $this->metadataFactory->getMetadataFor($data);
$cache_key = spl_object_hash($data);
$property_path = $is_root_call ? '' : PropertyPath::append($previous_path, $data->getName());
// Prefer a specific instance of the typed data manager stored by the data
// if it is available. This is necessary for specialized typed data objects,
// for example those using the typed config subclass of the manager.
$typed_data_manager = method_exists($data, 'getTypedDataManager') ? $data->getTypedDataManager() : $this->typedDataManager;
// Pass the canonical representation of the data as validated value to
// constraint validators, such that they do not have to care about Typed
// Data.
$value = $this->typedDataManager->getCanonicalRepresentation($data);
$value = $typed_data_manager->getCanonicalRepresentation($data);
$this->context->setNode($value, $data, $metadata, $property_path);
if (isset($constraints) || !$this->context->isGroupValidated($cache_key, Constraint::DEFAULT_GROUP)) {
......
<?php
namespace Drupal\Tests\Core\Plugin\Context;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the interaction between entity context and typed data.
*
* @group Context
*/
class EntityContextTypedDataTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['entity_test'];
/**
* Tests that entity contexts wrapping a config entity can be validated.
*/
public function testValidateConfigEntityContext() {
$display = EntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
]);
$display->save();
$violations = EntityContext::fromEntity($display)->validate();
$this->assertCount(0, $violations);
}
}
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