Problem/Motivation
Thanks to #2293773: Field allowed values use dots in key names - not allowed in config, the Drupal configuration system does not allow periods (a.k.a. "dots") to appear in any configuration key. Thus, a perfectly reasonable static_map usage to translate source strings to allowed_values:
field_cpu_bus_type:
plugin: static_map
source: BusType
bypass: true
map:
'PCIe 3.0': pcie_3_0
PCI: pci
...
Fails with
exception 'Drupal\Core\Config\ConfigValueException' with message 'PCIe 3.0 key contains a dot which is not supported.' in /var/www/example/docroot/core/lib/Drupal/Core/Config/ConfigBase.php:211
Here's a thought - permit map entries to be specified such that the map key is not a YAML key but rather a YAML value. E.g.:
field_cpu_bus_type:
plugin: static_map
source: BusType
bypass: true
map:
-
key: 'PCIe 3.0'
value: pcie_3_0
PCI: pci
...
Steps to reproduce
Proposed resolution
Add test of configuration key with a dot
Update the StaticMap documentation accordingly.
Remaining tasks
Update the patch
Review commit.
User interface changes
API changes
Data model changes
Release notes snippet
Comments
Comment #2
mikeryanComment #3
mikeryanDocumented at https://www.drupal.org/docs/8/api/migrate-api/migrate-process/process-pl....
Comment #5
osopolarOne workaround would be to first pipe the value through the machine_name plugin (but don't forget to rewrite the complete source value as the plugin will run the value over the transliteration service, lowercases it, replaces anything that's not a number or a letter with an underscore and removes duplicate underscores):
I added the workaround to the documentation.
Comment #7
socketwench commentedIt's important to note that the above workaround will not work if your static_map is using bypass and you need to preserve the dots. This is a problem when working with emails, since you will have no way to filter out some emails while preserving others.
Comment #13
xurizaemonFor mapping (say) email addresses in a user migration, this did the trick:
And in the yaml only
mail: - plugin: email_map source: mailPrefer to not do purpose-specific plugins, but it does the trick!
Comment #16
danflanagan8For any interested parties, I just added a new process plugin to Migrate Conditions that can be used to work around this static_map limitation pretty easily. The example is in the docs here.
It would look like this for the case in the IS.
Slightly verbose, but not bad.
Comment #17
danflanagan8We discussed this at the 10/20/2022 migrate meeting and it looks like this is truly a bug in the migrate_plus module. The core migrate framework does not rely on config entities, migrate_plus does.
I'm attaching a couple patches here. The first adds a little test coverage to core to show that this bug does not exist in core. The second is a patch for migrate_plus that exposes the bug there. When I run it locally I get this error:
Pretty damning!
Not sure if this issue should be moved to migrate_plus or if it should instead be closed, with a brand new issue on migrate_plus.
Comment #18
joelpittetSorry triggered tests, yes probably should move to the migrate_plus module
Comment #19
danflanagan8I was looking through the docblock on static_map today and noticed this hunk at the end:
I guess we should remove that since it's not true.
Comment #20
mikelutzThis report and that documentation were both probably true when they were written. Core migrate used to store migrations in config early in D8 development, but they were switched to plugins a long time ago.
Comment #21
quietone commented@danflanagan8, thanks for the testing!
Let's use this issue to add the new test and update the documentation for StaticMap. The title needs an update too.
Cheers.
Comment #23
mikelutzKeeping the test that shows this works, and changing the documentation in the plugin.
Comment #24
benjifisherThanks, @danflanagan8 and @mikelutz. The patch looks good.
Just to be sure that the test covers what we think it does, I hacked StaticMap and added
As expected, the test failed with my new error message.
I am updating the title, as @quietone suggested in #21.
I will also add an issue for the
migrate_plusmodule.Comment #26
mikelutzThe patch didn't fail testing, The testing failed the patch. 😐
Comment #27
quietone commentedI keep wondering why these have been changed. These test modules are not used for testing the static map plugin. I think these should be removed. Or have I missed something?
We have a pattern of adding documentation at the top of the plugin doc block, before all the examples so this should move to after the available keys section. See Substr.php.
And since I've asked for a change we should also make this text easier to read. I had a go at using plain language as follows. This is only a suggestion.
Comment #28
danflanagan8Regarding @quietone's 27.1
I added those changes to prove that a dot doesn't cause any problems when static map is used in yaml as opposed to simply called in a unit test. It looks like I edited those particular migrations because those are the test migrations that use static_map.
I agree with that. This is getting to be a pretty long time ago, but I don't think I ever intended for those changes to get committed. The main goal of my patches in #17 were to offer definitive proof that the problem is in migrate_plus rather than core. Using a dot in a migrate (yaml) plugin is fine, while using a dot in a migrate_plus migration config entity is not fine. I probably should have somehow indicated in the patch name that it was a "do-not-commit" patch or something. :)
I think we could revert those changes without replacement. I don't think we typically have tests for process plugin that would use a dedicated yaml, do we? I took a quick look right now and can't find any examples.
Comment #30
quietone commentedConvert to MR then made changes I suggested in #27 and @danflanagan8 agreed with in #28. I then made an attempt to simplify the new paragraph in staticmap.
Comment #31
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #32
quietone commentedComment #34
smustgrave commentedAdded nitpicky change for :void to test, didn't break anything.
Appears feedback has been addressed. Comment block reads well to me and leaning on quietone's expertise with the migration system.
Comment #35
alexpottI think the migrate_plus module should contain a fix for this as it is responsible for saving the static map to config. Essentially it is not be careful enough - as the comment says.
The change here for core looks good.
Committed and pushed 4da76f6b37 to 11.x and a1e704d78d to 10.3.x. Thanks!