1

How do I properly translate custom content block titles?

Problem:
I've got a custom content block with just one CKeditor text field. I want to show the custom block title and the text field. When I place that block, the PLACED block title is copied from (but not referencing) the custom content block.

When a content translator later translates that custom content block, the PLACED block title is wrong/untranslated. Is there a way to reference the correctly translated, custom content block title in block placement? E.g. using tokens?

NOT allowed solution:
Giving translators access to block placement to translate the block placement is an absolute no-go. They are only allowed to use custom block library.

Unwanted workaround:
I know I could create an additional fake-title field and disable "Show title" in block placement. But I really hope I can avoid this, or I would have to modify all my existing block templates.

1 Answer 1

5

To use the title from the custom content block entity (not the title from the block placement config):

function mymodule_preprocess_block (&$variables) {
  if ($variables ['elements'] ['#base_plugin_id'] == 'block_content') {
    $variables ['label'] = $variables ['content'] ['#block_content'] ->label ();
  }
}

This also respects translations and does not require access to block placement for translators.

2
  • 1
    Not relevant to my question. I already have the translated block. The problem is that the translated block title is not used by block placement.
    – Hudri
    May 9, 2018 at 9:43
  • 1
    Thanks, the preprocessing was the key. I added a small if-condition to respect "Show title" setting if ($variables['label']) { $variables['label'] = $variables['content']['#block_content']->label(); }
    – Hudri
    May 9, 2018 at 9:59

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.