0

On my user profile, I am displaying a field that looks like this:

Choose your favorite fruits:

  • Apples
  • Strawberries
  • Bananas
  • Grapes
  • Click to enter your own

Using the Conditional Fields module, clicking "Click to enter your own" displays a text field where the user can input whatever they like. This all works great.

However, if a user chooses "Apples", "Strawberries", and "Click to enter your own", when I display the user profile in views by adding the "favorite fruits" field, it looks like this:

Fruits I like: Apples, Strawberries, Click to enter your own

Needless to say this is very ugly and I want to hide "Click to enter your own"-- and that's all I want to do, hide that text. Is there an easy way to do that?

EDIT: I'm attempting to find the values for my site for ($view->field['my-field-name']['my-field-value']. Using print_r, it appears that the value I want to remove is: [field_purpose]['und'][0]['o']; "o" is the value of the checkbox for other in the field_purpose field. However, when I use this code: ($view->field['field_purpose']['und'][0]['o'], I get this error: Fatal error: Cannot use object of type views_handler_field_field as array mymodule on line 325

Perhaps I have the value wrong? When I use dpm instead of print_r (dpm($view->field['field_purpose']); I don't see the values of the checkboxes stored anywhere...

The dpm output is as follows:

enter image description here

4
  • Please, provide output of dsm($view->field['field_purpose']); Commented Jul 20, 2012 at 17:33
  • Added a screenshot. Commented Jul 21, 2012 at 5:08
  • Field values are stored in 'options' array. So you must try unset($view->field['field_purpose']['options]['OPTION_TO_DELETE']) Commented Jul 21, 2012 at 8:48
  • For the view handler field, the options array contains the Style options for the view, such as element_type, element_wrapper_class, etc. It does not contain the value of the field. Commented Jul 21, 2012 at 9:54

6 Answers 6

3

You can remove it with jQuery. Find that element by the value and just do .remove().

Even though this is not the perfect solution, it needs least amount of time and you can use it in the short term.

[EDIT]

Check Views selective filters. They can limit the options only to the ones available in the results; all others will be hidden.

1
  • Views selective filters module will not help because "Click to enter your own" option presents in result. Commented Jul 20, 2012 at 16:01
1
+50

To delete an element from an array you could simply execute something like this (copying from the previous answer):

unset($view->field['value'];

If you are not able to debug the module, use print_r. To the previous answer should be useful also to check the display:

function mymodulename_views_pre_render(&$view) {
  if ($view->name == 'my-view-id-name' && $view->display == 'my-display') {
     unset($view->field['my-field-name']['my-field-value']);
  }
}

Check obviously every name and variable used in the function :)

3
  • 1
    You could also create a template for the field and check values
    – arrubiu
    Commented Jul 20, 2012 at 7:08
  • I'm having trouble finding the value for 'my-field-value'. Please see my edit. Commented Jul 20, 2012 at 16:20
  • Awarding the bounty to this answer as it is the closest to what I wanted, even though I was not able to get it working. Commented Jul 27, 2012 at 1:26
1

Sorry, I think that there is no easy way. My solution would be to do it programatically. Create your own module and add a hook function for the prerender view function. This is how I would do it in drupal 7.

Something like this:

function mymodulename_views_pre_render(&$view) {
  if ($view->name == 'my-view-id-name'){
     unset($view->field['my-field-name']['my-field-value']); //not sure about the correct construct of this fields array, 
       //but print_r it and you'll find out, maybe an if and setting it to '' would be better, but pop should work too
  }
}
2
  • Thanks for this. However, after plugging in the values for my site, I received a Fatal error: Call to undefined function pop(). I tried using array_pop() instead, but this results in a different error. Commented Jul 17, 2012 at 16:42
  • sorry, the function should have been array_pop not only pop
    – reptilex
    Commented Aug 21, 2012 at 12:23
0

The best approach is to use Select (or other) field in user profile instead of two fields (select and textfield to enter custom value)

In this case all values that are entered by user will be available in view filter and "Click to enter your own" option will not be shown in exposed filters.

3
  • I can't use Select (or other) because it doesn't support display via Exposed Filters in Views and the maintainer has no plans to implement this feature. Commented Jul 20, 2012 at 8:26
  • drupal.org/node/1450870 Commented Jul 20, 2012 at 8:27
  • Yes, you are right. This module cannot be shown in exposed filter as drop-down. Commented Jul 20, 2012 at 15:57
0

In view edit your filter criteria settings for field "Fruits I like".

In popup window you will see list of all available options for this field- check all options except "Click to enter your own" and under this list check "Limit list to selected items".

Thats all.

2
  • Sorry, I'm not looking for a solution for exposed filters; I need something that works for fields in views. When you select a field, you don't get the option to exclude certain values from the results. Commented Jul 20, 2012 at 17:06
  • Ok, i see. Could you provide screenshot of your views settings page? Commented Jul 20, 2012 at 17:31
0

Ultimately, I solved this as described here: https://drupal.stackexchange.com/a/38624/5147

It turns out that using a views template file is a much easier, more maintainable, less complicated solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.