How to embed a view in hook_page_build() I'm trying to render a view into a region in hook_page_build().  I'm using views_embed_view(), but I'm getting the errors:


  Warning: Invalid argument supplied for foreach() in element_children()
  (line 6428 of /var/www/.../includes/common.inc). Warning: Illegal
  string offset '#children' in drupal_render() (line 5893 of
  /var/www/.../includes/common.inc). Warning: Illegal string offset
  '#children' in drupal_render() (line 5903 of
  /var/www/.../includes/common.inc). Warning: Illegal string offset
  '#children' in drupal_render() (line 5941 of
  /var/www/.../includes/common.inc). Warning: Illegal string offset
  '#printed' in drupal_render() (line 5948 of
  /var/www/.../includes/common.inc).


I have a view named 'blogs' and a view named 'block_2'.  In my hook:

function mymod_page_build(&amp;$page) {
    $page['sidebar_second']['blogs'] = views_embed_view('blogs', 'block_2');
}


I'm trying to move away from blocks and context, and just render content I'd otherwise use in blocks directly into their regions in hook_page_build().
The docs say hook_page_build()'s $page variable is for render arrays.

Less obvious from the docs without digging a little further but views_embed_view() returns a string of HTML markup.

So just assigning the result of views_embed_view() to the render array breaks it.

Instead you can use a #markup element.

Because it is the default element type you don't have to specify an element type or theme function or anything, so you can just do this:

/**
 * Implements hook_page_build().
 */
function MODULENAME_page_build(&amp;$page) {
  $page['sidebar_second']['blogs']['#markup'] = views_embed_view('blogs', 'block_2');
}


If that is your entire code for this though (if you don't have some complex logic relating to when this view is being added) I would not recommend adding a block to a region in this manner.

There are lots of options already out there for adding blocs to regions without custom code. For example you could use one of these:


Drupal core block module
Context
Panels