Hi

I want to call custom_search module block without using regions in page--mypagename.tpl.php

Comments

koneru.46@gmail.com’s picture

Could you provide some more details..like what actually you are trying to do.

rantebi’s picture

How about....
echo menu_block_view('main_menu');
menu - the module.
main_menu - the block delta.

That's the way I roll.

BTW - If you like to display menus that are dynamic I recommend using menu_block module!

mattez’s picture

I found this

$block = block_load('block', '1');
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;

(here: http://api.drupal.org/api/drupal/includes--module.inc/function/module_in...)
This only works form me...

LTech’s picture

How do I load the block title in drupal 7?
I can get the content using:

 $block = module_invoke('views', 'block_view', 'name-block_1');	
				print render($block['content']);

and I've tried print render($block['title']); and print render($block['header']); but I can't work out what I should be calling in the array to get the title.

drupal_simply_amazing’s picture

print $block->title;

Virang’s picture

Hi,

this is really helpful, you saved my time, thanks

omrmankar’s picture

With wrburgess's answer, you may get an error if your server is using a newer version of PHP.

Strict warning: Only variables should be passed by reference in include()...

This is what I did to not cause/get rid of the error.

    $blockObject = block_load('views', 'block_name');
    $block = _block_get_renderable_array(_block_render_blocks(array($blockObject)));
    $output = drupal_render($block);
    print $output;
  

Best regards,

Omprakash Mankar
Senior Drupal Developer

CatherineMurphy’s picture

Thank you! This worked for me. 

rakesh.gectcr’s picture

https://drupal.stackexchange.com/questions/8369/how-do-i-programmaticall...

 

echo views_embed_view('view_machine_name', 'block_1');

---
Kind regards,

Thank you,
Rakesh James

jucedogi’s picture

Have in mind that 

echo views_embed_view('view_machine_name', 'block_1');

only works for blocks, or other elements such as pages, created within a view.

The code

$blockObject = block_load('views', 'block_name');
$block = _block_get_renderable_array(_block_render_blocks(array($blockObject)));
$output = drupal_render($block);
print $output;

works for any block that you may want to render.

Hemangi Gokhale’s picture

It worked for me! Thanks.

auxiliaryjoel’s picture

@jucedogi I have a .tpl.php file I created based on Theme dubug file name suggestions:

 FILE NAME SUGGESTIONS:
   x block--views--home-slides-new-block.tpl.php

with that file now created, content I write in the file appears but it overrides the Block content, how do I use your code to render and apply classes to the block, rather than replacing it?

EDIT: I placed this into my .tpl.php file and it rendered the block :

<?php echo views_embed_view('home_slides_new', 'block'); ?>
jucedogi’s picture

The code examples given won't alter the output of the content that is being called upon.

Templates you use will display whatever you place on them as you noticed. I understand that you are using views to create a display. If you wish to replace the output then you need to find the corresponding template with the content you wish to alter.

On the advanced section of the view edit there is a theme information link in the lower area where you can get all the possible templates with their standard content. I believe what you are trying to alter would be in one of these template suggestions.

Depending on what you are trying to achieve you could also use a custom text field in which you would place the output of other fields within your custom markup.

auxiliaryjoel’s picture

Thanks @jucedogi do you have any code examples of how I could render a particular field rather than the whole block?

For example if 2 of 3 fields from a view or a block are find, but one needs-some fine-tuning with style, how I could separate the fields out and render them on the page?

jucedogi’s picture

There are methods that help you achieve this such as field_view_field.

If you need to modify the output of a specific field then you could also use the template system and make a template for said field. Look in the core modules for the field theme base template to start it out.

James Feng’s picture

Thank you jucedogi, You code has been of great help to me.