3

I am using D7 and views for one of my sites. In views I am using contextual filters to get the result based on the url arguments. My question how to display a text like "No results found" in the views if I don get any match based on the url.

3

It's under 'Advanced' on the right hand side. Then 'add' a NO RESULTS BEHAVIOUR.

4
  • Thanks a lot alex:) Ur answer helped me to solve my issue:)
    – user4081
    Jul 9 '12 at 8:08
  • Your welcome, have a good day :)
    – Alex Gill
    Jul 9 '12 at 8:09
  • 1
    How to do that on multi lingual site?
    – deem
    Aug 12 '13 at 13:31
  • If i want to add tokens then how to add.
    – Fawwad
    Jul 30 '15 at 5:33
3

You can also add a "no results behavior" view element in code in your own module.

That's useful if the element has to be dynamic – in the example below, it's a configurable button to add content to the view. (Content is added to an OG group with a node ID supplied as contextual filter parameter to the view.)

/**
 * Implements hook_views_pre_render().
 */
function MYMODULE_views_pre_render(&$view) {
  if($view->name != 'my_view_machine_name') {
    return;
  }

  if (isset($view->args[0])) {
    $target_group_nid = $view->args[0];
    $button_html = _generateButtonHtml($target_group_nid);

    // Create a "Global: Textarea" view element.
    $options = array(
      'id' => 'button2',
      'table' => 'views',
      'field' => 'area',
      'empty' => FALSE,
      'content' => '<div>No results yet. Add one:</div>' . $button_html,
      'format' => 'filtered_html',
      'tokenize' => 0,
    );
    $view->set_item('default', 'empty', 'button2', $options);
  }
}

Note: _generateButtonHtml() is just a stub for your own function to return HTML code for a button.

Relevant API docs:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy