可以說我有一個模塊mymodule。在mymodule forlder我有一個圖像文件夾,在該文件夾中我有圖像img1.jpg等在drupal中,我如何獲取模塊文件夾中圖像的URL?
如何獲取這些圖像的網址?
可以說我有一個模塊mymodule。在mymodule forlder我有一個圖像文件夾,在該文件夾中我有圖像img1.jpg等在drupal中,我如何獲取模塊文件夾中圖像的URL?
如何獲取這些圖像的網址?
drupal_get_path('module', $module_name);
// path
drupal_get_path('module', $module_name) . '/images';
file_create_url(drupal_get_path( '模塊',$ MODULE_NAME))。 '/圖片';
最簡單的方法,如上面提及,是使用相對路徑來站點根:
'/' . drupal_get_path('module', $module_name) . '/img1.jpg'
使用它而不在開始最後的斜線將打破它在多個級別別名,例如http://www.your_site.dev/category/2012/11/02/
要獲得圖像的正確URL,您需要在圖像路徑的開頭追加base_path()
。正確的URL將是:
$url = base_path() . drupal_get_path("module", "MY_MODULE") . "/image.png";
錯誤。 `file_create_url`將始終與_default/files_相關,而不是模塊目錄,因此URL最終將會像http://www.your_site.dev/sites/default/files/sites/all/modules/your_module/images這樣是不正確的。 – 2012-12-25 13:42:12
是的,正確的。 `file_code_url`將返回/ sites/default/files下的URL。謝謝。 – 2014-03-12 20:14:45