I'm having trouble setting header text dynamically in my custom views using the available hooks. I've seen others with similar issues (e.g. https://drupal.org/node/749452 + comments), but no concrete solution.

I can set fixed headers from hook_views_pre_view() (but NOT hook_views_pre_render()) like so:

    $options = array(
      'id' => 'area',
      'table' => 'views',
      'field' => 'area',
      'empty' => FALSE,
      'content' => $headertext,
      'format' => 'filtered_html',
      'tokenize' => 0,
    );
    $view->display_handler->set_option('header', array('text' => $options));

But what I really need is the same thing from hook_views_pre_render() (or similar) so that I can display stats on the number of results etc, data not available until after the query is executed. I could use the php filter via the GUI to do what I need, but I'm developing a module and don't want to force users allow that.

This worked from hook_views_pre_render():

 $header_options = array(
    'label' => t('Header text'), // Administrative label. Not really needed here.
    'content' => $headertext, // Content of header text.
    'format' => 'filtered_html', // Format of header text.
    'empty' => 1, // Show even when there are no results. Set to zero otherwise.
  );
  $view->add_item('default', 'header', 'views', 'area', $header_options);

... but only for 'default'. Also, if I used default and just changed the text dynamically, other displays get messed up such that, for example, clicking on pager links in the child displays causes a reversion to the default.

It seems like I'm missing something obvious, any pointers?

Comments

Tony Stratton’s picture

Just curious if any solution was found to this problem? I'm having the exact same issue. Thanks!

Tony Stratton’s picture

I'm still curious.. but this appears to have worked within hook_views_pre_render():

$view->attachment_before = $header_content;
pinin4fjords’s picture

Sorry I didn't see your replies earlier. I hadn't found a solution at all so I couldn't have helped you, but your solution works for me too - so thank you!

drasgardian’s picture

The solution from #2 doesn't appear to work when there are no results from the view.

spesic’s picture

@dragardian if you add a no results behavior to your view, it should work.

MustangGB’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.