Views 2 - Custom views filter handler - Labels not showing on options.

Events happening in the community are now at Drupal community events on www.drupal.org.
tararowell's picture

Hello group!

I have created a new views filter handler and it is working, but the labels on the values are missing.
I am sure that I have simply not called them, but i'm not sure how to do it. My handler_filter.inc file is preparing the values with this:

function get_value_options() {
    if (isset($this->value_options)) {
      return;
    }

    $this->value_options = array();
    $choices = array(
       "0" => array(
             "#title" => "title 1",
         ),
        "1" => array(
           "#title" => "title 2",
         ),
        "2" => array(
              "#title" => "title 3",
         ),  
        "3" => array(
            "#title" =>"title 4",
         ),
        "4" => array(
            "#title" => "title 5",
         ),  
        "5" => array(
            "#title" => "title 6",
         ),           
 
   );

    foreach ($choices as $stat => $info) {
      $options[$stat] = t($info->item);
    }
   $this->value_options = $options;

  }

So the values populate the exposed filter properly, and the query is correct, but the dropdown box in the filter has 6 blank rows.

Is there another function I have to use to send values to the form?
Thanks!

Comments

Basic PHP code error

andy_read's picture

I can't remember the API details, but just looking at your code, it seems obvious that the PHP won't put anything into values of the options array.

In the foreach loop, each $info is an array containing just the '#title' elements.
But you try to access $info->item, which is wrong on two counts:
a) $info is an array, not an object
b) $info does not have an 'item' field.

Try changing that line to:
$options[$stat] = t($info['#title']);

As I said: this is just fixing the basic PHP - no guarantees that you've got the API right.

Well here's the baffling part

tararowell's picture

Well here's the baffling part - I am SURE the PHP is off as you say, but it is returning the correct value to the query...or is that a coincidence because I want the value to be 0 thru 5 - but labeled otherwise? I'll give the PHP fixes a try and see how it responds.

Thanks!

Correct keys but no labels

andy_read's picture

Hi Tara. Yes, you are setting the array keys "0" - "5" correctly from $stat, but the values, which are used for labels, will be empty. I'm not really sure why you've created a complex array of arrays with #title elements, because this just makes it harder to read and is unnecessarily complex. The statement "$this->value_options = array();" is also completely redundant (although "$options = array();" before the foreach loop would be good practise). I'm guessing you've adapted a code example somewhere, but if you're just creating a static set of options the whole method can be simplified to:

function get_value_options() {
  if (isset($this->value_options)) {
    return;
  }

  $options = array(
    "0" => t("title 1"),
    "1" => t("title 2"),
    "2" => t("title 3"),
    "3" => t("title 4"),
    "4" => t("title 5"),
    "5" => t("title 6"),
  );

  $this->value_options = $options;
}

And I'm not sure explicitly setting the keys to "0" - "5" makes any difference either as they would default to 0-5 and probably be handled the same:
$options = array(t("title 1"), t("title 2"), ...);

Andy

You're awesome - that totally

tararowell's picture

You're awesome - that totally works! I knew my PHP was wrong.
I decided to keep the keys, even though they are the same by default, because of the relevance to the actual numbers in my application. If I have cause to reassign those values in the future, it will be obvious where to adjust them in this filter, as well.

Thank you so much - I really appreciate your expertise!

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: