快轉到主要內容

解決 Drupal 9 Media 列表縮略圖無法生成問題

·429 字·1 分鐘
Y Cheung
作者
Y Cheung
Blogger, Programer & Traveler.

站點安裝設置失誤導致在S3上缺失了 public://media-icons/ 下的所有文件,如今在media 中上傳任何 video、document、audio 都無法生成縮略圖,fallback 到本地path 提示 404 not found,檢視 Recent log messages 日誌,發現了相關錯誤信息:

Drupal log messeages
Source image at public://media-icons/generic/video.png not found while trying to generate derivative image at public://styles/thumbnail/public/media-icons/generic/video.png

❗當前使用的是 Drupal v9.3.3 和 s3fs 模組 v3.0.0-beta4 版本。

Y Cheung 嘗試手動在AWS S3中創建文件夾 media-icons/generic 並上傳圖片,問題仍未解決。

但是用drush打印 \\Drupal::service('file_url_generator')->generateAbsoluteString("public://media-icons/generic/video.png") 的結果,確認已經是正確的URL了,該地址也可以被公開訪問,奇怪。

後來Y Cheung找到了這篇 「“Image styles: Error generating image, missing source file” still an issue」:

For the s3fs module the s3fs_file SQL table is considered authoritative of the files list in the bucket. If a file is not listed in this database table it is not considered to be in the bucket and file_exists() should return false.

原來 S3fs 模組判斷一個文件是否存在是查詢數據庫表的(表名 s3fs_file),而不是查詢 AWS S3服務。所以,如文內所述,只要去 S3fs 模組設置中(/admin/config/media/s3fs/actions)的 FILE METADATA CACHE 刷新一下就好了。

Drupal S3FS config
S3 File System settings > action > Refresh file metadata cache️

相關文章

Drupal 9 Views 分頁器設置首頁顯示不同的條目個數

·372 字·1 分鐘
在Drupal Views 中自帶了分頁功能 Pager,可以在界面上自定義每頁要顯示條目個數 Items to display,要跳過的個數 Offset (number of items to skip),總頁數 Number of pages,分頁的標籤 Page Link Labels,指定分頁顯示的個數 Number of pager links visible。

drupal中获取 entity reference field 的 node type 值

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

Cross-Origin Resource Sharing (CORS) 跨來源資源共用錯誤解決方法

·932 字·2 分鐘
最近Y cheung 遇到了兩個典型 CORS error問題,XMLHttpRequest 和 Fetch 請求了跨來源資源被瀏覽器阻擋,記錄一下解決方法。 什麼是Cross-Origin Resource Sharing (CORS) 跨來源資源共用? # Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources. CORS also relies on a mechanism by which browsers make a “preflight” request to the server hosting the cross-origin resource, in order to check that the server will permit the actual request. In that preflight, the browser sends headers that indicate the HTTP method and headers that will be used in the actual request.

Drupal 9 Twig 獲取field值速查表

·1006 字·3 分鐘
在Drupal 8 和 Drupal 9 的 twig 模板開發中,常常會需要輸出指定的field,本文列出常見類型的field取值方法,以便查詢。 一般類型 General # Field type How to get value Note Boolean {{ content.field_name[0] }} 值為“On”或“Off”的label Boolean {{ content.field_name['#items'][0].value }} 值為1或0 Date {{ content.field_name[0] }} 值為套用time.html.twig模板的完整日期時間html片段 Date {{ content.field_name[0]['#text] }} 值為完整日期時間格式的純文本 Timestamp {{ content.field_name[0] }} 值為完整日期時間格式的純文本 Timestamp {{ content.field_name['#items'][0].value }} 值為時間戳數字 Email {{ content.field_name[0] }} 值為純文本格式 link {{ content.field_name[0]['#url'] }} 值為連結URL link {{ content.field_name[0]['#title'] }} 值為連結文字 數字類型 Number # Field type How to get value Note Number (float) {{ content.field_name[0] }} 顯示有前綴和後綴並number_decimal格式化 Number (float) {{ content.field_name['#items'][0].value }} 顯示未經格式化的浮點數 Number (integer) {{ content.field_name[0] }} 顯示有前綴和後綴 Number (integer) {{ content.field_name['#items'][0].value }} 顯示未經格式化的整數 Number (decimal) {{ content.field_name[0] }} 顯示有前綴和後綴 Number (decimal) {{ content.field_name['#items'][0].value }} 顯示未經格式化的小數 文字類型 Text # Field type How to get value Note Text (formatted) {{ content.field_name[0] }} 帶格式的文本 Text (formatted) {{ content.field_name[0]['#text'] }} 未經html轉化的文本 Text (formatted) {{ content.field_name['#items'][0].value|striptags }} 純文本 Text (formatted, long) {{ content.field_name[0] }} 帶格式的文本 Text (formatted, long) {{ content.field_name[0]['#text'] }} 未經html轉化的文本 Text (formatted, long) {{ content.field_name['#items'][0].value|striptags }} 純文本 Text (formatted, long with summary) {{ content.field_name[0] }} 帶格式的文本 Text (formatted, long with summary) {{ content.field_name[0]['#text'] }} 未經html轉化的文本 Text (formatted, long with summary) {{ content.field_name['#items'][0].value|striptags }} 純文本 Text (formatted, long with summary) {{ content.field_name['#items'][0].summary }} 摘要 Text (plain) {{content.field_name.0 }} 純文本 Text (plain, long) {{content.field_name.0 }} 純文本 列表類型 List # Field type How to get value Note List (float) {{ content.field_name[0] }} 在管理介面Manage Display中設置該欄位顯示值key或標籤label List (float) {{ content.field_name['#items'][0].value }} 顯示值的key List (integer) {{ content.field_name[0] }} 在管理介面Manage Display中設置該欄位顯示值key或標籤label List (integer) {{ content.field_name['#items'][0].value }} 顯示值的key List (text) {{ content.field_name[0] }} 在管理介面Manage Display中設置該欄位顯示值key或標籤label List (text) {{ content.field_name['#items'][0].value }} 顯示值的key 參照類型 Reference # Field type How to get value Note Fiel {{ content.field_name[0] }} 值為套用file-link.html.twig模板的html片段 File {{ file_url(content.field_name[0]['#file'].uri.value) }} 檔案連結 File {{ content.field_name[0]['#description'] }} 檔案描述 Image {{ file_url(content.field_name[0]['#item'].entity.uri.value) }} 圖片連結 Image {{ content.field_name[0]['#item'].value.alt }} 圖片alt文字 Image {{ content.field_name[0]['#item'].value.title }} 圖片title文字 Image {{ content.field_name[0]['#item'].value.height }} 圖片設置的高度 Image {{ content.field_name[0]['#item'].value.width }} 圖片設置的寬度 Taxonomy Term {{ content.field_taxonomy[0]['#options'].entity.id }} 在Manage display中將format設為Label時顯示term id Taxonomy Term {{ content.field_name[0]['#title'] }} 在Manage display中將format設為Label顯示term label Taxonomy Term {{ content.field_name[0]['#url'] }} 在Manage display中將format設為Label顯示term path 媒體類型 Media # 啟用Media模組後出的media field。