Hi!

I've been practising on Drupal for a few months, but there is something i still don't manage to do (i have almost no php knowledge, which makes things harder!).

Basically i want to build a block with a selection box in it, on a taxonomy vocabulary. Then i would like to be able to pass the selected values as argument in some page views on my site. The easiest way might be to use the "Action to take if argument is not present: ", select "php code", and write a few lines referring to the values selected in the selection block.

It seems simple, but having little php knowledge i don't find a way to sort this out. Also i looked similar posts on arguments, but never found any where someone had to generate the values for the arguments like this.. :(

Last comment: initially i was trying to do this with exposed filters which seems more straightforward. Yet i didn't find a way to use a single exposed filter for several page views. Moreover i also want to use exposed filter additionally to arguments in order to filter on other taxonomy terms/cck fields.

If you ave any idea about how to do this, i would be very happy to get some help!
Thanks a lot!

Matt

Comments

nevets’s picture

It is not clear what "Yet i didn't find a way to use a single exposed filter for several page views." means. Exposed filters are tied to a view which produces a result set depending on the filter and other settings in the view. More generally any form is tied to a single "page" (page here means path, the page could be paged).

So the question is given a user selection exactly what are you trying to display?

MattDrupalee’s picture

Here is how i tried this with exposed filters:
_ i created a view and associated page, with a filter on a town. I expose this filter. I display the associated block. When i select a taxonomy term in the exposed filter, the view elements are updated
_ if i create a 2nd page in the same view with a taxonomy term filter and expose it, it works fine also. I display the exposed filter block only if the URL of the page is called.
_ My problem: if i use block exposed filter 1 to filter page 1 on taxonomy term A, and then i go to url of page 2 to display page 2 elements, i have to use block exposed filter 2 and select again taxonomy term A. Yet i would like the taxonomy term A element to remain selected on that 2nd exposed filter block, as the user already selected it when browsing another page of the same view.

As i didn't manage to do this with exposed filters, i was wondering if it would be easier using arguments, creating a block with a taxonomy term selection list. Then if page 1 or page 2 of that view would be visited, they would be filtered by the taxonomy term selected via the "argument block". But i don't see neither how to create that argument filter block, and pass the selected taxonomy terms to the view pages (although i saw this could be done using the default option with php code..).

I hope this clarifies what i would like to do..and maybe someone would be able to help on one of these two options (exposed filters or arguments)!

Thanks a lot!

Matt

tommytom’s picture

any ideas? I have the same problem.

blup’s picture

Subscribing...

Currently my attempt is creating a link with the select boxes and some code, and then having the view in the destination page interpret the url through the arguments. I'm sure there is a cleaner solution to this, however.

MattDrupalee’s picture

Hello,

Did you progress on this?

I'll try to create a select block/form/box by taxonomy term as a first step..as i'm a php beginner any help is welcome!

Once i progress a bit i'll post it here!
Thanks,

Matt

abqaria’s picture

+1

MattDrupalee’s picture

HI,

I did good progresss on this, just missing one last part:
1/ i created a little module, which adds a block with a text form to input a location. User can enter the location he wants, which is then passed to a session
2/ the session can be read well as a default argument in views.
3/ I just need now to replace the text form in the block by a taxonomy filter for the desired vocabulary: any idea on how to do this? It seems this would work fine, but it's not in 6.x yet :( http://drupal.org/project/taxonomy_widget
Maybe there is a way to use the functions of http://drupal.org/project/hierarchical_select ?

Any help welcome on that last part!!

PS:
Here are the details of what i did so far:
1/


/**
 * Implementation of hook_block().
 *
 * Display a location switcher. 
 */
function location_switcher_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $block[0]['info'] = t('Location switcher');
    // Not worth caching.
    $block[0]['cache'] = BLOCK_NO_CACHE;
    return $block;
  }

  elseif ($op == 'view') {
      $block['subject'] = t('Locations');
      $block['content'] = drupal_get_form('location_form');
      return $block;
  }
}

/**
 * Implementation of the location_form
 *
 */

function location_form ($form_state) {
  $form = array();
  
  //make a texfield
  $form['location'] = array (
    '#type' => 'textfield',
    '#title' => t('Please enter desired location(s)'),
    '#required' => TRUE,
  );
  
  //a "submit" button
  $form['submit'] = array (
    '#type' => 'submit',
    '#value' => t('APPLY'),  
  );
  
  // If you forget this line your form will never exist
  return $form;
}

/**
 * form_submit function, once the form passes the validation (above) this runs
 * 
 * Docs @
 * http://api.drupal.org/api/file/developer/topics/forms_api.html/6
 * (under Submitting Forms)
 * 
 * drupal_set_message() Docs @
 * http://api.drupal.org/api/function/drupal_set_message/6
 *
 */
function location_form_submit ($form_id, &$form_state) {
  $location = $form_state['values']['location'];
  $output = t('The following location(s) have been selected ');
  $output .= check_plain($location);

  $_SESSION['Location'] = $form_state['values']['location'];
  drupal_set_message($output);
}

2/ In the view Argument, put default argument with PHP, and insert the code:
return $_SESSION['Location'];

blup’s picture

I don't have the time to test it right now, but here's what I've found. The function taxonomy_get_tree (http://api.drupal.org/api/function/taxonomy_get_tree), along with a couple other lines might do the trick (below). Let me know if you solve it, because i'm also interested in this feature.

$vid = 1; // Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$terms = taxonomy_get_tree($vid);
foreach ( $terms as $term ) {
...
MattDrupalee’s picture

Hi!

I'm almost there now. I used the function taxonomy_form() in order to replace the text filter by a filter with the taxonomy values.
My form seems to work now, yet i don't manage to retrieve/display its values.

I suppose that in the last lines below the "$location = $form_state['values']['location'];" is not right.
The "$output .= $location;" just displays "array" instead of the values of the array itself :(!

Any idea on how to solve this??

Thanks,

Matt


/**
 * Implementation of hook_block().
 *
 * Display a location switcher. 
 */
function location_switcher_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $block[0]['info'] = t('Location switcher');
    // Not worth caching.
    $block[0]['cache'] = BLOCK_NO_CACHE;
    return $block;
  }
  elseif ($op == 'view') {
      $block['subject'] = t('Locations');
      $block['content'] = drupal_get_form('taxonomy_location_form');
      return $block;
  }
}

/**
 * Implementation of the taxonomy_location_form
 *
 */

function taxonomy_location_form($form_state){

  $form = array();
  $form['location'] = taxonomy_form(3,1,NULL,taxonomy);
  
  //a "submit" button
  $form['submit'] = array (
    '#type' => 'submit',
    '#value' => t('APPLY'),  
  );
  
  // If you forget this line your form will never exist
  return $form;

}

 
/**
 * form_submit function, once the form passes the validation (above) this runs
 * 
 * Docs @
 * http://api.drupal.org/api/file/developer/topics/forms_api.html/6
 * (under Submitting Forms)
 * 
 * drupal_set_message() Docs @
 * http://api.drupal.org/api/function/drupal_set_message/6
 *
 */
function taxonomy_location_form_submit ($form_id, &$form_state) {
  $location = $form_state['values']['location'];
  $output = t('The following location(s) have been selected ');
  $output .= $location;

  $_SESSION['Location'] = $form_state['values']['location'];
  drupal_set_message($output);
}

Wim Leers’s picture

Here you go, exactly what you need: http://drupal.org/node/534986.

:)