Problem/Motivation
The expected behavior is that the new ConfigEntityAdapter::getTypedDataManager() will return the 'config.typed' service. The output above shows that Drupal\Core\TypedData\TypedDataTrait::setTypedDataManager() has already set the value to TypedDataManager, not TypedConfigManager.
Proposed resolution
Introduce a new protected method getTypedConfigManager() for the cases where the specific subclass MUST be available.
Additionally, prefer calls to $data->getTypedDataManager() where possible.
Remaining tasks
N/A
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Original summary
It looks like #1818574: Support config entities in typed data EntityAdapter has side-effects for existing config entity types that are not covered by the change record https://www.drupal.org/node/2954182.
Specifically, when running Domain after this patch was introduced, the provided domain context throws an exception because the wrong TypedDataManager is called. Debugging output follows.
#1 /Applications/MAMP/htdocs/domain8/core/lib/Drupal/Core/TypedData/TypedDataManager.php(92): Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter->setTypedDataManager(Object(Drupal\Core\TypedData\TypedDataManager))
#2 /Applications/MAMP/htdocs/domain8/core/lib/Drupal/Core/TypedData/TypedDataManager.php(103): Drupal\Core\TypedData\TypedDataManager->createInstance('entity:domain', Array)
#3 /Applications/MAMP/htdocs/domain8/core/lib/Drupal/Core/Plugin/Context/Context.php(101): Drupal\Core\TypedData\TypedDataManager->create(Object(Drupal\Core\Entity\TypedData\EntityDataDefinition), Object(Drupal\domain\Entity\Domain))
#4 /Applications/MAMP/htdocs/domain8/core/lib/Drupal/Core/Plugin/Context/Context.php(52): Drupal\Core\Plugin\Context\Context->setContextValue(Object(Drupal\domain\Entity\Domain))
#5 /Applications/MAMP/htdocs/domain8/modules/domain/domain/src/ContextProvider/CurrentDomainContext.php(43): Drupal\Core\Plugin\Context\Context->__construct(Object(Drupal\Core\Plugin\Context\ContextDefinition), Object(Drupal\domain\Entity\Domain))
#6 /Applications/MAMP/htdocs/domain8/modules/domain/domain/src/ContextProvider/CurrentDomainContext.php(62): Drupal\domain\ContextProvider\CurrentDomainContext->getRuntimeContexts(Array)
This occurs when trying to Place Block on the block overview page.
The expected behavior is that the new ConfigEntityAdapter::getTypedDataManager() will return the 'config.typed' service. The output above shows that Drupal\Core\TypedData\TypedDataTrait::setTypedDataManager() has already set the value to TypedDataManager, not TypedConfigManager.
I can't tell if this is a core bug or a documentation issue.
| Comment | File | Size | Author |
|---|---|---|---|
| #38 | 2982183-38.patch | 762 bytes | phenaproxima |
| #31 | 2982183-typedconfig-31-interdiff.txt | 2.29 KB | tim.plunkett |
| #31 | 2982183-typedconfig-31.patch | 5.56 KB | tim.plunkett |
| #30 | interdiff-2.txt | 1.21 KB | wim leers |
| #30 | interdiff-1.txt | 826 bytes | wim leers |
Comments
Comment #2
agentrickardBumping to critical as an API-breaking change.
The issue seems to be here in Drupal\Core\Plugin\Context\Context::setContextValue():
This method is not tied into the new ConfigEntityAdapter class, and so the call to $this->getTypedDataManager() returns the default manager defined in Drupal\Core\TypedData\TypedDataTrait, which is not compatible with config entities.
Comment #3
agentrickardEven more confusing, this error doesn't seem to affect the core NodeType context, which is the only core example of a config entity context.
So something is missing from the documentation, but I can't tell what.
The only difference I can see is that NodeType extends ConfigEntityBundleBase whereas Domain extends ConfigEntityBase.
Comment #4
berdirThere are two context classes, maybe you are using the one from Drupal\Components? Just a random guess.
Comment #5
agentrickardNo. That's not it. I essentially copied my context/condition code from Node module very early in the D8 cycle.
Something has changed in core, but I can't figure out what.
Comment #6
agentrickardComment #7
berdir> Even more confusing, this error doesn't seem to affect the core NodeType context, which is the only core example of a config entity context.
No, there is no NodeType context, only a NodeType condition which needs a node context which is a content entity :) That's the difference.
Confirmed that this is indeed a bug and regression.
It's not the call that you pointed out though, that's a completely different object. What seems to happen is that the adapter is created through \Drupal\Core\TypedData\TypedDataManager::createInstance(), which explicitly calls setTypedDataManager().
I don't know why this overlap exists and why we use the same property/methods for different services. That seems easy to fix, but then it dies on the next error, that might be more problematic:
Mabe the original issue needs to be reverted if this isn't an easy fix as well, as it looks like this isn't sufficiently tested yet? It *could* be missing/wrong config schema in domain.module, but I assume you're testing that and we shouldn't be failing like that even then.
Comment #8
berdirComment #10
berdirTrying to use a better issue title.
This is a bug and regression, definitely, but it's not really an API change, just things being messed up internally or so.
Comment #11
agentrickardI ran into that second error when trying to fix things as well. Reviewed the schema file and unsure what might be missing/wrong about it.
https://cgit.drupalcode.org/domain/tree/domain/config/schema/domain.sche...
Comment #12
berdirYeah, core failed on a similar error now, so I don't think it's you...
Comment #13
agentrickard:-D
Comment #14
agentrickardThere's definitely an issue in
Drupal\Core\Plugin\Context\Context::getContextDatawhich assumes the default typed data service.Comment #15
agentrickard@berdir
I found the issue, which may be self-inflicted.
With the change to typed data loading, it looks like the separation of concerns in these two methods is now strictly enforced.
In earlier Drupal 8 versions,
getAvailableContexts()could returngetRuntimeContexts()without issue. Now it creates a loop that fails.Correcting the definition of
Drupal\domain\ContextProvider::getAvailableContexts()makes this code work without the patch.See https://github.com/agentrickard/domain/pull/435/files
Comment #16
agentrickardI'm reducing the status of this issue. I don't think it's a release blocker but I do think it's a DX issue.
Comment #18
johnpicozziWas having similar issues to above. Applied patch in #8 and the error messages changed. I am using the Domain module and applied the solution in #15 and removed the patch and everything appears to be working correctly. Using Drupal 8.6.0 & Domain 8.x-1.0-alpha13
@agentrickard Do you have a patch for this fix some place? or is it suggested to use the dev version of Domain?
Comment #19
johnpicozziThinking this should be moved to the Domain module issue queue, but relevant here as well.
Used the following to resolve the problem.
https://patch-diff.githubusercontent.com/raw/agentrickard/domain/pull/43...
Comment #20
alexpottSo we have several reports of this in the wild... see #2997807: Can't place blocks to regions after upgrade to 8.6.0
I think it does make sense to attempt a core fix.
Comment #21
tim.plunkettI am hitting this bug (and the bug in the failing test above) when trying to validate a config entity.
drush ev "\Drupal::typedDataManager()->getDefinition('uuid');"will always fatal, because whileuuidis defined as a schema type, it has no corresponding DataType plugin.drush ev "\Drupal::service('config.typed')->getDefinition('uuid');"will work correctly, because it traverses the schema to know thatuuidisstringunderneath (as defined in core.data_types.schema.yml).I'm not sure the best way to know when to use which one, but this hack solves my problem.
Comment #22
tim.plunkettIn practice, that try/catch could be replaced by
$value = $data->getTypedDataManager()->getCanonicalRepresentation($data);But that's not a method on the interface, it just happens to exist.
Comment #23
tim.plunkettHere's an easy way to reproduce this:
drush ev "\Drupal\Core\Plugin\Context\EntityContext::fromEntity(entity_get_display('node', 'article', 'default'))->validate();"Comment #24
tim.plunkettNeeds tests
Comment #25
phenaproximaAsk and ye shall receive.
Comment #27
tim.plunkettThanks! Cleaned up that test a bit, as well as swapping the try/catch for a method_exists.
Comment #29
tim.plunkettIMO this is ready.
Comment #30
wim leersI read the entire issue and dug into the code. It does seem a bit brittle that we'd be picking the service injected into a value object over the one we'd be using from the DIC depending … but it is the most elegant solution possible unless we'd merge
TypedConfigManager(added in #2277945: Typed configuration implements TypedDataInterface improperly) intoTypedDataManagerso that there is only The One True TypedDataManager. But not only would that be a more disruptive change, it would also be huge in scope.This is one of the places where it's apparent that the Typed Data API and Configuration (Schema) API were developed in parallel. It's not up to this issue to unify them. This patch fixes a major bug, so let's get it in.
Except …
class ConfigEntityAdapter extends EntityAdapter extends TypedData, which usesTypedDataTrait, which containspublic function getTypedDataManager() {…}. This method is actually overriding that method (on that trait which is used two levels up the inheritance chain). Renaming this method and explicitly calling it means that other calls togetTypedDataManager()now are operating on the "non-config" typed data manager rather than the typed data manager.Interdiffs attached for either option. The latter appears to be the correct one. Yes this is confusing, but through no fault of this issue; it's because of the subtly complex overrides that
TypedConfigManagerintroduced.Comment #31
tim.plunkettThanks @Wim Leers!
I find the current architecture to be very confusing, and that suggestion is the only one I've heard that would make sense. But as you said, that's a lot more work.
So I opened #3011137: Merge TypedDataManager and TypedConfigManager.
And I took the theme of your interdiffs and went one step further.
If
getTypedConfigManager()has never been called, it will attempt to use the result of getTypedDataManager(), so as to respect any injecting done by setTypedDataManager().If
getTypedDataManager()has never been called and nothing has been injected, it will default to \Drupal::service('config.typed')if
setTypedDataManager()is called and an instance of TypedConfigManager is passed, it will update the result of getTypedConfigManager() as well.This is needed to preserve BC with this bizarre duplication intact.
(I've also made getTypedConfigManager() protected, as there is no need to spread that into the public API).
Comment #32
wim leersComment #33
alexpottThis stopgap approach looks good to me. A huge +1 for something along the lines of #3011137: Merge TypedDataManager and TypedConfigManager we've been edging towards making config less special with respect to typed data for a while.
I've committed this to 8.7.x - I just want another committer opinion before backporting to 8.6.x
Committed e23949a and pushed to 8.7.x. Thanks!
Comment #35
alexpottDiscussed with @catch we agreed that as this has affect the domain module and live site that we should backport it - even though domain as a workaround.
Comment #37
neclimdulYour handy "KernelTests" aren't UnitTests notification. Needs follow up.
Comment #38
phenaproximaHere's a patch moving the test to the Drupal\KernelTests\Core\Plugin namespace.
Comment #39
neclimdulLGTM, thanks!
Comment #41
alexpottCommitted and pushed 6c8526b0a9 to 8.7.x and 1af77f317e to 8.6.x. Thanks!