Hi All,

Has anyone had any success exposing a filter for a computed field, and turning this in to a selection box? For example - computed field outputs Monday, Tuesday, Wednesday, Thursday or Friday as an output. I would then like a user to be able to filter a view based on these values, but from a select box. Currently, it seems like support is only available for a text box entry.

Thanks

CommentFileSizeAuthor
#8 my_module.zip908 byteswerushka
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

flying_q’s picture

Title: Exposed views filter + Allowed values for Computer Field - Possible? » Exposed views filter + Allowed values for Computed Field - Possible?
ds1964’s picture

I've been trying to figure out the same thing for awhile and recently came up with something that seems to work.

1) I implemented the "mymodule_tools_form_views_exposed_form_alter" hook in a mini-module that I've created for various helper functions.

2) I placed the following code w/in the hook function:


  if ($form['myfieldname']) {
    $form['myfieldname']['#type'] = "select";
    $form['myfieldname']['#options'] = array(All=>"<All>",Yes=>"Yes",No=>"No");
    $form['myfieldname']['#default_value'] = No;
    $form['myfieldname']['#size'] = null;
  }

3) In the View's administration page, I set the "Value" field for the exposed computed field filter to "All".

The last step was necessary in order to clear an “An illegal choice has been detected. Please contact the site administrator.” error message that popped up on the View's display page otherwise. I should note that neither the default value property nor the "All" selection in the View definition affected the status of the HTML select field widget -- this was always set to the first value in the select list (in this case "All") on page load. However, after that everything seems to work normally.

This is actually the first time that I've implemented a Drupal hook, so if I'm doing anything wrong (or if there is a better way to approach the problem) I'd appreciate hearing about it.

I hope this helps!

flying_q’s picture

ESC - thanks so much for the response....instead of using a computed field I was actually fortunate enough to use the tokens available from the Date module. I stripped the 'day' field from the date a user selected (via the really slick JS Calendar popup). At time of Node-save I just created a simple rule set that said save Day from Date into Field XYZ. I am then able to filter based on those values.

Regards.

ds1964’s picture

flying_q ,

Thanks for the info. I had also been thinking about something along the same lines as your approach, but still have a couple questions.

1) What type of field is "Field XYZ" in your example and does it have an allowed values list? I've only been able to get select lists for allowed values fields (as opposed to a select list of actual current field values).

2) Sounds like you are using the Rules module to implement the save-time action. Is that right?

Appreciate any more info on these questions.

Best

Mac Clemmens’s picture

subscribing

1) What type of field is "Field XYZ" in your example and does it have an allowed values list? I've only been able to get select lists for allowed values fields (as opposed to a select list of actual current field values).

If there is a way to do this, that would be very helpful!

werushka’s picture

I am trying to do the same thing...

I am using template.php way. is it possible if you can provide the complete function that you would put to template.php if you were doing my way...I would appreciate some feedback

TomMynd’s picture

Hi,

I currently banging my head against this issue. I have a computed field called "age" that is filled up by a computed value from a birthday field. I want to have an exposed filter, where the user can select an age to filter only nodes which have the appropiate age.

Currently I'm stucked.

werushka’s picture

FileSize
908 bytes

i am attaching the module i created dont forget to change the field_name in the module then enable the module if you are on drupal6 create exposed filter with the value Is Equal to "All"

TomMynd’s picture

Hi,

thank you for this little snippet. I installed the module and after that the specified fields from "options" are shown and I can select those without problems. But when I hit the page, there is an error message "An illegal choice has been detected.".

Also when I select the "All"-Value inside the selectlist, no entry is shown.
I peeked inside the SQL-Queries for that page with the devel-module and the SQL for retrieving the data is:

SELECT node.nid AS nid FROM node node 
LEFT JOIN content_type_player node_data_field_age 
ON node.vid = node_data_field_age.vid 
WHERE (node.type in ('player')) 
AND (node.status <> 0) 
AND (node_data_field_age.field_age_value = 0) LIMIT 0, 10

I have set the default value for the filter of the Age-Field to "All".

And yes... it's Drupal6 with the newest available CCK and Views.

Best regards, Tom

TomMynd’s picture

Shame on me... I looked deeper and came to a simple solution. The computed field is an integer and not varchar (or somethings other text based), so I switched the default value for that field to a nuimber and not to "All" and it went smooth.

Best regards, Tom

werushka’s picture

would you happen to have idea about how we would find age range?

TomMynd’s picture

I found this over at gdo: http://groups.drupal.org/node/25941

I currently filter the age with "is less than" and not in a range with "in between". With that you will have 2 fields for the exposed filter. These both fields can be combined and after submitting they will be splitted for use inside the view.

kris_mcl’s picture

Thanks everyone, this is great info & has gotten me close to what I want to do. Using werushka's module technique from #8, I've got my exposed filter showing up as a select.
The only thing that's left for me is to populate that select with possible values on the fly instead of hard-coding a list of options in my module_form_alter function. I have a CCK computed field that I'd like to use as the possibilities list. Any ideas on how to do this? I've examined the entire $form variable, but haven't been able to get my computed fields included in there so that I can loop through them & add them to the select list.
I've got the node reference defined as a relationship, and added the computed field to the Fields list in my view, but that hasn't done the trick.
Thanks in advance for any advice!

Update: Got it!
I'm not sure if this is the best way to do it, but it did the trick for me. I just manually queried the DB for the fields I needed & built an array with the results, then used that array for my $form['field-name']['#options'] value. Here's my new mymodule_form_alter function:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-entries-page-1') {
		
    // Build an array of options with database content
    $options = array(All=>'<All>');
    $result = db_query("SELECT field_concert_label_value as label FROM {content_type_concert} ORDER BY field_concert_date_value ASC");
    while ($row = db_fetch_object($result)) {
      $options[$row->label] = $row->label;
    }
		
    $form['label-input-exposed']['#type'] = 'select';
    $form['label-input-exposed']['#options'] = $options;
    $form['label-input-exposed']['#default_value'] = All;
    $form['label-input-exposed']['#size'] = null;
  }
}

This works well. Thanks everyone!

OFF’s picture

My solution:

<?php

function mymodule_form_views_exposed_form_alter(&$form, $form_state) {
  if ($form['project_client']) {
    $form['project_client']['#type'] = 'select';
	$form['project_client']['#attributes'] = array (
      'size' => 1,
    );
    $form['project_client']['#options'] = array(
      '' => t('<Any>'),
      '0' => 'Собственный',
      '1' => 'Клиент',
    );
    $form['#submit'][] = 'mymodule_form_views_exposed_form_alter_submit';
  }
}
?>
amaisano’s picture

Works great for me as well! It would be even better though if this could be tweaked to allow multiple values, and use checkboxes instead. Is that possible?

I've tried myself, but my syntax and form settings are probably getting me in trouble. I've made sure the Views filter for this computed field is set to "Contains any word."

function my_module_form_views_exposed_form_alter(&$form, $form_state) {
  if ($form['field_game_dow_value']) {
	$form['field_game_dow_value']['#type'] = 'checkboxes';
	$form['field_game_dow_value']['#attributes'] = array (
      'size' => 1,
    );	
    $form['field_game_dow_value']['#default_value'] = variable_get('field_game_dow_value', array('All'));
	$form['field_game_dow_value']['#options'] = array(
	
	'All' => t('Any'),
        'Sunday' => 'Su', 
        'Monday' => 'Mo', 
        'Tuesday' => 'Tu',
        'Wednesday' => 'We',
        'Thursday' => 'Th',    
        'Friday' => 'Fr',
        'Saturday' => 'Sa',

	);
	$form['#submit'][] = 'my_module_form_views_exposed_form_alter_submit';
  }
}

I need the URL to look like this:

...field_game_dow_value=Monday+Tuesday+Wednesday

But instead it looks like this:

...field_game_dow_value%5BMonday%5D=Monday&field_game_dow_value%5BTuesday%5D=Tuesday&field_game_dow_value%5BWednesday%5D=Wednesday

chinita7’s picture

I modified the code from #8 for myself but form doesn't show up as a select list.
I'm not a coder and don't really understand php so I just changed the fom id and the field name.
'field_nedan' is a computed field I want to display as select list in exposed filter and has value like '1000-2000' '1000-2000'
Am I still missing something?

<?php
function my_module_form_alter(&$form, &$form_state, $form_id) {
  if($form['#id'] == 'views-exposed-form-search-block-2'){
    $form['field_nedan']['#type'] = "select";
    $form['field_nedan']['#options'] = array(All=>"<All>",1000-2000=>"1000-2000",1000-2000=>"2000-3000");
    $form['field_nedan']['#default_value'] = No;
    $form['field_nedan']['#size'] = null;
  }
}





dqd’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

Due to the Drupal core life cycle policy and security support advisery, Drupal 6 is no longer supported. So issues for Drupal 6 cannot be longer maintained. The project maintainer has asked for closing all D6 issues to clean up the issue queue.

colan’s picture