0

I built a custom form using the Form Render API on a page that is meant to lead to a view. You can select a region and it will bring you to the page containing the view with the fields and results already generated from the GET variables. The form appears like this

<form method="get" action="/products" accept-charset="UTF-8" data-drupal-form-fields="range_selector">
<div class="js-form-item form-item js-form-type-select form-item- js-form-item- form-no-label">
<select id="range_selector" name="field_product_range_target_id" class="form-select"><option value="">Focus Area</option><option value="782">Poultry</option><option value="920">Protection</option><option value="787">Rabbits</option><option value="917">Soil</option></select>
        </div>
<input class="js-form-submit form-submit" type="submit" name="op" value="View Products">
</form>

Then on the /products page there is a view with an exposed taxonomy filter that accepts the GET var field_product_range_target_id as the default value. There are other fields being used in the form, but I removed them for the sake of brevity. I'm letting you know in case having multiple fields matters.

This works at first but then there's an issue, whenever the user uses the exposed filters when they've visited the page the GET vars overwrite whatever was being put in the fields, since it is using AJAX and not another GET request. I understand that it probably would work if I didn't use AJAX but it'd be nice to use AJAX and I'm running into a bug where everything is blank and the filters aren't shown in AJAX is turned off. I figured I could solve my problem by using a POST request instead of a GET one but that didn't work either.

How can I use a custom form to lead to a view with default values using the GET method without overwriting anything from an AJAX request from the exposed filters?

Is there a hook I can use to give the AJAX values priority over GET vars?

UPDATE: For more info here's the format of how the form is being created

                'form' => [
                    '#type' => 'form',
                    '#attributes' => [
                        'class' => [''],
                        'method' => 'get',
                        'action' => $this->configuration['url'],
                    ],
                    'range' => [
                        '#type' => 'select',
                        '#options' => $range_options,
                        '#attributes' => [
                            'id' => 'range_selector',
                            'name' => 'field_product_range_target_id',
                        ],
                    ],
                    'view_all' => [
                        '#type' => 'button',
                        '#attributes' => [
                            'class' => ['more-info'],
                        ],
                        '#value' => 'View Products',
                    ],
                ],
  • Did you build this with the Form API? – Kevin Nov 27 '17 at 16:30
  • I'm using the form render element as seen here api.drupal.org/api/drupal/elements – Matt Nov 27 '17 at 16:33
  • I built a custom form on a page ? – Kevin Nov 27 '17 at 16:41
  • I guess I meant I am rendering a custom one in a block plugin that I'm setting the attributes manually – Matt Nov 27 '17 at 16:52
  • I updated my question to show how my form is being rendered – Matt Nov 27 '17 at 16:55

Your Answer

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

Browse other questions tagged or ask your own question.