4

I implemented a view as following :

  • a page display with an extensive exposed filters form (Better exposed filters), ajax enabled, let's call it the landing page
  • a clone of it, in a second page display with exactly the same fields, but few exposed filters instead (quick search form)
  • that clone has exactly the same path, Exposed form in block is set to yes

I display the clone created block (quick search form) on homepage : submitting its form redirects to landing page with correct results and the complete exposed filters. This is fine.

Question :

I need to display a results count next to the exposed form block, which refreshes each time you change a exposed filter value in the form.

The Global: Result summary in header or footer doesn't work because footer and header aren't displayed together with the exposed block, they only come with the full view display.

How could solve that, either with UI or programmatically ? Below some ideas to begin, there might be a good and straight way :

  • force the results count header to be displayed next to exposed block ?
  • add a markup to the form with ajax results ?
  • create another block ?

EDIT

I tried several approaches based on Shreya answer below, but the blocking point is always the same, I need to execute the view to get result count, but as soon as I do, it returns an Allowed memory size exhausted.

in template.php :

function shoot_preprocess_views_exposed_form(&$vars){

  if($vars['form']['#id'] == 'views-exposed-form-customer-products-list-search-block') {
    $view = views_get_view('MY_VIEW');
    $view->set_display('MY_DISPLAY');
    $view->execute();
    $count = count( $view->result );
    $vars['count'] = $count;
  }
}

in views-exposed-form--MY_VIEW.tpl.php

print 'There are:'. $count. 'results'; 

And other tries with views_get_current_view() or views_get_view_result() in the tpl, also tried within form alter :

function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  if($form['#id'] == 'views-exposed-form-my-view-my-display') {
    $view = views_get_view('MY_VIEW');
    $view->set_display('MY_DISPLAY');
    $view->get_total_rows = true; 
    $view->pre_execute(); 
    $view->preview('MY_DISPLAY');
    $view->execute(); 
    $content = $view->total_rows; 
  }
  $form['submit']['#value'] = 'Voir les'. $content.' résultats';
}

Any clue to explain why I can't execute the view, or how to return count in that context ?

5
  • you can alter the the exposed form tpl and print the total count May 3, 2016 at 13:03
  • Next to exposed form block means ..before the views results are displayed ..am i correct May 3, 2016 at 13:11
  • Yes, I want to display a result counter next to exposed form, before submitting and redirecting to results page. Counter refreshed whenever any exposed filter is changed...
    – Kojo
    May 3, 2016 at 13:28
  • $view = views_get_view('my_view'); $view->set_display("page_1"); ///$view->set_arguments($er); $view->get_total_rows = true; $view->pre_execute(); $view->execute(); $content = $view->total_rows; $form['mymodule'] = array( '#type' => 'textfield', '#title' => t('title'), '#value' => $content, ); this works – Use form alter May 5, 2016 at 11:36
  • @Shreya, tks but if I understand your proposal, it doesn't work. Wherever I execute the view in exposed block context, it ends with a memory size exceeded. And I try with a very very simple basic view and few results for dev purpose. Please edit your answer to expose the last solution you think is working
    – Kojo
    May 8, 2016 at 9:44

2 Answers 2

1
  1. Create a folder structure as sites/all/themes/example_theme/templates/views

2.Create a file named views-exposed-form.vars where you use MYTHEME_preprocess_views_exposed_form. Code snippet below:

function example_theme_preprocess_views_exposed_form(&$vars){
  if($vars['form']['#id'] == 'views-exposed-form-exaample-page') {
    $views_rows = views_get_view_result('my_custom_view', 'page');
$count_rows = count($views_rows);
   $vars['count'] = $count_rows;
  }

}
  1. Then you can override the exposed filter tpl. To override the exposed form template for a particular view, you need to create a template in the following name pattern,

    views-exposed-form--view_name.tpl.php

    and print the $count variable wherever you want to place the count. Once you add the template clear the cache.

7
  • Did you try this?? And keep use ajax to Yes May 4, 2016 at 6:15
  • Yes I tried this pm, thanks for your answer. First, I had never heard of .vars files before (assuming .vars.php). Googling gives results almost especifically in Bootstrap context . I tried anyway, but the code isn't processed.
    – Kojo
    May 4, 2016 at 17:38
  • So I tried to put that function in template.php. Then another problem is, you can count rows of any display of the view except for the currently exposed block, it returns memory fatal error. I made extensive tries, changing the display with exposed form block but it's always the same. you can't execute and count the one exposed.
    – Kojo
    May 4, 2016 at 17:38
  • $view = views_get_current_view() did you try this May 4, 2016 at 17:51
  • No, but I tried $view = views_get_view('customer_products_list'); $view->set_display('partir'); $view->execute(); $count = count( $view->result ); and it gives same error when I execute the view. What about the .vars.php file first please ?
    – Kojo
    May 4, 2016 at 17:54
0

You could try a front-end approach. In the views header you can show the total results (Global: Result Summary) in a hidden div and, using js, take the value from there and print it wherever you want.

Edit: I misuderstood your implemmentation. You could try using a block with exactly the same filters (hidden with css) you need, showing nothing but a header. You can adjust it to work with ajax, so the results will be refreshed and use js to change the filters with those changed in the exposed form in the block.

4
  • well as explained in question me the header is not showing up With an exposed form block display. No html output.
    – Kojo
    May 9, 2016 at 8:41
  • I've got many views with exposed forms in blocks and that doesn't have any effect in headers appearing or not. You could try using mefibs module which kind of improves better exposed filters. May 9, 2016 at 8:48
  • please read again my implementation. The header would appear in the main part of The View not in the exposed form block. and in order to have Xposed filters both in the block and in the target view, I have 2 displays not one. i'll have a look at that module but I would prefer coding because I already have a bunch of modules, this might be an overkill. Thank you
    – Kojo
    May 9, 2016 at 8:59
  • sorry, I changed my answer. I hope it can help. May 9, 2016 at 9:04

Your Answer

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

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