1

I'm trying to make a view that builds a drop-down select out of a taxonomy. I need to theme the fields as so:

<option value="{{ tid }}">{{ name }}</option>

where {{ tid }} and {{ name }} are my view's taxonomy fields.

I'm using a custom views-view-fields.html.twig template to provide the rest of the code for the select list.

How can I add the view's fields as theme suggestions? Something like this, but actually, you know, working.

function THEME_theme_suggestions_views_view_field_alter(array &$suggestions, array $variables) {

  $field_name = \Drupal::request()->attributes->get('field_name'); {# I have no idea what to use here :-) #}

  if (!empty($field_name)) {
    $suggestions[] = 'views_view_field__' . $field_name;
  }
}

I have no idea what to use to retrieve the field names to add them to the theme suggestions. I've stared at Kint output until my eyes were bleeding. Any help at this point would be greatly appreciated.

  • You can't populate a select list in the template, this will not work. The form api will not accept any item of that list. You have to do this when building the form and there I would suggest to use an entity query on the taxonomy term, not a view. – 4k4 Feb 27 '16 at 10:22
  • For an Isotope like js filter, not for Form API. – UpTil4Music Feb 28 '16 at 2:15
  • 1
    Even then I would not use views, you can build the select list with an entity query with a few lines of code and you have all in one place. – 4k4 Feb 28 '16 at 10:02
  • Let's pretend I don't know how to do that. :-) Is there a step-by-step somewhere that would walk me through how to? – UpTil4Music Mar 1 '16 at 11:15
  • If you search in google you find something like this: drupal8.ovh/en/tutoriels/17/… – 4k4 Mar 1 '16 at 13:30

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.