5

I have added 1 view display into my view's footer via "Global: View area | The view to insert into this area." and a regular "Global: Text area".

So my view has 2 footer displays, one being another view display and the other a normal text area.

Now I want to unset/hide either one of the footer display based on a condition, perhaps via hook_views_pre_render()

I was expecting to find documentation on something like:

$view->display['MY_VIEW_DISPLAY_ID']->handler->handlers['footer']['FOOTER_DISPLAY_ID']->unset_display();

But obviously that is not the way to go. Is it possible to accomplish that via hook_views_pre_render() or any other means? If so, how?

2 Answers 2

10

The snap code below has been tested only on D7. In case this code does not work on D6, check out the comments on this post here.

function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'VIEW_NAME' && $display_id == 'DISPLAY_ID') {

    // To find out the DISPLAY_NAME, FIELD_NAME and FIELD_VALUE use the devel module function
    // below to print them out.
    // Replacing 'footer' by 'header' works the same way.
    dpm($view->display_handler->options['footer']['DISPLAY_NAME']['FIELD_NAME']);

    $view->set_item_option($display_id, 'footer', 'DISPLAY_NAME', 'FIELD_NAME', 'FIELD_VALUE');

    // In this case scenario you needed to unset the footer display.
    // For the embedded view display you do like this:
    $view->set_item_option($display_id, 'footer', 'DISPLAY_NAME', 'view_to_insert', '');
    // For the normal text area display you do like this:
    $view->set_item_option($display_id, 'footer', 'DISPLAY_NAME', 'content', '');
  }
}

Due to comments, to determine what views name and display came to this function, make next (of course, "devel" module should be enabled)

    function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {
      dsm($view->name);
      dsm($display_id);
      if ($view->name == 'VIEW_NAME' && $display_id == 'DISPLAY_ID') {
...
      }
    }

And update views page.

3
  • The snap code you've posted initially did not work but the link you posted along with it led me to the answer I was looking for. Thank you. I have updated your answer. Commented Feb 11, 2013 at 7:53
  • To find out the DISPLAY_NAME, you can use dpm($view->display_handler->get_option('footer'));.
    – joe_flash
    Commented Jan 23, 2014 at 16:27
  • Make sure you are implementing hook_views_pre_view() --- this code only seems to work there, and not in other hooks like hook_views_pre_render().
    – mcaleaa
    Commented Mar 7, 2015 at 15:52
2

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

/**
 * Implements hook_views_pre_render().
 */
function MYMODULE_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']);
}

Here is another example using hook_views_pre_view:

/**
 * Implements hook_views_pre_view().
 */
function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'my_view') {
    // Unset: Global: Text area
    $view->display_handler->set_option('empty', NULL);

    /* Programatically update the 'No results behavior' by inserting another view. */

    // Example 1
    $options = array(
        'id' => 'view',
        'table' => 'views',
        'field' => 'view',
        'empty' => TRUE,
        'view_to_insert' => 'new_view:page',
    );
    $view->display_handler->set_option('empty', array('view' => $options));

    // Example 2
    $view->set_item_option($display_id, 'empty', 'view', 'id', 'view');
    $view->set_item_option($display_id, 'empty', 'view', 'table', 'views');
    $view->set_item_option($display_id, 'empty', 'view', 'field', 'view');
    $view->set_item_option($display_id, 'empty', 'view', 'empty', TRUE);
    $view->set_item_option($display_id, 'empty', 'view', 'view_to_insert', 'new_view:page');
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.