开发者

Drupal views - splitting up the exposed form possible?

开发者 https://www.devze.com 2023-03-17 02:43 出处:网络
I need to display part of the exposed form in my page\'s sidebar, and the rest of the form and content in the $content area. There\'s 开发者_Go百科really no good way that I can find to do this. I sort

I need to display part of the exposed form in my page's sidebar, and the rest of the form and content in the $content area. There's 开发者_Go百科really no good way that I can find to do this. I sort of got it to show up in a way by making a "block" view with "exposed form" set and then trying to only show the part that i needed through .tpl files. The problem is that then, when the submit button is clicked (the submit button is in the $content area), then the filters that are in the sidebar are not taken into account.


Some lateral thinking... Why not explore CSS-only options? You can place that form element playing with position:absolute ? Or (considering is a right-sidebar) float:right and then some negative right margin to push it to the sidebar? If you are using 960 grid system, play with pull and push classes.


First I am going to answer your question, then I will explain why you are asking the wrong question:

If you build the form outside of the formapi, you might have some luck. This will get upgly and will require you to take a lot of extra care about attack-vectors such as mass-assignment.

views_some_view.tpl.php:

<form name="input" action="/link/to/view" method="get">
  Country: <input type="text" name="country" />

my_custom_exposed_view.module:hook_block() City:

That would make a form, which in most situations will start with <form>, have some input fields, then have a lot of random HTML, then some more input fields and then the closing .

As you may know, a <input type="submit" value="Submit" /> will only post everything of the form tags it is enclosed in. The submit button in the following HTML:

<form name="input_1" action="/link/to/view" method="get">
  Country: <input type="text" name="country" />
</form>
<form name="input_2" action="/link/to/view" method="get">
  City: <input type="text" name="city" />
  <input type="submit" value="Submit" />
</form>

will only send the City. These are not the droids you are looking for.

It will need to be one, big form, but since everything between form and /form is very dynamic, and contains a large quantity of HTML, including potential other forms, this is really not what you want. Moreover: a blocks appearance (shown/not-shown) is controlled completely independent of the content. You will need a lot of sturdy code to ensure the block a) never shows up when the starting form tag is not present, and b) the block will guaranteed to be shown when that opening form tag is present. Else you have not just invalid HTML, but broken HTML that will truly render your page unusable in most cases.

You simply don't want a part of the form in a block and the other part in the content.

However, you want it visualised as if one part is in the body, the rest in a sidebar.

The good news, is that with HTML presentation structure are independant. That is where your solution lies.

  1. Give your form-fields good ids and classes. You could use a hook_form_alter to change existing forms, but you probably simply just want to create the HTML for that entire form yourself. The theme layer allows that.
  2. Use CSS to pick out either single form-fields by ID and position:absolute them into the correct place. Or pick out classes of fields by CLASS and position:relative them into the correct place.
  3. Make a simple identification-routine that allows adding a class to the body-tag. (see below).
  4. Add some CSS to shift the sidebar lower, making space for the form-fields to be moved in, when that class is in the body-tag.

    <body class="<?php print $splitform ?>">

    function my_themename_preprocess_page() {
      if ($GET['q'] == 'path/to/view') {
        $vars['spliform'] = "splitform"
      }
    }


From the above explanation I am assuming that you are printing same form in block and in content area and you are hiding some part of form in page.tpl , if this is true then you can use hook_form_alter() in your custom module then

  1. Store the value of the form element(present in block) in global variable.
  2. Now use that global variable and set form element(present in content area, this form element is not visible to user).

Provide more information if you implemented other way.

Regards, Chintan.


There is a related issue here: https://drupal.stackexchange.com/questions/3827/multiple-copies-of-views-filter-form-exposed-filters which describes how to duplicate your filters. However it seems like an ugly hack.

A bit cleaner seems this solution mentioned in #6: http://drupal.org/node/641838#comment-3247748 Haven't tested it out, but it looks good.

It will still give you some overhead (duplicate views) but it might be the easiest way doing this using views.

On the other hand you might write a module and build your own custom filter block which hooks into your view. Here is a blog post about this: http://www.hashbangcode.com/blog/creating-custom-views-filters-exposed-form-element-drupal-6-561.html


If you use something like context you could get the exposed filters block to display twice in the same page. You could then use CSS to hide the fields you don't want to do display in each form.

The fundamental problem you're having is that to have the two forms in different places, they'll each have their own form element - when a submit is triggered, only the form fields within the same form element are sent. You need to move them into one form, or rely on JavaScript to gather the fields from both forms and construct the post.


You could create the block as an empty div and have javascript from the main page populate it with the secondary filter form and whatever else you need in there. Again, you could use javascript to copy the form values from the block form to hidden fields in the main form on submit. That gives you all the control you need from one place (the node output). Only caveat is that it relies a lot more on javascript to join it all together.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号