I have a Drupal 7 site and I would like to change the view format with a button click, so I want to ask if it is a way to change the configuration of a View page with php.

In a view page configuration, the option is this:

enter image description here

Now it has "List without format" value.

  • Just to clarify, you're asking how to changes a view format configuration with PHP code?
    – Shawn Conn
    May 10, 2018 at 16:38
  • The idea is this: I have a results and two buttons: one button to show the results in a grid and the other to show in a list. Something like this: vandelaydesign.com/list-grid-view-switch but fro Drupal7 May 10, 2018 at 17:02

1 Answer 1


It is possible to call a View using PHP code. It is also possible modify a view using code such as hook_views_pre_view() to change how a View is displayed dynamically.

I actually wouldn't do either for your request. Simply do the following:

  • In your View make 2 displays.
    • 1 view display that shows the Grid format.
    • 1 view display that shows the list format.

On the page in question use an HTML button, html link, or even a Rules Link that uses Ajax to reload the div on the page and calls the view with the correct display mode you want. Your ajax call will be calling like: $view = views_embed_view('MY_VIEW_NAME', 'display_id'); and updating the page with $view contents via Ajax.

So make 2 view displays, and learn how to call them using Ajax.

Your Answer

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

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