drupal-7 - Drupal 7 theme_image src 来自绝对 uri 的绝对路径
问题描述
当前使用以下方式渲染图像:
$desktop_img = theme('image', array(
'path' => drupal_get_path('module', 'my_awesome_module') . '/images/desktop.png',
'width' => 20,
'height' => 20,
'alt' => t('View pc version'),
));
呈现为:
<img src="http://myawesome.site/sites/all/modules/custom/my_awesome_module/images/desktop.png" width="20" height="20" alt="View pc version" />
但我想要的是:
<img src="/sites/all/modules/custom/my_awesome_module/images/desktop.png" width="20" height="20" alt="View pc version" />
现在我们有一个解决方案可以使用:
function another_awesome_module_file_url_alter(&$uri) {
// Get outta here if there's an absolute link
if (strpos($uri, '://') !== FALSE) {
return;
}
// If the path includes references a gif/jpg/png images elsewhere
if (strpos($uri, conf_path()) !== FALSE ||
preg_match('/\.(jpg|gif|png)/i', $uri)) {
$uri = $GLOBALS['base_path'] . ltrim($uri, '/');
}
}
返回所有文件的绝对路径。所以我的问题是,是否有一种在 theme_image 中仅针对手头的图像执行此操作的方式,而不是更改所有文件的路径?
解决方案
使用base_path()函数来获取正确的路径,然后你给主题函数一个绝对路径。
$desktop_img = theme('image', array(
'path' => base_path() . drupal_get_path('module', 'my_awesome_module') .
'/images/desktop.png',
'width' => 20,
'height' => 20,
'alt' => t('View pc version'),
));
问题不在于drupal_get_path
,它给出了相对路径,在图像主题中。
推荐阅读
- javascript - 邮递员检查响应值是否大于或等于另一个值
- xml - Qt 5.15.1 中的 QXmlStreamWriter
- amazon-web-services - 用于设置 AWS Codepipline 通知规则的 Terraform
- c++ - 我正在 apache-parquet(cpp) 中执行一个名为 arrow-test.cc 的文件,我面临错误:未在此范围内声明“MakeTable”
- reactjs - React:在 useEffect 中调用上下文函数时防止无限循环
- typescript - 类型“字符串”不可分配给类型“T[K]”
- r - 我无法绘制此图形
- azure-devops - Azure 管道 - 精确的 .net 核心版本
- vue.js - Vue:在触发转换之前更新转换名称不会呈现新的转换
- hadoop - 基于 Azure 的 Hadoop 集群 (HDInsight) 如何转换为经典的本地 Hadoop 集群