18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3<br />

As you can see, the link type specifies that the render element should be passed<br />

to two #pre_render functions. And it is the drupal_pre_render_link() function<br />

that looks for the special render element properties in our example's link element,<br />

namely, #title, #href, and #options.<br />

So to reiterate, hook_element_info() defines the default properties for its render<br />

element types, and it also specifies render callbacks that have their own internal API,<br />

defining render element properties.<br />

Using this framework, modules can create their own complex render element<br />

by implementing hook_element_info(), using the properties specified by<br />

drupal_render(), and by creating any render callbacks and associated APIs.<br />

hook_page_alter()<br />

So what's the point? By creating these complex render elements, we delay rendering<br />

of the data and allow opportunities to alter that data before it is rendered into a<br />

string. Before render elements were used in <strong>Drupal</strong>'s theme system, themers and<br />

module developers often had to completely re-render data after it had been rendered<br />

the default way. This was obviously inefficient. Now each of these render elements<br />

can be altered in preprocess functions or even directly in a template file with the<br />

show() and hide() functions.<br />

Now that we've looked at the guts of the Render API, it becomes much easier to<br />

understand how the template-embedded hide() function works. If a template file<br />

calls hide($element['child']); it simply sets the #printed property to TRUE, so<br />

when print render($element); is later called, the child element is not printed.<br />

We can then later call print render($element['child']); and render() will set<br />

#printed to FALSE and pass $element to drupal_render().<br />

<strong>Drupal</strong>'s theme implementations use render elements in various places throughout<br />

its theme hooks. But the two primary places render elements get used are in the<br />

block and page theme hooks.<br />

Any hook_block_view() implementation should return a renderable element,<br />

and any menu callback which supplies a page's main content should also return<br />

a render element.<br />

Once the page's main content is retrieved, drupal_render_page() will<br />

decorate the $page element using hook_page_build(). During the block module's<br />

block_page_build(), all of the page's regions are added to the $page element as<br />

child elements; and each of the region's child elements contain child elements for<br />

each of the blocks in that region. drupal_render_page() will then allow modules<br />

to modify the giant $page render element using hook_page_alter().<br />

[ 81 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!