6

I am reading this theming article http://themery.com/dgd7/advanced-theming/render/manipulate and I have a question.

This article has the following code:

$page['highlighted']['new_stuff'] = array(
'#type' => 'container',
'#attributes' => array('class' => 'my-container'),
);
$page['highlighted']['new_stuff']['heading'] = array(
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => t('Heading'),
'#attributes' => array('id' => 'my-heading'),
);
$page['highlighted']['new_stuff']['list'] = array(
'#theme' => 'item_list',
'#items' => array(
  'First item',
  'Second item',
  'Third item',
), 
);

I see that when you provide the #theme property you are telling the Render API to use the theme_item_list, and you pass it the #items variable. But isn't that exactly what you are doing when you set type to #html_tag, you're using the theme_html_tag and passing it variables?

1 Answer 1

5

The purpose is a little different:

  • '#type' is telling to the render API to merge what returned from hook_element_info()
  • '#theme' is saying to the render API which theme function to use

In the example you are making, the theme function used is theme_html_tag() because system_element_info() returns the following data.

  $types['html_tag'] = array(
    '#theme' => 'html_tag',
    '#pre_render' => array('drupal_pre_render_conditional_comments'),
    '#attributes' => array(),
    '#value' => NULL,
  );

If '#theme' were set to 'tag_html', theme_tag_html() would be the theme used by the render API.

5
  • Thats very interesting. Is there a reason we'd use #type if we could just directly use the #theme? Dec 9, 2014 at 13:48
  • 1
    with '#type', you can set more than just '#theme'.
    – apaderno
    Dec 9, 2014 at 20:00
  • Can you elaborate a bit more? I see that both are setting properties which are really fields being passed to the template. Isn't this how it works? What can #type do more? Dec 9, 2014 at 21:55
  • 2
    The properties returned by hook_element_info() includes #pre_render, #process, #theme_wrappers; setting #type you can potentially set all those, not just #theme.
    – apaderno
    Dec 10, 2014 at 14:28
  • I asked their if really #type is necessary drupal.org/project/drupal/issues/3309942
    – gagarine
    Sep 15, 2022 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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