简体   繁体   中英

How to prepare field for display in Drupal

I'm having a problem with picture module and loading of lists. So my structure is content type with some fields one of them is image set to display picture.

On another page I wan't to display 2 objects of that content type. So I select it from the database and preprocess fields. With normal fields it's ok since i only need theirs value, but with picture I need all other data like breakpoints, theme. Basicly for image I get that:

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)

And I need that:

'#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)

So the question is how to prepare field.

Thanks!

I think what you are searching for is field_view_field() . This function will invoke internally hook_field_formatter_view() . The picture module implements this hook , and depending on the $display parameter it will internally call one of the provided display functions.

Based on the desired output you requested, you should pass picture as display parameter, in order to invoke picture_field_formatter_picture_view() which seems to return the appropriate renderable array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM