Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Porting views2 to views3, i ran into problems while assigning footer dynamically.
The below code which works well in views2 does not work in views3
function myview_views_pre_render(&$view) {
$footer_text = "This is dynamic footer";
$view->display_handler->set_option('footer',$footer_text);
}
Taking example from 88 in views_plugin_display.inc i changed the above code to what it looks like below, but without any luck.
$options = $view->display_handler->get_option('footer');
$format = $view->display_handler->get_option('footer' . '_format');
$options['area'] = array(
'id' => 'area',
'table' => 'views',
'field' => 'area',
'relationship' => 'none',
'group_type' => 'group',
'label'=>'',
'empty'=> NULL,
'content'=>$view_footer,
'format' => !empty($format) ? $format : variable_get('filter_default_format', 1),
);
$view->display_handler->set_option('footer',array('text'=>$options));
I am still looking into the code in further detail, was wondering if anyone else had similar problems.
Comments
Comment #1
dawehner$view->set_item($displayid, $type, $id, $item) where $type = 'footer' should work too.
But much easier you could implement a area plugin.There you generate the dynamic data and enable this area plugin in the view.
Comment #2
Ravi.J CreditAttribution: Ravi.J commentedHi Dereine,
Thanks for information, That worked for me nicely.
I did notice that $view->set_item only works when $displayid is set to default, and does not work with 'page_1'.
Is that because in views3 dynamic footers can be set through area plug-in implementation ?
Comment #3
Ravi.J CreditAttribution: Ravi.J commentedComment #4
kenorb CreditAttribution: kenorb commentedUsing Views 3 with example of adding some text to existing header programatically:
Comment #5
bentekwork CreditAttribution: bentekwork commentedTook me a bit to figure this out so hopefully this can save someone some time (I chose to do this operation in hook_views_pre_view so I would have easy access to the display_id):
How to set a header or footer using set_option in Drupal 7
Comment #6
chx CreditAttribution: chx commentedFor Drupal 7, this worked for me:
Comment #7
herve CreditAttribution: herve commentedAll of this solutions doesnt work for me. Any ideas? I tried #6, #5 and #4.
Regards,
herve.
Comment #8
djdevin@herve: #5 worked for me, D6/Views3
Comment #9
bob.hinrichs CreditAttribution: bob.hinrichs commentedNor could I get any of these to work. Though this method is not exactly answering your question, this succeeds in placing text after the view, and is very simple to achieve. Example:
Comment #10
jfrederick CreditAttribution: jfrederick commentedThe solution in #9 worked for me. In D7 I could not get #5 or #6 to work. Thanks much, bob!
Comment #11
TuWebO CreditAttribution: TuWebO commentedChanging view header in D7 and Views 3:
This is working for me:
IMPORTANT: While this code works fine for hook_view_pre_view, same code (changing $display_id for $view>current_display) in hook_view_pre_render won't work.
Hope this helps, since I spent some time trying to figure it out!
Comment #12
bpadaria CreditAttribution: bpadaria commentedThanks TuWebO,
Your given "IMPORTANT" stuff helped.
You saved my day.
Comment #13
a.milkovskyJust have found good D7 example here:
http://smartwolverine.net/article/drupal-7-programmatically-inject-arbit...
If you don't want to unset footer, but add another value you should do:
Comment #14
bburgI tried #13 and it changed my fields based view to display rendered nodes with a full content display mode...
Comment #15
ann b CreditAttribution: ann b commentedAdding some text to existing header programmatically.
This worked for me in D7 in hook_views_pre_view:
Please note I'm using a Global: Result Summary in the header.
*
The problem with the above hook is the exposed input filter values aren't available yet. So I needed hook_views_pre_render for my case.
Thanks to this post. It was a lifesaver!
Comment #16
generalconsensus CreditAttribution: generalconsensus commentedCorrection to both of the items above. This works 100%:
Comment #17
init90This code works fine (Drupal 8.1.3)
Comment #18
k_bouchek CreditAttribution: k_bouchek commented#13 worked thanks a.milkovsky :)