I need to embed a views exposed filter form in a node create form, and I came across this code for doing it. The problem is, drupal_build_form (which is a views function) returns the rendered HTML, but since I'm in hook_form_alter, I need the data in array from so I can add it to the $form array. Is there another function or method I could call to get the exposed filter form data in array form?

Thanks.

Comments

wonder95’s picture

I got the form to display by calling views_exposed_form():

      $view = views_get_view('canned_search');
      $view->set_display('page_1');
      $view->init_handlers();
      $form_state = array(
        'view' => $view,
        'display' => $view->display_handler->display,
        'method' => 'get',
        'rerender' => TRUE,
        'no_redirect' => TRUE,
      );
      $exposed_filter_form = views_exposed_form(&$form_state);
      $form['exposed_filter_form'] = $exposed_filter_form;

Then, I specified a submit function and added two more textareas to the form:

      $form['exposed_filter_form']['#submit'] = 'feature_package_submit';
      $form['submit']['#weight'] = 10;
      //add textareas for displaying filter results and selected items for package
      $form['exposed_filter_form']['search_results'] = array(
        '#type' => 'textarea',
        '#title' => 'Results',
        '#weight' => -1,
      );
      $form['exposed_filter_form']['package_content'] = array(
        '#type' => 'textarea',
        '#title' => 'Package Content',
        '#weight' => -1,   
      );

The next trick is getting the search_results area to be filled with data returned by the view that uses the exposed filters. I'm assuming I'll need to call views_exposed_form_submit() to get the data, and then write it to the textarea. Is that the correct function to call?

Thanks.

dawehner’s picture

There is $view->set_exposed_input($filters); But i'm not sure how $filters looks exact.

I suggest to create another view, and get the $view object and have a look at $view->exposed_input. After this, you can use the function.

merlinofchaos’s picture

They will look just like they do from $_GET where the 'key' to the filter is the identifier that is set in the UI. It is pretty easy to construct exposed filter input this way.

wonder95’s picture

OK, so that tells me how to get the filter data, and set it. It sounds like I read from $_GET and create $filters, and call $view->set_exposed_input.

How do I go from there? I'm using the multiselect widget to create my target field, so I want to put the results of the view in the left field when I submit the form. Do I specify my own submit function and then call views_exposed_form_submit? How do I get the data returned from the view so I can put it in that field?

Thanks.

wonder95’s picture

Status: Active » Closed (fixed)
tema’s picture

Status: Closed (fixed) » Active

Sorry, I've make it active again.
@wonder95 Do you embed a view into a node form?
The method of exposed form is GET. How to get it working beside another elements of a node form?

esmerel’s picture

Status: Active » Closed (fixed)

Please do not reopen issues like this. if you need help, open a new issue, and reference this one.