function render_example_page_alter

Implements hook_page_alter().

Alters the page in several different ways based on how the form has been configured.

Related topics

File

render_example/render_example.module, line 330

Code

function render_example_page_alter(&$page) {
    // Re-sort the sidebar in reverse order.
    if (variable_get('render_example_reverse_sidebar', FALSE) && !empty($page['sidebar_first'])) {
        $page['sidebar_first'] = array_reverse($page['sidebar_first']);
        foreach (element_children($page['sidebar_first']) as $element) {
            // Reverse the weights if they exist.
            if (!empty($page['sidebar_first'][$element]['#weight'])) {
                $page['sidebar_first'][$element]['#weight'] *= -1;
            }
        }
        $page['sidebar_first']['#sorted'] = FALSE;
    }
    // Add a list of items to the top of sidebar_first.
    // This shows how #theme and #theme_wrappers work.
    if (variable_get('render_example_note_about_render_arrays', FALSE) && !empty($page['sidebar_first'])) {
        $items = array(
            t('Render arrays are everywhere in D7'),
            t('Leave content unrendered as much as possible'),
            t('This allows rearrangement and alteration very late in page cycle'),
        );
        $note = array(
            '#title' => t('Render Array Example'),
            '#items' => $items,
            // The functions in #pre_render get to alter the actual data before it
            // gets rendered by the various theme functions.
'#pre_render' => array(
                'render_example_change_to_ol',
            ),
            // The functions in #post_render get both the element and the rendered
            // data and can add to the rendered data.
'#post_render' => array(
                'render_example_add_hr',
            ),
            // The #theme theme operation gets the first chance at rendering the
            // element and its children.
'#theme' => 'item_list',
            // Then the theme operations in #theme_wrappers can wrap more around
            // what #theme left in #chilren.
'#theme_wrappers' => array(
                'render_example_add_div',
                'render_example_add_notes',
            ),
            '#weight' => -9999,
        );
        $page['sidebar_first']['render_array_note'] = $note;
        $page['sidebar_first']['#sorted'] = FALSE;
    }
    // Move the navigation menu into the content area.
    if (variable_get('render_example_move_navigation_menu', FALSE) && !empty($page['sidebar_first']['system_navigation']) && !empty($page['content'])) {
        $page['content']['system_navigation'] = $page['sidebar_first']['system_navigation'];
        $page['content']['system_navigation']['#weight'] = -99999;
        unset($page['content']['#sorted']);
        unset($page['sidebar_first']['system_navigation']);
    }
    // Show the render array used to build the page render array display.
    if (variable_get('render_example_show_page', FALSE)) {
        $form['render_example_page_fieldset'] = array(
            '#type' => 'fieldset',
            '#title' => t('Page render array'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
        );
        $form['render_example_page_fieldset']['markup'] = array(
            // The kpr() function is from devel module and is here only allow us
            // to output the array in a way that's easy to explore.
'#markup' => kpr($page, TRUE),
        );
        $page['content']['page_render_array'] = drupal_get_form('render_example_embedded_form', $form);
        $page['content']['page_render_array']['#weight'] = -999999;
        $page['content']['#sorted'] = FALSE;
    }
    // Add render array to the bottom of each block.
    if (variable_get('render_example_show_block', FALSE)) {
        foreach (element_children($page) as $region_name) {
            foreach (element_children($page[$region_name]) as $block_name) {
                // Push the block down a level so we can add another block after it.
                $old_block = $page[$region_name][$block_name];
                $page[$region_name][$block_name] = array(
                    $block_name => $old_block,
                );
                $form = array();
                $form['render_example_block_fieldset'] = array(
                    '#type' => 'fieldset',
                    '#title' => t('Block render array'),
                    '#collapsible' => TRUE,
                    '#collapsed' => TRUE,
                );
                $form['render_example_block_fieldset']['markup'] = array(
                    '#type' => 'item',
                    '#title' => t('%blockname block render array', array(
                        '%blockname' => $block_name,
                    )),
                    // The kpr() function is from devel module and is here only allow us
                    // to output the array in a way that's easy to explore.
'#markup' => kpr($old_block, TRUE),
                );
                // Add the new block that contains the render array.
                $page[$region_name][$block_name]['render_example_block_render_array'] = drupal_get_form('render_example_embedded_form', $form);
                $page[$region_name][$block_name]['render_example_block_render_array']['#weight'] = 999;
            }
        }
    }
    // Add #prefix and #suffix to a block to wrap a div around it.
    if (variable_get('render_example_prefix', FALSE)) {
        foreach (element_children($page) as $region_name) {
            foreach (element_children($page[$region_name]) as $block_name) {
                $block =& $page[$region_name][$block_name];
                $block['#prefix'] = '<div class="block-prefix"><p>Prefixed</p>';
                $block['#suffix'] = '<span class="block-suffix">Block suffix</span></div>';
            }
        }
    }
}