Drupal Views UI Filter Fields by Content Type

Submitted by tomo on April 21, 2011 - 4:18am

Late night hack:

You have a lot of fields in a lot of content types. You're creating a view with new display fields but it's a pain to find just the content type fields you want. Wouldn't it be cool if you could just select a content type from a pulldown and see content fields filtered to just the ones in that content type?

Add this bookmarklet to your bookmarks bar:
Views UI Filter

Now when you're editing a view and you click the plus button to add a field, you can click that bookmarklet in your bookmarks bar which will insert a select menu next to the "Groups" select pulldown with a list of your content types. When you select one, you'll only see fields you want!

Make sure to first filter groups by "Content". And click the bookmarklet only after you click the plus to add a field and after that pane has been loaded. This is just a quick hack for now but may make its way into a Views UI extending module later.

A slightly more readable version of the code:

javascript:(function(){

var exp = /^.*Appears in: (([^,]*[, ]*)*)$/;
var ctypes = $('.views-radio-box .form-item').map(function(){
 var desc = $(this).children('.description').html();
 if (desc.match(exp)) {
  return desc.match(exp)[1].split(', ');
 }
});
if (ctypes.sort != undefined)
 ctypes = ctypes.sort();
var uniques = {};
for (var i = 0; i < ctypes.length; i++)
 uniques[ctypes[i]] = 1;
var select_html = '<select id="show-ctype"><option value="Show All">-Show All-</option>';
for (var ctype in uniques)
 select_html += '<option value="' + ctype + '">' + ctype + '</option>';
select_html += '</select>';
$('#edit-group').after(select_html);
$('#show-ctype').change(function() {
 $('.views-radio-box .form-item').show();
 if ($('#show-ctype option:selected').val() == 'Show All')
  return;
 $('.views-radio-box .form-item').map(function(){
  var desc = $(this).children('.description').html().trim();
  if (desc.match(exp)) {
   var arr = $.map(desc.match(exp)[1].split(', '), function(x){return x.trim();});
   if (-1 == $.inArray($('#show-ctype option:selected').val().trim(),arr))
    $(this).hide();
  }

 });
});

)();

Update: Fixed to work w/ jQuery 1.2, default in Drupal 6

Read the rest of this article...
Anonymous (not verified)

Very innovative and excellent work. Could you also work to remove some fields which are not part of the selected files?

But, very useful work. Thanks.

tomo

Can you explain what fields you mean, and what you mean by selected files?

© 2010-2014 Saigonist.