Drupal 7.x and Exposed AJAX Filter w/ Default Argument

I have a Drupal 7.x site and inside template.php, I’ve tapped into an AJAX-enabled block view using HOOK_form_alter() to assign a default value to an exposed form filter from the view’s block-view (not the page-view).

2 files currently play a part in all this: template.php and foobar.js. Besides initializing the Drupal behaviors jQuery, template.php also assigns the default value to the input text box the exposed form has inside the block. Below is the code for both files:

template.php -

function foobar_form_alter(&$form, &$form_state, $form_id) {
        if ($form['#id'] == 'views-exposed-form-foobar-block') {//If this is the view's block-view...
            $form_state['input']['title_1'] = 'Foo';//Assign default value of "foo" to the text box.

foobar.js -

(function ($) {
    Drupal.behaviors.foobarPage = {
        attach: function(context, settings){
            $('#views-exposed-form-foobar-block').ready(function() {

When a user types into the exposed form’s text box and the AJAX is triggered, the form submits and the user is taken to the view’s page-view. What I want is to have the block-view return a result set and the user remain on the original page but I want all this to happen as soon as this page loads (i.e. - when it’s accessed or refreshed)…

There’s all sorts of array configuration values I can use to adjust certain aspects of the exposed form but judging from all the stuff I’m reading on the internet, I can’t really find a good confirmation about how doing what I’m trying to do, is done. I found these Drupal.org pages (https://drupal.org/node/1153780 and https://drupal.org/node/834952) that bring to light the issue I think I’m experiencing here, but so far, nobody has provided a solid answer.


Thought I might update this…

This is where I am now: I managed to get the view to display in the page via some code I added in template.php… But I need the block-view to be displayed because right now, it’s displaying the page-view.

The code I’m using in template.php is as follows:

//Inject view into main content...
   $name = 'foobar'; 
   $display = 'Block';
   $view = views_get_view($name); 

   $exposed_filters = array( 'title_1' => 'Foo');//This will be the default arg I'm going to pass into the exposed filter. This DOES work. (Will match "foobar", "foo-bar", "fooBar"; whatever--it's a CONTAINS filter arg... "title_1" is the name of the exposed filter text-box, btw.
   $view->exposed_input = $exposed_filters; 
   $view->exposed_raw_input = array_merge($exposed_filters, (array)$view->exposed_raw_input); 
   $view->exposed_data = $exposed_filters;

   $output = $view->preview(); 
   if ($view->result) { 
      $vars['page']['content']['system_main']['nodes'][83]['body'][0]['#markup'] = $output.$vars['page']['content']['system_main']['nodes'][83]['body'][0]['#markup'];

So as you can see, I’m trying to set the $display to be “Block”. It’s just not working, though… Any ideas why? The reason I know it’s not working is because in the views screen, I set all titles of the block-view to have “BLAH” pre-pended to them and it’s not showing up…

Figured it out…

(Keep in mind that the following code could have better validation with things like isset(), etc.–I just haven’t gotten to it yet.):

function HOOK_preprocess_page(&$vars){

    //Replace "HOOK" with either the name of your theme folder or else with the name of your module.

    $view_name = 'my_view';//Get this in the URL... It's the 4th word left of "admin" (i.e. - admin/structure/views/view/<NAME>)...
    $display_name = 'my_display';//The display name is in the upper-left corner of the main views screen.
    $exposed = 'foobar';//This is the exposed input you're using. If in the main views screen, go to the "Configure filter criterion[B]" [/B]screen for the respective field you wish to expose and then click on that "More" link to reveal the additional setting inputs (i.e. - admin title and the identifier). In this example, "foobar" appears to be the identifier that the code below relies on to set values for, so this is pretty important--I'm sure it can be done without specifying this, but it's just what I did.

    $view = views_get_view($view_name);
    $view->exposed_input['external_contact'] = 'God';//Pass in the default arg here! :.) In my situation, I was using an exposed filter that made use of a "CONTAINS" operator (hence the example value--I had nodes that started with the letters "GOD" and in so using these, I could verify if the logic matched anything. Has nothing to do with the almighty! :)
    $view->preview = TRUE;

    //Not sure how much of this is required... Documentation about these kinds of things on the Drupal docs is somewhat vague.
   $output = $view->display_handler->preview();

    //In my situation, I needed to put the contents of the block directly next to the main content I had on the page... This is where / how I did that.
    if (isset($output)) { 
      $vars['page']['content']['system_main']['nodes'][83]['body'][0]['#markup'] = $output.$vars['page']['content']['system_main']['nodes'][83]['body'][0]['#markup'];

I’ve heard a lot of people clamor about putting things like this in their own module folders… It’s something to consider. There’s pros and cons to doing that and it really just depends on what your requirements are. I usually put most of my “smallish” customizations in template.php (inside the theme folder) because for me, this usually equates to less maintenance. However, if functionality enhancements require larger pieces of code, I then move over into a module folder, etc. Again, it’s all about requirements.

Anyway, I hope this helps someone in the future. Suggestions for improvements are encouraged. :slight_smile: