2

I'm theming a Drupal 8 Views page. I created the twig template file and can output the 2 exposed filters with the code below. The problem is that nothing happens when I click Apply on the exposed filters. It's probably missing some attribute, but I can't figure out how to do it. Has anyone done this?

{% if exposed %}
  <div class="view-filters form-group">
    {{ exposed.field_category_target_id }}
    {{ exposed.field_address_locality }}
    {{ exposed.actions }}
  </div>
{% endif %}

Here are the printout of the exposed variable exposed variable

6

This is not possible in a view template. The rendered form elements are useless, because they are not in the html form (between <form>/</form>).

You need a custom form template for this, so that you are inside of the form. For the exposed form there is already a customizable form template in core:

/core/themes/stable/templates/views/views-exposed-form.html.twig

{#
/**
 * @file
 * Theme override of a views exposed form.
 *
 * Available variables:
 * - form: A render element representing the form.
 *
 * @see template_preprocess_views_exposed_form()
 */
#}
{% if q is not empty %}
  {#
    This ensures that, if clean URLs are off, the 'q' is added first,
    as a hidden form element, so that it shows up first in the POST URL.
  #}
{{ q }}
{% endif %}
{{ form }}

You can copy this twig to the /template folder of your theme and start theming the form elements:

  {{ form.field_category_target_id }}
  {{ form.field_address_locality }}
  {{ form|without('field_category_target_id', 'field_address_locality') }}

See How do I assign a template to the user edit form? for more info how to add and use these form templates in other forms.

0

In addition, if you want to theme form differently for different views you can do so like this:

View Name Machine Name: my_view Name of theme file to only affect that form: views-exposed-form--my-view.html.twig

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.