I am looking for a way to hide labels for fields if the fields are empty (and ideally skip that line of the output all together).
I am trying to have a list of users of a certain role on a page, and I want them to be able to update their information in one place (their profile) and it to be updated accross the site.
The view as it is now is exported below. In this case, the field of Specific Role in the profile is rarely filled, so I would not want that to display unless there was content in that field. So, if no one on the page has the given role, then the field might as well be excluded in the view for that page load. I can do this using a simple if statement for each of the fields using php, but would rather find a way using the interface directly if possible.
$view = new view;
$view->name = 'eboard';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'users';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
'rid' => array(
'label' => 'Positions',
'type' => 'ul',
'separator' => ', ',
'empty' => '',
'exclude' => 0,
'id' => 'rid',
'table' => 'users_roles',
'field' => 'rid',
'relationship' => 'none',
),
'value_1' => array(
'label' => 'Specific role',
'exclude' => 0,
'id' => 'value_1',
'table' => 'profile_values_profile_position',
'field' => 'value',
'relationship' => 'none',
),
'picture' => array(
'label' => 'Photo',
'exclude' => 0,
'id' => 'picture',
'table' => 'users',
'field' => 'picture',
'relationship' => 'none',
),
'value' => array(
'id' => 'value',
'table' => 'profile_values_profile_name',
'field' => 'value',
),
'value_4' => array(
'label' => 'Phone Number',
'exclude' => 0,
'id' => 'value_4',
'table' => 'profile_values_profile_phone',
'field' => 'value',
'relationship' => 'none',
),
'value_3' => array(
'label' => 'Office Location',
'exclude' => 0,
'id' => 'value_3',
'table' => 'profile_values_profile_office',
'field' => 'value',
'relationship' => 'none',
),
'value_2' => array(
'label' => 'Office Hours',
'exclude' => 0,
'id' => 'value_2',
'table' => 'profile_values_profile_hours',
'field' => 'value',
'relationship' => 'none',
),
'name' => array(
'label' => 'Username',
'link_to_user' => 1,
'exclude' => 0,
'id' => 'name',
'table' => 'users',
'field' => 'name',
'relationship' => 'none',
),
));
$handler->override_option('arguments', array(
'rid' => array(
'default_action' => 'ignore',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'default_argument_type' => 'fixed',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'ignore',
'break_phrase' => 1,
'add_table' => 0,
'require_value' => 0,
'reduce_duplicates' => 1,
'id' => 'rid',
'table' => 'users_roles',
'field' => 'rid',
'relationship' => 'none',
'default_argument_user' => 0,
'default_argument_fixed' => '',
'default_argument_php' => '',
'validate_argument_node_type' => array(
'blog' => 0,
'book' => 0,
'charter' => 0,
'minutes' => 0,
'page' => 0,
'report' => 0,
'story' => 0,
),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(
'2' => 0,
'3' => 0,
'1' => 0,
),
'validate_argument_type' => 'tid',
'validate_argument_php' => '',
),
));
$handler->override_option('filters', array(
'rid' => array(
'operator' => 'or',
'value' => array(
'8' => '8',
'4' => '4',
'3' => '3',
'9' => '9',
'5' => '5',
'7' => '7',
'6' => '6',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'rid',
'table' => 'users_roles',
'field' => 'rid',
'relationship' => 'none',
'reduce_duplicates' => 0,
),
));
$handler->override_option('access', array(
'type' => 'none',
'role' => array(),
'perm' => '',
));
$handler->override_option('title', 'Executive Board View');
$handler->override_option('header', 'These are the Executive Board Members for the Student Association.');
$handler->override_option('header_format', '1');
$handler->override_option('use_pager', 'mini');
$handler->override_option('style_plugin', 'grid');
$handler->override_option('style_options', array(
'grouping' => '',
'columns' => '3',
'alignment' => 'horizontal',
));
$handler->override_option('row_options', array(
'inline' => array(
'rid' => 'rid',
'value_1' => 'value_1',
),
'separator' => '',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', 'E-Board Members');
Comments
I'm trying to figure out the
I'm trying to figure out the exact same thing.
If you don't mind sharing, how do you do this with PHP? What's the code and where do you put it?
Since the post, I took a
Since the post, I took a look at what is exported more closely and didn't see an easy way to do it. I was originally thinking that a simple if statement within the view after it is exported would do it, then if you import the modified piece of code it would work, but it clearly is not meant to do that. I've read some things indicating that you could theme a view independently of other views, but so far, I have not found any instructions on how to do that anywhere.
Views Template
I just realized I had the same problem in my D6 site and I figured out a solution using the Theme Information in my theme.
I copied the code for views-view-fields.tpl.php and added an if statement to check for the existence of $field->content, namely:
Seems to work fine. I'd like to hear of better/different options.
Where do you put that code?
I'm new at Drupal so this is probably a dumb question, but where do you put that code?
Happy to help!
No problem -- it's nice for me to once be on the other end and be able to provide a solution. :-)
Are you using Drupal 6? Go into your view and look for the Theme Information link. Click that and select Row style output. Copy that template into your theme directory. Then compare that template to my edits and you'll see where I added
if ($field->content):andendif;around the code where the label is printing. Basically I'm telling it to only print the label if there is content in the field.Let me know if that helps.
You may want to put the
You may want to put the if-statement outside the part where it prints the $field->inline-html, or you end up with unnecessary div/span tags.
The best way to explain this
The best way to explain this process is to start from the view page in drupal 6.
go to the "Theme Information" link in the first column. Once there click on the "Row Style Output"
i then created the file "views-view-field.tpl.php" in the themes base directory. and copied the above code into the correct place.
However this code was not working correctly because of the misplacement of the
</<?php print $field->inline_html;?>>this line of code is actually closed in php statement for the element type below.here is the code that worked for me while solving this problem... my labels do not print unless there is content in the field that it is labeling....
send me a message if you need help ... once again ... this code worked fine for me in my instance.
evolvedideas
www.evolvedideas.net
Providing solid evolved online solutions for a wide variety of ideas.
Thanks! I had the same problem.
I had the same problem, your code fixed it. May be it needs to be incorporated in the Views.
Just realised that doing this moves my top navigation bar and the search box to the bottom of the page.Sorry, my mistake. The 'endif' should have been outside the 'inline_html'.
Thanks!
Not working for me
BS"D
Thanks but this is not working for me.
I got a white screen as follows after copying the code from row style output and following your directions above:
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/siteprot/domains/woswn.com/themes/garland/views-view-fields.tpl.php on line 2
Parse error: syntax error, unexpected T_STRING in /home/siteprot/domains/woswn.com/themes/garland/views-view-fields.tpl.php on line 2
What went wrong? Thanks!
I'd love to see this feature
I'd love to see this feature in a release. might i suggest that you are sending this to the official VIEWS team, so they can review it?
thanks!
Thnx
Thanks evolvedideas, this seems to work. But why don't you exclude the complete processing if the field is empty, like this:
If the field is empty, there is no need to print it, is there?
This didn´t unfortunately
This didn´t unfortunately work for me. All it did, was that my taxonomy terms werent visible anymore...
Thank you! I'm surprised this
Thank you! I'm surprised this isn't just a Views 2 setting, but I'm grateful to have it working.
Cheers,
Andrea
thank you
thanks for posting this! was of great help to me :)
Thank you
Thank you very much kriskd. Been stuck triying to figure this out for days!
works perfectly..
Gonzalo
Yes, it works for me too!
Yes, it works for me too! thankz kriskd..
filtermusic.net
Two CCK's different fields.
the issue i have is similar but different. I have 2 content types. One has a cck image field and one doesn't. So when I build the view I select both content types. What I then want to do is print a block with the image and title of the nodes. BUT if the node type is the one that doesn't have a cck image I want to print a default image.
The issue I am having is that even if the field is not printing it is still returning an empty value. I have tried all these
if (!$field->content) { print a default image }
or
if ($fields->content==NULL { print default image }
But nothing works. For some reason for the content type that has no Image field it still prints something although there is nothing in the html code and nothing for the view to print. So the fields loop and think there is something there when there isn't.
Any ideas?
can't figure it out...
Hi,
I have the same problem and can't seem to figure it out...
First of all, I am using the latest version of Drupal and Views. I have a view of type User displaying an unformatted list. I want to hide the labels for the empty fields.
I followed the instructions in the post entitled 'The best way to explain this'. I clicked on Row style output and copied the code into a file that I named views.view.field.tpl.php I made the modifications and then uploaded that and cleared my cache.
however I get the error message:
warning: Invalid argument supplied for foreach() in /home/perm2327/public_html/LLF-test/sites/all/themes/nitobe/views-view-field.tpl.php on line 22.
I tried a lot of things and don't want to get into details in order not to confuse you. I have a question: besides the views-view-field.tpl.php is there another .tpl.php file that I need to copy from views into my theme folder in order for the override to work?
Can anyone help? I can give more details if you need (I am famous for not giving enough details!)
Thanks!
try this
BastouBach, I don't know if this might solve your issue but here is what you should do,
When you create a new views display you give a name to it. When you whant to modify the view display output you have to create individual files for that view depending of what you want to control (rows, table, fields, etc). In this particular case, we want to modify the "field" display so:
Copy the file views-view-fields.tpl.php (not the file views-view-field.tpl.php) from /sites/all/modules/views/theme and paste it in /sites/all/themes/YOURTHEME-FOLDER/views (you have to create the views folder) and rename that file to views-view-field--YOUR-VIEW-NAME.tpl.php . Notice that if the user view that you crated is called for example "user_ranking" you have to rename the file to views-view-field--user-ranking.tpl.php . This new .php file is the one you have to edit with the code provided and explained by kriskd.
Then go to edit you view and click on "Theme: Information" and then press "Rescan Template Files" in order for the views module to use the new template file you created.
Hope this will help you.
Gonzalo
Been having the same problem
Been having the same problem and I did everything it suggest here, but still get the: warning: Invalid argument supplied for foreach() error...
Added to Views 6.x-2.x-dev
This feature is apparently added to Views in the latest dev version. See #480162: Hide labels for empty fields.
One step that wasn't
One step that wasn't mentioned here that needs to be done is clearing the site's cache by doing the following steps:
go to Administer -> Site Configuration -> Performance
click on "clear cache data"
then go to your view and verify.
Thanks,
To do this with a table
If you are wanting to output a table, the process is slightly different. (my file was views-view-table.tpl.php)
The code I used was this:
Table view update
The modified table view above will only work if the empty columns are the same in each row. The following modification will work even if you have several columns which may or may not have data and could be different for each row:
Nice work, here's a slightly
Nice work, here's a slightly modified (cleaner loops?) version, with php short tags replaced by long tags.
Use preprocessing function
Thank you all for examples. I modified them so column checking code is in preprocessing function.
Put following in template.php
You can now use $column_has_content variable in views-view-table--VIEW-NAME.tpl.php
Don't forget to replace VIEW-NAME with name of your view and THEME-NAME with name of your theme.
Switching x and y on the table.
I've combined the first little snippet with code from http://drupal.org/node/174578 to create a table that switches y and x and also hides rows with empty labels. Find it here: http://drupal.org/node/174578#comment-3407100
A Project Ricochet, Drupal Developers Team Member
I can't thank you enough for
jsims281, I can't thank you enough for your example. It has greatly helped to solve my endeavor.
Hard to find
THANK YOU! This solution for Tables was extremely difficult to find but should definitely be in the options when editing a table view.
Maybe a simple checkbox to "Hide Label" in field settings?
Hide an empty field in a views template
I'm adding this here in case anyone hits the same issue. I needed to NOT print a field in a custom template if it was empty. However, checking the "hide if empty" option under the field settings in the view wasn't enough. The system kept spitting out an UNDEFINED_INDEX error.
This snippet of code embedded in my view template did the trick:
Maybe this will help someone. I couldn't find an apparent solution anywhere online.