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

dawehner’s picture

$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.

Ravi.J’s picture

Hi 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 ?

Ravi.J’s picture

Status: Active » Closed (fixed)
kenorb’s picture

Using Views 3 with example of adding some text to existing header programatically:

function hook_views_pre_render(&$view) {
  $header_item = $view->get_item('default', 'header', 'text');
  $view->set_item_option('default', 'header', 'text', 'content', 'MY TEXT' . $header_item['content']);
}
bentekwork’s picture

Took 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


//I had a view with 3 attachments so I used the $display_id to limit the function
function YOURMODULENAME_views_pre_view(&$view, &$display_id, &$args) {

	if($view->name == 'YOURVIEWNAME' && $display_id == 'YOURDISPLAYID') {
                $footer = "This is the text that I want in my footer!!!!";
		$options = array(
		    'id' => 'area',
		    'table' => 'views',
		    'field' => 'area',
		    'empty' => FALSE,
		    'content' => $footer,
		    'format' => 2,
		    'tokenize' => 0,
		);
		$view->set_item('YOURDISPLAYID', 'footer', 'area', $options);

       }
}
chx’s picture

For Drupal 7, this worked for me:

    $options = array(
      'id' => 'area',
      'table' => 'views',
      'field' => 'area',
      'empty' => FALSE,
      'content' => $footer,
      'format' => 'filtered_html',
      'tokenize' => 0,
    );
    $view->display_handler->set_option('footer', array('text' => $options));
herve’s picture

All of this solutions doesnt work for me. Any ideas? I tried #6, #5 and #4.

Regards,

herve.

djdevin’s picture

@herve: #5 worked for me, D6/Views3

function custom_views_pre_view($view, $display_id, $args) {
  if ($view->name == 'my_view' && $display_id == 'page_2') {
    $footer = 'some text';
    $options = array(
      'id' => 'area',
      'table' => 'views',
      'field' => 'area',
      'empty' => FALSE,
      'content' => $footer,
      'format' => 2,
      'tokenize' => 0,
    );
    $view->set_item('page_2', 'footer', 'area', $options);
  }
}
bob.hinrichs’s picture

Nor 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:

function mjcc_global_views_pre_render(&$view, &$display_id = 'default', &$args = array()) {
  if($view->name == "commerce_cart_form" && $display_id == 'default' ) {
    $view->attachment_after = l('Add a New Mix', 'add-mix');
  }
}
jfrederick’s picture

The solution in #9 worked for me. In D7 I could not get #5 or #6 to work. Thanks much, bob!

TuWebO’s picture

Changing view header in D7 and Views 3:
This is working for me:

function my_module_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'view-name' && $display_id == 'current-display') {
    $view->set_item_option('$display_id', 'header', 'area', 'content', 'My custom text');
  }
}

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!

bpadaria’s picture

Thanks TuWebO,

Your given "IMPORTANT" stuff helped.
You saved my day.

a.milkovsky’s picture

Just 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:

/**
* Implements hook_views_pre_views_pre_view().
*/
function mymodule_views_pre_view($view, &$display_id, &$args) {
  if ($view->name == 'my_view') {
    $footer = 'Super awesome footer content goes here';
    $view->add_item('default', 'footer', 'views', 'area', array('content' => $footer, 'format' => 'full_html'));
  }
}
bburg’s picture

Issue summary: View changes

I tried #13 and it changed my fields based view to display rendered nodes with a full content display mode...

ann b’s picture

Adding some text to existing header programmatically.

This worked for me in D7 in hook_views_pre_view:

$header = $view->display_handler->get_option('header');

$header['result']['content'] .= ' more text';

$view->display_handler->set_option('header', $header);

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.

$header = $view->display_handler->get_option('header');

$header_content = $header['result']['content'] . (do something with $view->exposed_input);

$view->display["$view->current_display"]->handler->view->header['result']->options['content'] = $header_content;

Thanks to this post. It was a lifesaver!

generalconsensus’s picture

Correction to both of the items above. This works 100%:

/**
 * Implements hook_views_pre_views_pre_view().
 */
function herp_views_pre_view($view) {
  if ($view->name == 'herp' && $view->current_display == 'derp') {
    $footer = $view->display_handler->get_option('footer');

    $footer['result'] = array(
      'id' => "area",
      'table' => "views",
      'field' => "area",
      'relationship' => "none",
      'group_type' => "group",
      'ui_name' => "",
      'label' => "View Your Derpy Herp",
      'empty' => 0,
      'content' => 'View Your Derpy Herp',
      'format' => "filtered_html",
      'tokenize' => 0
    );

    $view->display_handler->set_option('footer', $footer);
  }
}

DovganPavlo’s picture

This code works fine (Drupal 8.1.3)

**
 * Implements hook_views_pre_view().
 */
function MODULE_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  if ($view->id() == 'VIEW_ID' && $display_id == 'VIEW_DISPLAY_ID') {
     
      $options = array(
        'id' => 'area_text_custom',
        'table' => 'views',
        'field' => 'area_text_custom',
        'relationship' => 'none',
        'group_type' => 'none',
        'admin_label' => '',
        'empty' => TRUE,
        'tokenize' => FALSE,
        'content' => "HIRE CONTENT",
        'plugin_id' => 'text_custom',
      );

      $view->setHandler('VIEW_DISPLAY_ID', 'footer', 'area_text_custom', $options);
  }
}