快轉到主要內容

Drupal 9 多語言站點解決 Paragraphs 字段在custom block 中I18翻譯無法正確顯示問題

180 字1 分鐘00
作者Y Cheung
目錄

Drupal 9 多語言站點啟用 Paragraphs 模組,如果在 custom block 中使用了 paragraphs 類型的字段 ,會遇到字段內文字的翻譯無法顯示問題。該 paragraphs 字段的內容始終加載的是 default 的語言內容(通常是英文),需要在custom module 代碼中使用 hook_preprocess_block() 修改一下。

環境設定:#

  • Drupal v9.3.3
  • Paragraphs v1.12.0

代碼如下:#

 1// web/modules/custom/custom_module/custom_module.module
 2
 3/**
 4 * Implements hook_preprocess_HOOK().
 5 */
 6function custom_module_preprocess_block(&$variables) {
 7  if ($variables['base_plugin_id'] == 'block_content' && isset($variables['content']['field_paragraphs'])) {
 8    $lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
 9    if (count($variables['content']['field_paragraphs']['#items'])) {
10      foreach ($variables['content']['field_paragraphs']['#items'] as $item) {
11        if ($item->entity->hasTranslation($lang)) {
12          $item->entity = $item->entity->getTranslation($lang);
13        }
14      }
15    }
16  }
17}

Reference#

分享

相關文章

drupal 7中操作遠程oracle數據庫

作者 Y Cheung582 字2 分鐘00

系統環境需求 PHP環境最好是 PHP 7,系統最好是紅帽系。 操作步驟 安裝Oracle Instant Client 在Oracle官網頁面中找到系統對應的客戶端下載安裝,比如 …

Drupal7中自定义表格输入

作者 Y Cheung1240 字3 分鐘00

Drupal默认的节点表单(entity form)输入有时候并不能满足我们的需求,比如我想做一个类似excel表格输入形式的表单呢? = DEMO点这里 (PS. 用 …

↑