【问题标题】:How do I output a drupal image field?如何输出 drupal 图像字段?
【发布时间】:2012-06-21 15:44:53
【问题描述】:

很可能我只是在寻求帮助来查找已经存在于 drupal (7) 中的函数的名称,但有时文档有点难以浏览。希望有人可以帮助我。

我有一个具有自定义字段的节点。

我在模板 field--mycustomcontenttype.tpl.php 中工作,因此我试图找到输出的 PHP 函数的名称和带有图像样式的图像字段。

mycustomcontenttype 是一个带有以下附加字段的节点:

[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'); 这样工作的函数的名称是什么。

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

【问题讨论】:

    标签: drupal drupal-7 drupal-theming drupal-themes


    【解决方案1】:

    您正在寻找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']); ?>
    

    【讨论】:

    • 我同意这一点,虽然我会说使用 Drupals 主题层渲染图像更好api.drupal.org/api/drupal/includes!theme.inc/function/…
    • 我完全同意,但问题似乎是在寻找一个采用这种图像样式的函数。不过,我已经进行了编辑以包含渲染选项。
    • 节省了我的时间。非常感谢
    【解决方案2】:

    我会使用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); ?>
    

    我写了一篇关于字段访问的文章,这正是here

    祝你好运。

    【讨论】:

    • 这太棒了!但是(至少对于 Drupal 7),对 field_view_value 的调用应该是: field_view_value('node', $node, 'field_image', $images[0], array( 'type' => 'image', 'settings' = > array( 'image_style' => 'custom_image_style' // 可以是 'thumbnail' ) ); ... 你需要包含字段名称。
    • 有没有办法将 css 类也添加到字段包装器中?!
    • 这对我有用,似乎尊重语言模块 oob
    • 这是我在 node.tpl.php 中得到的代码: field_image) ) { $node_image = field_get_items('node', $node, 'field_image '); ?> " />
    【解决方案3】:

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

    <?php print render($content['field_image']); ?>
    

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

    【讨论】:

    • 我已经修改了我的答案以包括这个。我假设他们已经知道这一点,并希望能够以与默认样式不同的样式输出图像。
    【解决方案4】:

    我用这个:

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

    您还可以包含一个“属性”数组变量,其中包含(例如)类、alt 和标题的元素。我认为这是允许您选择图像样式的更好选择,同时仍使用 Drupal(或您的主题)的默认图像渲染。

    【讨论】:

      【解决方案5】:

      你应该使用

      <?='<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']).'" />'?>
      

      【讨论】:

        【解决方案6】:

        我认为 Ayesh K 的回答是将字段打印到页面的首选 Drupal 方式。更容易阅读,并保留您对 UI 中的图像所做的任何操作/预设。

        您可以将其分解,但似乎只适用于狭窄的用例。

        【讨论】:

          【解决方案7】:
          <?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">&nbsp;';
           }
          
          echo "</marquee>";
          echo "</div>";
          ?>
          

          【讨论】:

          • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。 (来自here
          【解决方案8】:

          可以这样做:

             $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)." >";
          

          【讨论】:

            【解决方案9】:
             $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:&nbsp;</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>

            注意:可以使用字段的内容类型管理显示设置删除 label-above 类,方法是将 Label 设置为 ,但这对我现在不起作用,所以我只是使用 CSS 来隐藏 .field__label 类。

            【讨论】:

              【解决方案10】:

              这对我向我的搜索结果页面添加图像很有用:

              <?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; ?>
              

              【讨论】:

              • 发生了太多事情,比如必须分解 uri、构建文件路径……抱歉,它应该会简单得多。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-11-04
              • 2016-02-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多