Drupal 8.1.10 translated custom block title not displayed, only display the default value.

Steps I did:

  • Make custom blocks translatable
  • add custom block
  • translate that block
  • placed the block to a region set display title

Result
switching language only body field displayed in different translation and the default title only displayed

Comments

mhmd created an issue. See original summary.

make77’s picture

To display the title translation on custom blocks, I had to do this:

function my_module_preprocess_block(&$variables) {
  if ($variables['elements']['#base_plugin_id'] == 'block_content') {
    $variables['label'] = $variables['content']['#block_content']->label();
  }
}
niyon’s picture

Status: Active » Needs review
bahuma20’s picture

#2 works for me.

I just have added a check for empty titles to do not display these:

function my_theme_preprocess_block(&$variables) {
  if ($variables['elements']['#base_plugin_id'] == 'block_content') {
    if ($variables['label'] !== '') {
      $variables['label'] = $variables['content']['#block_content']->label();
    }
  }
}
Rizwan Siddiquee’s picture

#4 works for me

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

brayfe’s picture

#4 worked for me as well. I just don't understand why that isn't the normal behavior within core when viewing a translated block.

Version: 8.1.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

fengtan’s picture

Tried #4 as well -- this works as expected.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
vangelisp’s picture

Just wanted to say that #4 worked for me as well ! Many thanks all!

quietone’s picture

Category: Support request » Bug report
Status: Needs review » Closed (duplicate)
Issue tags: +Bug Smash Initiative

Thanks for the patches!

Tested on standard install of Drupal 9.4.x and this is still happening. This is a bug, so changing category.

This is a duplicate of #3226452: Block title is displayed in interface language, block content in content language. Should be content language. and even though that is later it has more discussion of the problem so I am closing this as a duplicate. And adding credit over there.

Thanks!

mvpfi’s picture

As of Drupal 9.4.5 this is still happening. While it puzzles me why, #4 fixes this issue just like that.

ressa’s picture

In my case, it was because the blocks were from a View, and they are considered configuration, see #3549927: Streamline translation of Views block titles, custom menu titles, etc.: Allow Interface Translation.