【问题标题】:How to prepare field for display in Drupal如何准备字段以在 Drupal 中显示
【发布时间】:2015-01-20 10:41:49
【问题描述】:

图片模块和列表加载有问题。所以我的结构是内容类型,其中一些字段是图像集以显示图片。

在另一个页面上,我不想显示该内容类型的 2 个对象。所以我从数据库和预处理字段中选择它。使用普通字段没关系,因为我只需要它们的值,但使用图片我需要所有其他数据,如断点、主题。 基本上我得到的图像是:

array (size=1)
  'und' => 
    array (size=1)
      0 => 
        array (size=18)
          'fid' => string '3023' (length=4)
          'uid' => string '1' (length=1)
          'filename' => string 'profile_picture.jpg' (length=19)
          'uri' => string 'public://profile_picture.jpg' (length=28)
          'filemime' => string 'image/jpeg' (length=10)
          'filesize' => string '325835' (length=6)
          'status' => string '1' (length=1)
          'timestamp' => string '1421745203' (length=10)
          'type' => string 'image' (length=5)
          'uuid' => string '59484070-9a95-42fb-8b2a-594e5d3a7483' (length=36)
          'field_file_image_alt_text' => 
            array (size=0)
              empty
          'field_file_image_title_text' => 
            array (size=0)
              empty
          'rdf_mapping' => 
            array (size=0)
              empty
          'metadata' => 
            array (size=2)
              'height' => int 500
              'width' => int 400
          'alt' => string '' (length=0)
          'title' => string '' (length=0)
          'width' => string '400' (length=3)
          'height' => string '500' (length=3)

我需要那个:

'#theme' => string 'picture_formatter' (length=17)
  '#attached' => 
    array (size=1)
      'library' => 
        array (size=3)
          0 => 
            array (size=2)
              0 => string 'picture' (length=7)
              1 => string 'picturefill_head' (length=16)
          1 => 
            array (size=2)
              0 => string 'picture' (length=7)
              1 => string 'picturefill' (length=11)
          2 => 
            array (size=2)
              0 => string 'picture' (length=7)
              1 => string 'picture.ajax' (length=12)
  '#item' => 
    array (size=18)
      'fid' => string '3023' (length=4)
      'uid' => string '1' (length=1)
      'filename' => string 'profile_picture.jpg' (length=19)
      'uri' => string 'public://profile_picture.jpg' (length=28)
      'filemime' => string 'image/jpeg' (length=10)
      'filesize' => string '325835' (length=6)
      'status' => string '1' (length=1)
      'timestamp' => string '1421745203' (length=10)
      'type' => string 'image' (length=5)
      'uuid' => string '59484070-9a95-42fb-8b2a-594e5d3a7483' (length=36)
      'field_file_image_alt_text' => 
        array (size=0)
          empty
      'field_file_image_title_text' => 
        array (size=0)
          empty
      'rdf_mapping' => 
        array (size=0)
          empty
      'metadata' => 
        array (size=2)
          'height' => int 500
          'width' => int 400
      'alt' => string '' (length=0)
      'title' => string '' (length=0)
      'width' => string '400' (length=3)
      'height' => string '500' (length=3)
  '#image_style' => string 'large_custom_user_mobile_1x' (length=27)
  '#breakpoints' => 
    array (size=5)
      'custom.user.ultra_wide' => 
        array (size=1)
          '1x' => 
            array (size=2)
              'mapping_type' => string 'image_style' (length=11)
              'image_style' => string 'large_custom_user_ultra_wide_1x' (length=31)
      'custom.user.wide' => 
        array (size=1)
          '1x' => 
            array (size=2)
              'mapping_type' => string 'image_style' (length=11)
              'image_style' => string 'large_custom_user_wide_1x' (length=25)
      'custom.user.normal' => 
        array (size=1)
          '1x' => 
            array (size=2)
              'mapping_type' => string 'image_style' (length=11)
              'image_style' => string 'large_custom_user_normal_1x' (length=27)
      'custom.user.narrow' => 
        array (size=1)
          '1x' => 
            array (size=2)
              'mapping_type' => string 'image_style' (length=11)
              'image_style' => string 'large_custom_user_narrow_1x' (length=27)
      'custom.user.mobile' => 
        array (size=1)
          '1x' => 
            array (size=2)
              'mapping_type' => string 'image_style' (length=11)
              'image_style' => string 'large_custom_user_mobile_1x' (length=27)
  '#path' => string '' (length=0)
  '#colorbox_group' => 
    array (size=0)
      empty
  '#colorbox_image_style' => string '' (length=0)

所以问题是如何准备字段。

谢谢!

【问题讨论】:

    标签: image drupal drupal-7


    【解决方案1】:

    我认为您要搜索的是field_view_field()。该函数将在内部调用hook_field_formatter_view()。图片模块implements this hook,根据$display参数,它将在内部调用提供的显示函数之一。

    根据您请求的所需输出,您应该将picture 作为显示参数传递,以便调用似乎返回适当的可渲染数组的picture_field_formatter_picture_view()

    【讨论】:

    • 您能举个例子吗?我试过这样: field_view_field("my_content_type", $node, "field_image");但我得到空数组作为回报。我用 node_load(nid) 得到 $node。
    • 不要使用内容类型作为第一个参数,只需传递 'node' 就可以了。 $node = node_load(336); field_view_field('node', $node, 'field_author');
    • 我有点让它与 field_view_field("node", $node, "field_image", array('type' => 'picture'));它不完全一样,但它有效!
    猜你喜欢
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    相关资源
    最近更新 更多