如何输出drupal图像字段?

Ale*_*x C 20 drupal drupal-7 drupal-themes drupal-theming

我很有可能只是寻找帮助找到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
        (
    )
)
Run Code Online (Sandbox Code Playgroud)

所以我可以使用(丑陋的)手动滚动函数显示图像,这些函数获取了找到的值$item['#options']['entity']->field_image并替换public://了实际的服务器路径,然后我也可能想要加载正确的图像drupal图像样式(缩略图,自定义样式等...)

可悲的是,我只是不知道函数的名称是什么类似于:unknown_function_name_drupal_image_print($item['#options']['entity']->field_image, 'thumnail');是.

有没有人可以帮我找到这个?

Spa*_*ers 23

你在找 image_style_url(style_name, image_url);

例如:

<?='<img src="'.image_style_url('fullwidth', $node->field_page_image['und'][0]['filename']).'" />'?>
Run Code Online (Sandbox Code Playgroud)

编辑

如前所述,您还可以在"管理显示"页面中为内容类型设置图像样式,然后使用渲染进行输出.

<?php print render($content['field_image']); ?>
Run Code Online (Sandbox Code Playgroud)


mav*_*ame 14

我会使用field_get_itemsfield_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); ?>
Run Code Online (Sandbox Code Playgroud)

我有一篇关于现场访问的文章和这里的内容.

祝好运.

  • 这很棒!但是(至少对于Drupal 7),对field_view_value的调用应该是:field_view_value('node',$ node,'field_image',$ images [0],array('type'=>'image','settings'= > array('image_style'=>'custom_image_style'//可能是'缩略图')); ...您需要包含字段名称. (2认同)

Aye*_*h K 12

SpaceBeers的答案是正确的 - 你可以用这种方式打印图像.但在Drupal方面,这很糟糕.您的语言未定义,如果它是多值字段,则直接使用第一个字段.此外,您正在硬编码一些Drupal的好东西,例如使用UI更改图像样式.

<?php print render($content['field_image']); ?>
Run Code Online (Sandbox Code Playgroud)

这将打印具有适当尺寸(在img标签中),alt标签的图像,并始终尊重您在mycustomcontenttype节点类型的"管理显示"选项卡中设置的内容.


Hea*_*aye 8

我用这个:

print theme('image_style', array('path' => [image uri from field], 'style_name' => [image style]));
Run Code Online (Sandbox Code Playgroud)

您还可以包含一个"attributes"数组变量,其中包含(例如)class,alt和title的元素.我认为这是一个更好的选择,允许您选择图像样式,同时仍然使用Drupal(或您的主题)默认图像渲染.