Warning In common.inc File When Calling module_invoke Function In Drupal 7

August 8th, 2014  | Categories: Drupal, Drupal 7

PROBLEM
So I want to create a carousel on the homepage of a website. The carousel items should be their own content type. Two of the carousel items need to generate a random publication and a random study (both content types). These can be generated by creating a View for both. I need to call this view though inside the carousel item content type.

I used the module_invoke method like this inside the body of the node (using the PHP filter):

<?php

$block = module_invoke(‘views’, ‘block_view’, ‘studies-block_1’);
print render($block);

?>

It outputted the content great. Exactly what I wanted it to do. However, it generated the following errors:

Warning: Invalid argument supplied for foreach() in element_children() (line 6428 of C:\xampp\htdocs\drupal_project\includes\common.inc).
Warning: Illegal string offset ‘#children’ in drupal_render() (line 5893 of C:\xampp\htdocs\drupal_project\includes\common.inc).
Warning: Illegal string offset ‘#children’ in drupal_render() (line 5903 of C:\xampp\htdocs\drupal_project\includes\common.inc).
Warning: Illegal string offset ‘#children’ in drupal_render() (line 5941 of C:\xampp\htdocs\drupal_project\includes\common.inc).
Warning: Illegal string offset ‘#printed’ in drupal_render() (line 5948 of C:\xampp\htdocs\drupal_project\includes\common.inc).

SOLUTION
Apparently these kinds of errors are very difficult to track down. It usually means some third-party module has passed in something wrong to one of Drupal’s common functions.

Since I’m using the render function from Drupal 7, I decided to output what the $block would be. It’s an array. As I’m already using PHP inside a block, what’s the harm in using some more? I just needed the first part of the array…

<?php

$block = module_invoke(‘views’, ‘block_view’, ‘studies-block_1’);
print($block[“content”][“#markup”]);

?>

Very handy to know the module_invoke function to call specific blocks so you can include them in your content without having to put them into the structure… however it does make the content harder to maintain as you’ll have to remember where you’re calling the blocks.

  1. cman
    April 22nd, 2015 at 00:54
    Reply | Quote | #1

    Thanks for this post. I was having exact same issue!

TOP