快轉到主要內容

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

·181 字·1 分鐘
Y Cheung
作者
Y Cheung
Blogger, Programer & Traveler.
目錄

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中获取 entity reference field 的 node type 值

·421 字·1 分鐘
标题很拗口我知道…… 用过 entity reference 模组的朋友都知道,这个模组(module)能让你在节点(node)中添加一个 Entity Reference 的字段(Field),可以引用其他的实体(entity)。此次添加引用的是节点。根据需求,须输出节点类型(node type),但是 entity reference 的字段只能输出被引用实体的ID(本例是 nid),所以……

獲取最新惡意爬蟲列表配置 Fail2ban Filter

·873 字·2 分鐘
安裝並設置好 Fail2ban 後(可以參考Y Cheung 之前寫的『Fail2ban 配置 Nginx filter』),可以看到在 /etc/fail2ban/filter.d/nginx-badbots.conf 內容如下: 1# /etc/fail2ban/filter.d/nginx-badbots.conf 2# Fail2Ban configuration file 3# 4# Regexp to catch known spambots and software alike. Please verify 5# that it is your intent to block IPs which were driven by 6# above mentioned bots. 7 8[Definition] 9 10badbotscustom = EmailCollector|WebEMailExtrac|TrackBack/1\.02|sogou music spider|(?:Mozilla/\d+\.\d+ )?Jorgee 11 12badbots = Atomic_Email_Hunter/4\.0|atSpider/1\.0|autoemailspider|bwh3_user_agent|China Local Browse 2\.6|ContactBot/0\.2|ContentSmartz|DataCha0s/2\.0|DBrowse 1\.4b|DBrowse 1\.4d|Demo Bot DOT 16b|Demo Bot Z 16b|DSurf15a 01|DSurf15a 71|DSurf15a 81|DSurf15a VA|EBrowse 1\.4b|Educate Search VxB|EmailSiphon|EmailSpider|EmailWolf 1\.00|ESurf15a 15|ExtractorPro|Franklin Locator 1\.8|FSurf15a 01|Full Web Bot 0416B|Full Web Bot 0516B|Full Web Bot 2816B|Guestbook Auto Submitter|Industry Program 1\.0\.x|ISC Systems iRc Search 2\.1|IUPUI Research Bot v 1\.9a|LARBIN-EXPERIMENTAL \(efp@gmx\.net\)|LetsCrawl\.com/1\.0 \+http\://letscrawl\.com/|Lincoln State Web Browser|LMQueueBot/0\.2|LWP\:\:Simple/5\.803|Mac Finder 1\.0\.xx|MFC Foundation Class Library 4\.0|Microsoft URL Control - 6\.00\.8xxx|Missauga Locate 1\.0\.0|Missigua Locator 1\.9|Missouri College Browse|Mizzu Labs 2\.2|Mo College 1\.9|MVAClient|Mozilla/2\.0 \(compatible; NEWT ActiveX; Win32\)|Mozilla/3\.0 \(compatible; Indy Library\)|Mozilla/3\.0 \(compatible; scan4mail \(advanced version\) http\://www\.peterspages\.net/?scan4mail\)|Mozilla/4\.0 \(compatible; Advanced Email Extractor v2\.xx\)|Mozilla/4\.0 \(compatible; Iplexx Spider/1\.0 http\://www\.iplexx\.at\)|Mozilla/4\.0 \(compatible; MSIE 5\.0; Windows NT; DigExt; DTS Agent|Mozilla/4\.0 efp@gmx\.net|Mozilla/5\.0 \(Version\: xxxx Type\:xx\)|NameOfAgent \(CMS Spider\)|NASA Search 1\.0|Nsauditor/1\.x|PBrowse 1\.4b|PEval 1\.4b|Poirot|Port Huron Labs|Production Bot 0116B|Production Bot 2016B|Production Bot DOT 3016B|Program Shareware 1\.0\.2|PSurf15a 11|PSurf15a 51|PSurf15a VA|psycheclone|RSurf15a 41|RSurf15a 51|RSurf15a 81|searchbot admin@google\.com|ShablastBot 1\.0|snap\.com beta crawler v0|Snapbot/1\.0|Snapbot/1\.0 \(Snap Shots&#44; \+http\://www\.snap\.com\)|sogou develop spider|Sogou Orion spider/3\.0\(\+http\://www\.sogou\.com/docs/help/webmasters\.htm#07\)|sogou spider|Sogou web spider/3\.0\(\+http\://www\.sogou\.com/docs/help/webmasters\.htm#07\)|sohu agent|SSurf15a 11 |TSurf15a 11|Under the Rainbow 2\.2|User-Agent\: Mozilla/4\.0 \(compatible; MSIE 6\.0; Windows NT 5\.1\)|VadixBot|WebVulnCrawl\.unknown/1\.0 libwww-perl/5\.803|Wells Search II|WEP Search 00 13 14failregex = ^<HOST> -.*"(GET|POST|HEAD).*HTTP.*"(?:%(badbots)s|%(badbotscustom)s)"$ 15 16ignoreregex = 17 18datepattern = {^LN-BEG}%%ExY(?P<_sep>[-/.])%%m(?P=_sep)%%d[T ]%%H:%%M:%%S(?:[.,]%%f)?(?:\s*%%z)? 19 ^[^\[]*\[({DATE}) 20 {^LN-BEG} 21 22# DEV Notes: 23# List of bad bots fetched from http://www.user-agents.org 24# Generated on Thu Nov 7 14:23:35 PST 2013 by files/gen_badbots. 25# 26# Author: Yaroslav Halchenko 如果沒有找到這個文件,請新建一個,或者將 apache-badbots.conf 拷貝並重命名。

Win 10 讀取 Mac HFS+ 格式硬碟

·363 字·1 分鐘
因需找出了多年不用的移動硬碟SSD,結果win 10 無法讀取!檢查了驅動什麼的都沒問題,也看到有這個硬碟了,就是不能正常加載出來,著急。 原本還以為是TOSHIBA的問題,因為看到網上有說win 10更新後無法正常使用東芝硬碟的,但是那都是2018年的事情了啊,應該早修復了呀。

XDebug升級更新配置

·83 字·1 分鐘
不知不覺中,XDebug 已經是Version 3 了! 根據官方文檔 https://xdebug.org/docs/upgrade_guide ,之前XDebug 2 的配置 (/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini )如下所示: 1xdebug.remote_connect_back=0 2xdebug.remote_autostart=1 3xdebug.remote_enable=1 4xdebug.remote_host="host.docker.internal" 5xdebug.remote_port=9001 6xdebug.idekey=PHPSTORM 7memory_limit = 1024M 8xdebug.remote_log="/tmp/xdebug.log" 需要改成這樣(v3): 1xdebug.discover_client_host=false 2xdebug.mode=debug 3xdebug.start_with_request=yes 4xdebug.client_host="host.docker.internal" 5xdebug.client_port=9001 6xdebug.idekey=PHPSTORM 7memory_limit = 1024M 8xdebug.log="/tmp/xdebug.log"