3

What is the yml syntax in drupal .schema.yml files for specifying the same config for two entities? Something like this is what I need:

node.type.*.third_party.my_module:
media.type.*.third_party.my_module:
  type: mapping
  label: 'My Module'
  mapping:
    lots more lines here ...
    which I do not want to duplicate ...

I want the same settings for both node.type.* and media.type.* and there should be a way to avoid repeating all the definition and just having those two rows at the top.

3
  • I have tried a comma, semi-colon or just blanks, and tried the two on the same line. Can't find the right combination. Jan 4, 2021 at 17:53
  • 1
    Not sure if it works, but this is what I'd try: stackoverflow.com/q/4255311/6335551
    – sonfd
    Jan 4, 2021 at 23:07
  • Thanks @sonfd yes that does work and is perfect for avoiding duplication. For clarity, and to help others, the definition of the alias has to be on the same line as the first key. However, when it used the second time it can be on a new line after the second key. Jan 5, 2021 at 8:45

1 Answer 1

0

Solution

Here is a very good way to do this, with thanks to @sonfd for the link.

node.type.*.third_party.my_module: &third_party_settings_alias
  type: mapping
  label: 'My Module'
  mapping:
    lots more lines here ...
    which I do not want to duplicate ... 

# Use saved alias to repeat the same settings for media.type
media.type.*.third_party.my_module:
  *third_party_settings_alias

That's all there is to it. All the settings for node.type are repeated for media.type

1
  • Further info: If you want to reuse the alias again, I found (strangely) that putting a comment line before the second reuse causes error Invalid data type in config my_module.schema, found in file modules/my_module/config/schema/my_module.schema.yml : Unable to parse at line 116 (near "*third_party_settings_alias"). in Drupal\Core\Config\FileStorage->read() (line 118 of core/lib/Drupal/Core/Config/FileStorage.php). But removing the comment allows it to work just fine. May 29, 2021 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.