dpgui8229808 2012-02-23 00:55
浏览 45
已采纳

用php改变整个块html

I'm running a pretty large drupal 7 website.

I'd like to implement esi support on blocks by wrapping each block content with tags.

Now I can do that pretty easilly with theme_preprocess or hook_block_view_alter, but this only works if the block "content" variable passed to the drupal template is a string. If that variable is an array for instance it's imposible to wrap it in those tags, because this array is parsed in the template.

So the only way I could get it working is after getting the block value returned from the template. But I don't think the damn drupal even supports that... without hacking the core and outputting the tags before and after template incluision. But I really don't want that... so does anybody have an idea how would I be able to achieve this goal ?

Thank you very much!

  • 写回答

1条回答 默认 最新

  • douxingmou4533 2012-02-23 01:32
    关注

    If the block content is an array you can add a #prefix and #suffix to it:

    function mymodule_block_view_alter(&$data, $block) {
      if (is_array($data['content']) {
        $data['content']['#prefix'] = '<div class="my-class">';
        $data['content']['#suffix'] = '</div>';
      }
      else {
        $data['content'] = '<div class="my-class">' . $data['content'] . '</div>';
      }
    }
    

    The #prefix and #suffix will be honoured for any Drupal render array when it's passed through render() (or drupal_render()) so this should solve the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?