![](/img/trans.png)
[英]In Drupal 8 views, how can I rewrite the output of image field to become a div with this image as a background image?
[英]How do I output a drupal image field?
我很有可能只是寻找帮助找到drupal(7)中已存在的函数的名称,但有时文档有点难以导航。 希望有人可以帮助我。
我有一个具有自定义字段的节点。
我正在模板field--mycustomcontenttype.tpl.php
,所以我试图找到输出的PHP函数的名称和图像样式的图像字段。
mycustomcontenttype
是一个带有以下附加字段的NODE:
[field_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 51
[alt] => ImageAltText
[title] =>
[width] => 150
[height] => 150
[uid] => 29
[filename] => myimagename.jpg
[uri] => public://myimagename.jpg
[filemime] => image/jpeg
[filesize] => 8812
[status] => 1
[timestamp] => 1339445372
[uuid] => a088ea8f-ddf9-47d1-b013-19c8f8cada07
[metatags] => Array
(
)
)
所以我可以使用(丑陋的)手动滚动函数来显示图像,这些函数采用$item['#options']['entity']->field_image
并将public://
替换为实际的服务器路径,然后我也可能想要使用正确的drupal图像样式(缩略图,自定义样式等)加载图像。
遗憾的是,我只是不知道函数的名称是什么,例如: unknown_function_name_drupal_image_print($item['#options']['entity']->field_image, 'thumnail');
是。
有没有人可以帮我找到这个?
您正在寻找image_style_url(style_name, image_url);
例如:
<?='<img src="'.image_style_url('fullwidth', $node->field_page_image['und'][0]['filename']).'" />'?>
编辑
如前所述,您还可以在“管理显示”页面中为内容类型设置图像样式,然后使用渲染进行输出。
<?php print render($content['field_image']); ?>
我会使用field_get_items和field_view_value的组合来完成此操作。
例如:
<?php
// In some preprocessor...
$images = field_get_items('node', $node, 'field_image');
if(!empty($images)) {
$image = field_view_value('node', $node, 'field_image', $images[0], array(
'type' => 'image',
'settings' => array(
'image_style' => 'custom_image_style' // could be 'thumbnail'
)
)
);
}
$variables['image'] = $image;
// Now, in your .tpl.php
<?php print render($image); ?>
我对现场的访问和这事写了这里 。
祝好运。
SpaceBeers的答案是正确的 - 你可以用这种方式打印图像。 但在Drupal方面,这很糟糕。 您的语言未定义,如果它是多值字段,则直接使用第一个字段。 此外,您正在硬编码一些Drupal的好东西,例如使用UI更改图像样式。
<?php print render($content['field_image']); ?>
这将打印具有适当尺寸(在img标签中),alt标签的图像,并始终尊重您在mycustomcontenttype节点类型的“管理显示”选项卡中设置的内容。
我用这个:
print theme('image_style', array('path' => [image uri from field], 'style_name' => [image style]));
您还可以包含一个“attributes”数组变量,其中包含(例如)class,alt和title的元素。 我认为这是一个更好的选择,允许您选择图像样式,同时仍然使用Drupal(或您的主题)默认图像渲染。
你应该用
<?='<img src="'.image_style_url('fullwidth', $node->field_page_image['und'][0]['uri']).'" />'?>
代替
<?='<img src="'.image_style_url('fullwidth', $node->field_page_image['und'][0]['filename']).'" />'?>
我认为Ayesh K的答案是将字段打印到页面的首选Drupal方式。 更容易阅读,并在UI中保留您对图像所做的任何操作/预设。
你可以分解它,但似乎这将是狭隘的用例。
<?php
global $base_url;
$nid=6;//node id
$node=node_load($nid);
echo '<div style="margin-right:350px;">';
echo "<marquee>";
foreach($node->field_image_gallery['und'] as $v){
$image=$v['uri'];
$image_path=explode("public://",$image);
$url_path=$base_url.'/sites/default/files/'.$image_path[1];
echo '<img src="'.$url_path.'" width="100" height="100"> ';
}
echo "</marquee>";
echo "</div>";
?>
这可以这样做:
$style='full_width';
$path=$node->my_img_field['und']['0']['uri'];
$style_url = image_style_url($style, $path);
print "<img src=".file_create_url($style_url)." >";
$field_image = field_view_field('node', $promoNode, 'field_image');
$variables['promo_image'] = render($field_image);
呈现:
<div class="field field--name-field-image field--type-image field--label-above"> <div class="field__label">Image: </div> <div class="field__items"> <div class="field__item even"> <img typeof="foaf:Image" src="http://domain/sites/domain/files/promo1.png" width="360" height="301" alt="Promotional Image" title="Promotional Image Title"> </div> </div> </div>
注意:可以使用字段的内容类型Manage-Display设置删除标签上面的类,方法是将Label设置为,但这对我来说现在不起作用,所以我只是使用CSS来隐藏。 field__label类。
这有助于我将图像添加到搜索结果页面:
<?php if ($result['node']-> field_image): ?>
<?php
$search_image_uri = $result['node']-> field_image['und'][0]['uri'];
// assuming that the uri starts with "public://"
$search_image_locate = explode('//', $search_image_uri);
$search_image_filepath = '/sites/actual/path/to/public/' . $search_image_locate[1];
?>
<div class="search-image">
<img src="<?php print $search_image_filepath; ?>" />
</div>
<?php endif; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.