1

I created a module that installs a block with some fields when installed. I added a schema file, and altered the blockSubmit() function so that the translation is saved in the schema.

Here is my module.schema.yml:

block.settings.worldwide:
  type: block_settings
  label: 'Worldwide'
  mapping:
    overtitle:
      type: label
      label: 'Overtitle'

Here is the blockSubmit() function from my Block plugin:

public function blockSubmit($form, FormStateInterface $form_state) {
    parent::blockSubmit($form, $form_state);
    $values = $form_state->getValues();
    $block_name = $this->getPluginId();

    $this->blockConfigFactory->setBlockConfig($block_name, 'overtitle', $values['overtitle']);
    $this->setConfigurationValue('overtitle', $values['overtitle']);
}

When saving, I have no error, and can translate the field, which will keep the translation when updated. So everything seems to work. Additionnally, the website displays correctly what I saved for the default language.

Now, let's say that the default language is english and the alternative is french. My issue is that if I set the website in french, the default (english) value will keep displaying.

However, I can't figure out a way to displayed the saved translation when I want to look at the block in a language that has the block's translation saved. I have no idea where to look, and can't seem to find relevant stuff on internet.

Thank you in advance

  • Take a look at this thread on drupal.org and the module translated_config. – Hudri Aug 13 at 16:09
  • "Now, let's say that the default language is english and the alternative is french. My issue is that if I set the website in french, the default (english) value will keep displaying. " Displaying where? The block form or the rendered block? – 4k4 Aug 13 at 16:59
  • My rendered block, in my template – Jaeger Aug 13 at 17:03
  • OK, the translation form is not built by the plugin, Drupal builds it independently for all configuration values which are translatable according to the schema. You can also translate configuration objects in exported yaml files, which I think would be a good starting point for debugging. – 4k4 Aug 13 at 17:50
  • Is it possible retrieve the saved translation from the configuration ? Like, loading "blocks.settings.worldwide"? I tried to do that but I couldn't find a way to retrieve my data – Jaeger Aug 13 at 18:06
0

Little did I know, the translation was correctly loaded, but not used in the twig files. Both the fields' content and configuration were loaded, were content was ie an image and a link, and configuration the field's translation.

Somehow, still, the configuration needed several cache rebuilds to show up, but then went ok and kept being updated if I made any modifications.

The twig files were using variables such as {{ content['field_worldwide_text'] }}, so I needed it to use config[] instead.

Additional notes for future self, here is how to load content and configuration:

$content = $this->blockConfigFactory->getBlockConfig($this->getPluginId());
$config = $this->getConfiguration();
| improve this answer | |

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.