3

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

3

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 http://cgit.drupalcode.org/examples/tree/block_example/config/schema/block_example.schema.yml

Configuration values you map as type: label (single line of text) or type: text (multiple lines of text) are translatable.

  • 1
    Thanks! Can I add it anytime after enabling the module? Or do I need to add these values through a hook_update_N()? – Ismail Cherri Jun 27 '17 at 20:15
  • 1
    the new schema yaml is discovered when you clear the cache – 4k4 Jun 27 '17 at 20:21
  • 1
    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 '18 at 11:18
  • @GiDo great article! Can you advice: are theme_setting translatable? Cant make them work normal with translation :( – Mykola Mykolayovich Dolynskyi Jul 31 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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