5

In the block_example module, from the Drupal 8 Examples for Developers, what purpose does the file block_example.schema.yml serve?

I'm familiar with using schema.yml files to set configuration and access it like so:

$config = \Drupal::config('system.maintenance');

However, it appears that the "block_settings" type is different.

1 Answer 1

5

That's basically config schema inheritance, like you have class inheritance in PHP.

Any type that you define can be used as by by someone else. And it is what you want to do when you extend the class (usually a plugin) for which it is defined.

block_settings itself is a type that extends from mapping and defines a few common keys, it is defined in core.data_types.schema.yml

block_settings:
  type: mapping
  label: 'Block settings'
  mapping:
    id:
      type: string
      label: 'ID'
    ...

(Multiple keys in there shouldn't exit and are for example block_content specific, but that's a bit complicated to fix now).

And if you'd provide a block plugin that would extend from example_configurable_text, then you can define it like this:

block.settings.my_block:
  type: block.settings.example_configurable_text
  mapping:
    another_thing:
      type: text
      label: 'Another thing'

And you automatically get everything it defines, and what it itself inherits from block_settings.

2
  • Thanks, @Berdir. I'm still wondering, though, how this inheritance plays out. In block_example.schema.yml, block.settings.example_configurable_text is of type block_settings, which has various keys like label and info. However, when run \Drupal::config('block.block.titleoffirstblockexample_configurable_text')->get('label') the result is null. If this block inherits from block_settings, I would expect to see a value or at least know how to set it. What am I missing?
    – cole
    Commented Feb 10, 2017 at 16:27
  • block_settings define the block settings. try settings.label and so on. Also not all those keys exist all the time. the block itself is defined in block.schema.yml, and its settings is then block.settings.[%parent.plugin], so its gets its schema dynamically depending on the value of the type. See hojtsy.hu/blog/2014-dec-12/… for more information
    – Berdir
    Commented Feb 10, 2017 at 18:38

Your Answer

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

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