0

Folks - I'm trying to create a custom field view with custom content that utilises the standard theme_field template, to display in a node template.

I've tried to call the function programmatically e.g:

  $field_vars = array(
    'title_attributes' => 'test123',
    'label' => t('custom field label'),
    'items' => array('<p>This is a test</p>'),
    'classes' => 'test',
    'attributes' => FALSE
  );
  $my_field = theme('field', $field_vars); 
  print $my_field;

It returns the template, but none of the $field_vars are being used. Has anyone done something similar?

1 Answer 1

1

I'm not really sure what you are trying to do, but if you only like to render a custom array with the field template this should work:

$field = array(
  '#theme' => 'field',
  '#field_type' => 'text',
  '#field_name' => 'temp_field',
  '#entity_type' => 'node',
  '#bundle' => null,
  '#items' => array(
     array(
       'value' => '',
     )
   ),
   0 => array(
     '#markup' => '<p>This is a test</p>'
   )
);
print theme_field(array(
  'label_hidden' => false,
  'label' => t('custom field label'),
  'items' => array($field),
  'classes' => 'test',
  'content_attributes' => '',
  'attributes' => '',
  'item_attributes' => ''
));

You can load one of your existing field using field_view_field() to check what data the renderable array contains. For example to load the body field from a node with an id equal to 1000 you could do like this:

$node = node_load(1000);
$field = field_view_field('node', $node, 'body');
print_r($field);
1

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.