【问题标题】:How to programmatically query the translated content of a block in drupal9如何以编程方式查询drupal 9中块的翻译内容
【发布时间】:2022-01-18 03:52:31
【问题描述】:

我创建了一个区块,默认语言是英文,翻译语言是中文。现在我在我的代码中查询这个块的内容:

$block = \Drupal::entityTypeManager()->getStorage('block_content')
      ->loadByProperties([
        'info' => $info,
        'langcode' => 'zh-hant'
      ]);

但我得到的仍然是英文,我做错了什么?

【问题讨论】:

    标签: drupal


    【解决方案1】:

    您必须在没有 langcode 条件的情况下加载block_content 实体,然后通过getTranslation() 获得您需要的语言的翻译:

    $blocks = \Drupal::entityTypeManager()->getStorage('block_content')
      ->loadByProperties([
        'info' => $info
      ]);
    $translated_blocks = array_map(function ($block) {
      return $block->getTranslation('zh-hant');
    }, $blocks);
    // do somthing with $translated_blocks
    

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 2021-12-24
      • 2015-12-27
      相关资源
      最近更新 更多