7

I have the following code in my custom block:

    public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);

    $config = $this->getConfiguration();

    $form['headline'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Block headline'),
      '#default_value' => isset($config['headline']) ? $config['headline'] : NULL,
      '#required' => TRUE,
    );


      return $form;
    }

and:

    public function blockSubmit($form, FormStateInterface $form_state) {
      $this->setConfigurationValue('headline', $form_state->getValue('headline'));
    }

The configuration is saved correctly, but only for English language.
The block translation doesn't show my variable (headline) in the translatable configuration.
What am I doing wrong?

Thanks

2

1 Answer 1

11

You have to provide a schema in /config/schema/mymodule.schema.yml of the type block_settings.

Example:

block.settings.example_configurable_text:
  type: block_settings
  label: 'Example configurable text block configuration'
  mapping:
    block_example_string:
      type: text
      label: 'Block contents'

From the example module https://git.drupalcode.org/project/examples/-/blob/HEAD/modules/block_example/config/schema/block_example.schema.yml

Configurations mapped as type: label (single line of text) or type: text (multiple lines of text) are translatable.

4
  • 2
    Thanks! Can I add it anytime after enabling the module? Or do I need to add these values through a hook_update_N()? Jun 27, 2017 at 20:15
  • 2
    the new schema yaml is discovered when you clear the cache
    – 4uk4
    Jun 27, 2017 at 20:21
  • 2
    We dedicated a blog post about Config Translation (schema, default value, UI) in Drupal 8 with a full example: antistatique.net/en/we/blog/2018/05/01/…
    – GiDo
    May 3, 2018 at 11:18
  • 1
    @GiDo great article! Can you advice: are theme_setting translatable? Cant make them work normal with translation :( Jul 31, 2019 at 19:46

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.