3

I am creating a custom module to read content from text files and generate various custom content types I have defined on a project. Some will have specialized lists and tables and all will have specific class assignments. I can easily generate the appropriate markup added to the text and send it to the node->body, but have been researching render().

I understand that render takes an array that encodes the content and styling, but cannot find a framework for the array to know how to structure it for my content.

How does render work?

1 Answer 1

4

There is no magic framework involved (just magic array structures). You just need to build up your array (Or in your case, extend $node->content in hook_node_view() if you want to alter how a node is displayed).

Basically, a render array is a nested array with properties (always start with a #) and child-elements, which are assumed to be another render-array. drupal_render() will then dig itself through and convert it to HTML.

There are basically three things you can do, define a #type (types are defined in hook_element_info(), see system_element_info) including the necessary properties, define a #theme function and the necessary properties or just simply #markup, which is simply going to print whatever is in it (Note that #markup actually #type => markup, which is added automatically)

A basic example:

<?php
$render_array = array(
  '#type' => 'cointainer',
  '#attributes' => array('class' => array('your-class')),
  'child_element' => array(
    '#markup' => t('Some text'),
  ),
  'a_table' => array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
  ),
);
$node->content['your_module'] = $render_array;
?>

Assuming that the node template uses render($content), this will result in <div class="your-class">Some text<table>...</table></div>

You can also directly return such an array in a page callback function, see userpoints_list_transactions() for an example.

I have a feeling that you are however asking the wrong question: Note that is only for rendering, you can not store anything in this structure. To store that, you should first create the necessary fields on your content types and put your data in it, if you don't want it to be just text within the body. Also have a look at Display Suite, which gives you a lot of control on how to display your fields.

7
  • First, thanks for a great answer. I must add however, that it would have been even better if you had written a book on the subject. :) Between your answer and the references to userpoints_list_transactions, hook_element, & system_element_info, I must have a thousand follow up questions on what those parts are doing. A few basic ones follow:
    – Ashlar
    Commented Nov 24, 2011 at 1:38
  • The system_element_info is really nice in that it shows the range of #properties associated with system form elements. Is there more documentation that outlines what is used elsewhere? And how render uses it?
    – Ashlar
    Commented Nov 24, 2011 at 1:41
  • You're right, I am a bit confused by your statement that I need to enter the content into the node. In your render array example above you have text in the child element, and a table with content in $rows. The content I want to place will have similar sections of paragraphs, lists and tables all within a single body field.
    – Ashlar
    Commented Nov 24, 2011 at 1:48
  • 1
    If you have follow-up questions, it's best to create separate questions and refer to this one, comments don't work well for long explanations. A render array is basically a subset of a $form definition, so you can use the form api documentation api.drupal.org/api/drupal/….
    – Berdir
    Commented Nov 25, 2011 at 20:40
  • 1
    And yes, as I said, you can not store render arrays, that just doesn't make sense. You only have two options, creating a number of fields which only works if your structure is static. If you have dynamic, complex content structures, you really only have one choice. Write it in the body or another text field. As HTML. Render arrays are designed to convert data structures into something that can be rendered to HTML but still be altered/themed differently.
    – Berdir
    Commented Nov 25, 2011 at 20:41

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.