0

I need to insert a block inside in a specific place of my Drupal posts of only one type.

I understand that would be done through page.tpl.php file but can somebody explain how do I actually create that block in the first place and ascribe the right name to it? I tried searching in documentation and I'm a bit confused... Thank you!

    <div id="content-wrap">
                <?php print render($title_prefix); ?>
                    <?php if ($title): ?>
                      <h1 class="page-title"><?php print $title;  ?></h1>
                    <?php endif; ?>
                <?php print render($title_suffix); ?>

                <!-- I GUESS THE NEW BLOCK GOES IN HERE BUT WHERE DO I SET IT UP? -->

                <?php print render($page['content']); ?>

enter image description here

2

You render the block in the preprocess function for that template. Each template (with its family of suggestions) has a preprocess function. For instance, if you are rendering page.tpl.php or a suggestion based off that, then the preprocess will be named THEME_preprocess_page (template_preprocess_page in the docs).

Inside the preprocess function you add a new key to the $vars array, and set it to the html output that you want printed. Then, in the template file (ie page.tpl.php) you print that variable.

example:

template.php file

function MYTHEME_preprocess_page(&$vars) {
  $block = module_invoke('module_name', 'block_view', 'block_delta');
  $vars['my_block'] = render($block['content']);
}

page.tpl file:

<?php print $my_block; ?>

You can also just assign the block to a region (below content for instance) and then move it out of there into wherever you want it by using hook_page_alter.

Drupal 7 preprocess tutorial adding variable to template

Using hook_page_alter to move node render items into blocks

Placing the contents of a block in any location

Drupal 7 - How to Print Blocks in templates

  • You can also use the hook_preprocess_page in a custom module to decouple it from your theme. And to create the block see this – sareed Jan 27 '15 at 23:07

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.