0

I have inherited a drupal site with a few modules. They're not exactly written according to best practices... they return html code instead of renderable arrays. In other words, they do this:

function custommodule(){
 $output = '<p> Some output. </p> <p> Some more output. </p>';
 return $output;
}

Instead of this:

function custommodule(){
   $args = array( arg1, arg2 );
   return theme('sometheme', $args);
}

The output is supposed to be displayed in the content region. However, since plain html is returned that is not wrapped in a node or anything like that, the output gets scattered all over my page... for example, the text Some output. appears above the navigation, Some more output. appears underneath the footer, etc.

By the way, the page-array has the returned markup (the $output from above) in this place:

page['content']['system_main']['main']['#markup']

Whereas for example a typical article would have its #markup in

page['content']['system_main']['nodes'][$nid]['body'][0]['#markup']

So I guess drupal doesn't realize I would like my output to be in the content region?

Is there any way I can safely render that unpleasant html?

Thanks a lot in advance!!

  • Take it you dont have hook_menu with a page callback? – Alex Gill Feb 2 '15 at 10:40
  • If they're custom modules can't you just rewrite and fix them? – rooby Feb 2 '15 at 10:43
  • Is the markup hard coded like that or coming from user input of some sort? If it's hard coded then it is already safe to render (unless you have malicious code in your hard coded markup). – rooby Feb 2 '15 at 10:45
  • Returning plain HTML doesn't cause things to be broken or print in the wrong place. With the information you have provided it's pretty hard to tell what your problem is or how to go about fixing it. – rooby Feb 2 '15 at 10:47
  • @AlexGill : I have a page callback; just omitted it for the sake of brevity. – Michael Langbein Feb 2 '15 at 11:08
0

Once again, the problem sat in front of the screen ;)

Like @rooby said, there was a mistake in the html returned. A missing </table> tag messed up my layout. Adjusting this solved my problem. Thanks!!

But to be able to influence the layout of custom html a bit more generally, I did this:

When a module returns output without first passing it through a theme function, it ends up in page['content']['system_main']['main']['#markup'], whereas in a normal page, there would be no #markup on this level of the page array, only elements like node, pager, #block etc. So finding page['content']['system_main']['main']['#markup'] means some custom code has been returned by some custom module.

So in my theme's template.php, I implemented

function mytheme_preprocess_page(&$variables, $hook) {
    if ( $variables['page']['content']['system_main']['main']['#markup'] ){
        $variables['page']['content']['system_main']['main']['#markup'] = '<div class="customcontent">' . $variables['page']['content']['system_main']['main']['#markup'] . '</div>';
 }
}

Thank you guys very much! Always awesome how good the feedback in the drupal community is.

  • Some time playing around later, it turns out the above mentioned thing is not a smart thing to do: there are some places in drupal where markup is intentionally placed in $page['content']['system_main']['main']['#markup']. The admin menu of the responsive_bartik theme would be one example. I just noted my theming via mytheme_preprocess_page also affected that area. Still, at least I've learned now where unrendered markup is usually placed in the page-array. – Michael Langbein Feb 2 '15 at 13:50

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.