2

I hoped the documentation for theme_field() would tell me how to create a render array that may be parsed by that function.

Based on that, I thought something like this would work.

return array(
    'test' => array(
        '#theme' => 'field',
        '#label_hidden' =>  false,
        '#title_attributes' => 'class="test123"',
        '#label' => 'test',
        '#content_attributes' => 'class="test456"',
        '#items' => array()
    )
);

It prints a  .

what am I doing wrong?

The ultimate goal is of course printing some field_items, but I can find no documentation on how the render array for #items should look; nothing I tried outputs anything.

A pointer to better documentation might answer my questions.

  • Why are you trying to do this manually? The API has functions to prepare fields for you (field_view_field, field_view_value, etc.), it'd be far easier to use those to prepare your fields – Clive Jun 4 '13 at 12:26
  • I dont want to display drupal database fields, but custom data from a remote api. For consistency, I want it rendered on the front end 'as if it were drupal items'. – commonpike Jun 4 '13 at 12:48
2

If you absolutely need to piggyback on theme_field() rather than implement your own theme function (which would be preferable IMHO), then you'll have to jump through a few more hoops.

The key place to look is template_preprocess_field(), which will show you all of the elements that are expected to be in the array. Specifically for the label, the #title property is used. So this

return array(
  'test' => array(
    '#theme'                => 'field',
    '#label_hidden'         =>  false,
    '#title_attributes'     => 'class="test123"',
    '#label'                => 'test',
    '#title' => 'test',  // Required
    '#label_display' => 'visible',  // Required
    '#content_attributes'   => 'class="test456"',
    '#items'    => array()
  )
);

will return what you're looking for (with a bunch of notices about undefined indicies).

Eventually you'll also need to provide #field_name, #field_type, #bundle and #entity_type for each of your 'field' items.

  • excellent, that worked. i still cant get data to show up for the fields .. but since I'm going to have to fake a #bundle name and #field_type, indeed maybe this is not the way to go. – commonpike Jun 4 '13 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.