How do you display Views' exposed filters in a block? Drupal6!!

BartVG - August 3, 2008 - 20:38

Inspired by http://drupal.org/node/213069, I tried converting this code for use in D6. This is my result:

<?php

$view
= views_get_view('frontpage2');
$form_state['view'] = $view;
$form_state['display'] = $view->display['default'];
$form = drupal_retrieve_form('views_exposed_form', $form_state);
$form['#action'] = url($view->display['page']->display_options['path']);
drupal_process_form('views_exposed_form', $form, $form_state);
return
drupal_render_form('views_exposed_form', $form);
?>

On the positive side: it works without breaking the site and it gives only 1 php warning.
On the negative side: only the submit button is showed. I think it has something to do with the $form_state array. I explicitly assign $view and $view->display['default'] to that array, but after that it seems to be the only content of it. I guess this array should come from previous form building stages? At least it should have more content than only the two variables that I assing to it. Does someone know where to get the rest of the array?

No progression

BartVG - August 4, 2008 - 18:03

I'm also not sure whether the view object contains what it should contain:

view Object
(
    [db_table] => views_view
    [base_table] => node
    [args] => Array
        (
        )

    [use_ajax] =>
    [pager] => Array
        (
            [use_pager] =>
            [items_per_page] => 10
            [element] => 0
            [offset] => 0
            [current_page] => 0
        )

    [exposed_data] => Array
        (
        )

    [exposed_input] => Array
        (
        )

    [vid] => 2
    [name] => frontpage
    [description] => Emulates the default Drupal front page; you may set the default home page path to this view to make it your front page.
    [tag] => default
    [view_php] =>
    [is_cacheable] => 0
    [display] => Array
        (
            [default] => views_display Object
                (
                    [db_table] => views_display
                    [vid] => 2
                    [id] => default
                    [display_title] => Defaults
                    [display_plugin] => default
                    [position] => 1
                    [display_options] => Array
                        (
                            [sorts] => Array
                                (
                                    [sticky] => Array
                                        (
                                            [id] => sticky
                                            [table] => node
                                            [field] => sticky
                                            [order] => ASC
                                        )

                                    [created] => Array
                                        (
                                            [order] => DESC
                                            [granularity] => second
                                            [id] => created
                                            [table] => node
                                            [field] => created
                                            [relationship] => none
                                        )

                                )

                            [defaults] => Array
                                (
                                    [sorts] =>
                                    [filters] =>
                                    [access] =>
                                    [use_ajax] =>
                                    [items_per_page] =>
                                    [offset] =>
                                    [use_pager] =>
                                    [pager_element] =>
                                    [style_plugin] =>
                                    [style_options] =>
                                    [row_plugin] =>
                                    [row_options] =>
                                )

                            [filters] => Array
                                (
                                    [promote] => Array
                                        (
                                            [id] => promote
                                            [table] => node
                                            [field] => promote
                                            [operator] => =
                                            [value] => 1
                                            [group] => 0
                                            [exposed] =>
                                            [expose] => Array
                                                (
                                                    [operator] =>
                                                    [label] =>
                                                )

                                        )

                                    [status] => Array
                                        (
                                            [id] => status
                                            [table] => node
                                            [field] => status
                                            [operator] => =
                                            [value] => 1
                                            [group] => 0
                                            [exposed] =>
                                            [expose] => Array
                                                (
                                                    [operator] =>
                                                    [label] =>
                                                )

                                        )

                                    [type] => Array
                                        (
                                            [operator] => in
                                            [value] => Array
                                                (
                                                    [poll] => poll
                                                    [story] => story
                                                )

                                            [group] => 0
                                            [exposed] =>
                                            [expose] => Array
                                                (
                                                    [operator] =>
                                                    [label] =>
                                                )

                                            [id] => type
                                            [table] => node
                                            [field] => type
                                            [relationship] => none
                                        )

                                    [tid] => Array
                                        (
                                            [operator] => or
                                            [value] => Array
                                                (
                                                    [6] => 6
                                                    [7] => 7
                                                    [9] => 9
                                                    [8] => 8
                                                    [13] => 13
                                                    [10] => 10
                                                    [11] => 11
                                                    [12] => 12
                                                )

                                            [group] => 0
                                            [exposed] => 1
                                            [expose] => Array
                                                (
                                                    [use_operator] => 0
                                                    [operator] => tid_op
                                                    [identifier] => trefwoord
                                                    [label] => Trefwoord
                                                    [optional] => 1
                                                    [single] => 1
                                                    [remember] => 1
                                                    [reduce] => 0
                                                )

                                            [type] => select
                                            [vid] => 3
                                            [id] => tid
                                            [table] => term_node
                                            [field] => tid
                                            [hierarchy] => 1
                                            [relationship] => none
                                            [reduce_duplicates] => 1
                                        )

                                )

                            [access] => Array
                                (
                                    [type] => none
                                    [role] => Array
                                        (
                                        )

                                    [perm] =>
                                )

                            [header_format] => 1
                            [footer_format] => 1
                            [empty_format] => 1
                            [use_ajax] => 1
                            [items_per_page] => 10
                            [offset] => 0
                            [use_pager] => 1
                            [pager_element] => 0
                            [style_plugin] => default
                            [style_options] => Array
                                (
                                )

                            [row_plugin] => node
                            [row_options] => Array
                                (
                                    [teaser] => 1
                                    [links] => 1
                                )

                        )

                )

            [page] => views_display Object
                (
                    [db_table] => views_display
                    [vid] => 2
                    [id] => page
                    [display_title] => Page
                    [display_plugin] => page
                    [position] => 2
                    [display_options] => Array
                        (
                            [path] => frontpage
                            [menu] => Array
                                (
                                    [type] => none
                                    [title] =>
                                    [weight] => 0
                                )

                            [tab_options] => Array
                                (
                                    [type] => none
                                    [title] =>
                                    [weight] => 0
                                )

                        )

                )

            [feed] => views_display Object
                (
                    [db_table] => views_display
                    [vid] => 2
                    [id] => feed
                    [display_title] => Feed
                    [display_plugin] => feed
                    [position] => 3
                    [display_options] => Array
                        (
                            [sitename_title] => 1
                            [title] => Front page feed
                            [defaults] => Array
                                (
                                    [title] =>
                                    [sitename_title] =>
                                )

                            [style_options] => Array
                                (
                                    [mission_description] => 1
                                    [description] =>
                                )

                            [row_plugin] => node_rss
                            [row_options] => Array
                                (
                                    [item_length] => default
                                )

                            [path] => rss.xml
                            [menu] => Array
                                (
                                    [type] => none
                                    [title] =>
                                    [weight] => 0
                                )

                            [tab_options] => Array
                                (
                                    [type] => none
                                    [title] =>
                                    [weight] => 0
                                )

                            [displays] => Array
                                (
                                    [default] => default
                                    [page] => page
                                )

                        )

                )

        )

    [type] => Overridden
    [loaded] => 1
    [executed] =>
    [built] =>
    [build_info] => Array
        (
        )

    [attachment_before] =>
    [attachment_after] =>
)

Exposed inputs are not available (see views.module line 801 and includes/view.inc line 157 in views-6.x-2.0-rc1) when I call the view like this. The frustrating thing is that the exposed form does get rendered in the page view. :-(
Also, there are no filters in the object (see views.module line 816).

I'm giving up for now. Things get too complicated and I can't seem to find a solution. Ik word er stiepelzot van!!

Grrrr

mdixoncm - August 15, 2008 - 20:52

I've just been struggling with the same problem - but I think I've cracked it!

  $view = views_get_view('your_view_name');
  $view->set_display('default');
  $view->init_handlers();
  $form_state = array(
    'view' => $view,
    'display' => $view->display_handler->display,
    'method' => 'get',
    'rerender' => TRUE,
    'no_redirect' => TRUE,
  );
  $output = drupal_build_form('views_exposed_form', $form_state);
  return $output;

Not been tested in anger, but it certainly seems to be working ...

Mike,
Computerminds offer Drupal development, consulting and training

Thanks a lot!!

BartVG - August 16, 2008 - 19:32

Now it's working here too!
If you were a girl, I would ... euhm ... nevermind...

Works wonderfully,

brendoncrawford - December 23, 2008 - 06:46

set_display()

radj - January 1, 2009 - 05:38

Posted at computerminds but just reposted here just in case someone's browsing.

I get this PHP error when I use the code above:

Fatal error: Call to a member function set_display() on a non-object in drupal\includes\common.inc(1547) : eval()'d code on line 4

What am I doing wrong? Using D6.6.

didn't change the your_view_name

radj - January 1, 2009 - 06:01

Woops! If you encounter an error like mine, you forgot to change the view. LOL. This might need error checking then to prevent PHP compile errors. ^_^

  $view = views_get_view('taxonomy_term');
  if($view){
    $view->set_display('default');
    $view->init_handlers();
    $form_state = array(
      'view' => $view,
      'display' => $view->display_handler->display,
      'method' => 'get',
      'rerender' => TRUE,
      'no_redirect' => TRUE,
    );
    $output = drupal_build_form('views_exposed_form', $form_state);
  }else{
    $output = ""; // error messages or something...
  }
  return $output;

Another thing, how do you stop the filters from showing up on the original place?

Not working with view display other than 'default'

sagannotcarl - March 23, 2009 - 00:27

Thanks for this code. It works great for the default display of a view but no matter what you put in the $view->set_display(); it still goes and looks up the default display.

Any tips?

Thanks!

[ Quilted, Stitching Together Technology and Social Change ]

_

WorldFallz - March 23, 2009 - 01:29

You don't need to code this-- you can place the exposed filters in a block with the 'Exposed form in block' option in the Basic settings box.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

Exposed filter block in panels does not seem to work

SocialNicheGuru - April 25, 2009 - 08:04

I am going to try this piece of code and report back to see if it works.

If I use exposed form block in a panel, it always returns the wrong url. I hope I can set it with this code.

Chris

--
Bringing value to the social web by connecting people with events, products, and services that match their interests and values

set exposed filter url and $view->set_display

SocialNicheGuru - May 7, 2009 - 17:39

I would like to pass in the url of the page the view is on.

exposed filters blocks do not work in a panel or a situation where you use node/%/foo as the url.

I am hoping that I could modify the code to ensure

1. that the url passed is the url of the given page
2. set $view->set_display() to take another value other than default

Any ideas?

--
Bringing value to the social web by connecting people with events, products, and services that match their interests and values

I'd love to work through this

CWolff - June 30, 2009 - 18:37

I'd love to work through this with anyone that's interested in starting a code scrum to figure it out.

------------------------
Cassandra Wolff
Senior Technical Engineer
HiDefWeb LLC.
cassandra@hidefweb.com

Here's the magical thing!

paganwinter - October 3, 2009 - 06:09

Set the 'expose form in block' and voila, the form disappears from the page view!
(though it never appeared in the block view in the first place)

Also, I used the above code snippet in a custom block (PHP filter - and then did a "print $output" instead of the "return $output")

Thank you so much for this.

do it different way by accident

najibx - November 15, 2009 - 01:03

Hi ...I need both expose filter on it's own view's page (expose filter on on top of the results) and expose filter in a block when actually viewing the content. But after searching via this expose block, this expose block disappear.

By accident I was able to do this without using the snippets. Here's how I do it ...

1. Initially Exposed form in block: Yes. With this is, it create expose block, but REMOVE expose filter from view's page.
2. Enable the block from this expose block only on the pages detailed. (use block visibility but not on the view's page). Otherwise you'll have redundant filter (in the block and on top of the page)
3. Now, set .... Exposed form in block: No. Now, set it back into No so I can also have the filter on the view's page as well.

I have cleared cache & view's cache. Seems all working as I wanted. comment?

_

WorldFallz - November 15, 2009 - 13:00

Sounds like a bug to me-- I don't know if I would rely on it as it might get fixed one day. But whatever works, lol.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

 
 

Drupal is a registered trademark of Dries Buytaert.