4

I have a soccer team Drupal 7 site where I keep track of the players using a user role. I have a view of the players which grids their profile pics and I'm very happy with it. It's URL is /players

Now I want to divide the players into separate groups. My first stab at this was to add a user_program field onto the User content type. I added this field of type List(text), and gave it keys/values like: youth_competitive_program|Youth Competitive Program primary_competitive_programe|Primary Competitive Program

I then added a Contextual Filter so that I have /players/youth_competitive_program and such. Works very well. Except: when I tell those views to override the title using %1 I get the key instead of the value. What I really want is my human readable value up there, not the key.

9
+50

edit your Contextual Filter check Specify validation criteria in Validator check PHP code and enter following:

if ($argument == 'youth_competitive_program') {
  $handler->options['title'] = 'Youth Competitive Program';
  return true;
} else if ($argument == 'primary_competitive_programe') {
  $handler->options['title'] = 'Primary Competitive Program';
  return true;
}
return false;

there is another similar way:

if (!empty($argument)) {
  $handler->options['title'] = t($argument);
  return true;
}

return false;

and translate them, so if you add more option to your user_program fields, this code will work better.

but there is another option yet:

$field = field_info_field('field_user_program');
if (array_key_exists($argument, $field['settings']['allowed_values'])) {
  $handler->options['title'] = $field['settings']['allowed_values'][$argument];
  return true;
}
return false;

I will select the last way

| improve this answer | |
  • 3
    This is actually pretty good, don`t understand the downvotes. Downvoting is constructive when supported with a comment on why the post is considered less viable. Unexplained downvotes are pretty much worthless. – stefgosselin Apr 12 '12 at 19:25
  • 1
    Well notwithstanding the downvotes this is the best answer for me for sure! Thank you very much and enjoy the bounty! – Michael Daum Apr 13 '12 at 0:55
  • Don't forget to check Override title, if not your title won't be updated. If you like to mess around you can add $handler->options['title_enable'] = 1; instead. – tostinni May 5 '13 at 20:40
3

Why don't you use Human readable values for both the key and value of the list? Views should be able to accept an argument with hyphens and substitute spaces in the title. You may have to modify the template to get title cases in your title using something like php's ucwords().

| improve this answer | |
  • This would be OK if you haven't already created the field but for an existing field it is not trivial to change the keys of the options. – rooby Jun 1 '16 at 6:35

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.