![](/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.