0

I have a textfield for one of my content types and there are many unique results for it. I am trying to create a View of this content type of this textfield as an Exposed Filter with a list of all the unique results from this textfield. Views by default only gives you a textbox for you to search through these results, but if you don't know what to look for, it doesn't work too well.

  • How would I expose this textfield of all the unique results in a selectbox in a view?
1

I had the same issue sometime ago and finally the only way to display all the values from a field in a dropdown filter seems to be the Drupal taxonomy. The Grouped flters option is fine but you need to enter all the values manually in the table (Label/value) and I think this is not what you are looking for.

If you can clone your Text field in a Term reference field (Taxonomy vocabulary), you should be able to create a dropdown filter in Views, based on this new Taxonomy field, with autocomplete or dropdown option for filter display. Then, each time a new term is created in the vocabulary, it will be suggested in the dropdown menu.

It was my option but maybe someone can suggest a way to display all field values in a filter, even if it is not a vocabulary. I would be really interested in that solution, anyway.

Hope this helps.

2

You can use Grouped filters of exposed filters to set unique results for select box in a views. Please see the screenshot given below:

Grouped filters example to convert textfield to dropdown

  • The thing is that my field is a custom field, but it acts like a textfield storing the same type of data. I tried to do the same thing, but I dont see the same options: prntscr.com/bgb7wn – Patoshi パトシ Jun 14 '16 at 16:08
  • In the attached screenshot(prntscr.com/bgb7wn), I can see the option of grouped filters with ability to add options for the field. – Vishal Patil Jun 15 '16 at 6:02
0

In my case I had to filter my view with a bunch of offices, but these offices were in a custom made field formatted in json. I had to clean up the field by running it through views_php with preg_replace() to get it ready for processing.

Once my data is formatted correctly for js consumption. I concatenate all of these offices together into a gigantic array. From there I run through this array to detect duplicates and end up with a results array of unique results.

Then I add these unique results into an appended

enter image description here

//If needed run the field through a views_php field to prepare it for processing. Separate each entry with a comma so you can split it via js.

a = jQuery(".views-field-php em").text();
b = a.split(',');

var results = []; //Create an empty results array variable to be filled


jQuery.each(b, function(index, value) { //Run through "each" value and put it into results array

  if ((jQuery.inArray(value, results) == -1) && value != '') { //Does the value exist in the results array? If not do the following
    results.push(value); //Adds the value to the results array
  }

});

results = results.sort(); //Sort all the arrays alphabetically

//Add in the <select element into the page
jQuery("#edit-field-role-offices-wrapper").append('<select id="filter-off"><option>Filter by Office</option></select>');

//Add the <option elements according to EACH array value in results
results.forEach(function(z) {
  jQuery("#filter-off").append('<option value="' +z+ '">' +z+ '</option>');
});

//When selecting an item, copy that value into the textfield above and then hit the submit button
jQuery("#filter-off").live("change", function() {
  jQuery("#edit-field-role-offices").val(this.value);
  jQuery("#edit-submit-yearbook").click();
});

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.