0

Let's say there are 2 vocabularies:

  • Sizes (S, M, L, XL)
  • Colors (Red, Green, Yellow)

pathauto sizes: /size/s, /size/m, ... pathauto colors: /color/red, /color/green, ...

There is a catalog - taxonomy/term/% view with the following filters:

  • Size
  • Color

If we use a filter, then the url will be like this - ?field_size_tid=2&field_color_tid=3 It's normal, when several fields are selected in the filter

But when one field is selected in the filter, the url will be like - ?field_color_tid=2 But in fact the ?field_color_tid=2 = /color/green

How to make an redirect to /color/green instead of ?field_color_tid=2 in this case after exposed form submit?

  • This part But in fact the ?field_color_tid=2 = /color/green is confusing. Do you want to say it looks like that in URL? – AltaGrade Mar 22 '18 at 15:36
  • I meen, that views results for url ?field_color_tid=2 are the same with results for taxonomy term page /color/green (taxonomy/term/2). ?field_color_tid=2 = taxonomy/term/2 (alias for this path: /color/green) (in this case, when in filter selected only one exposed filter, exposed filter results = contextual filter results) – Viktor Mar 22 '18 at 19:28
  • If you want views page to immediately redirect to taxonomy page after filter is applied, then why use views at all? Just use taxonomy and some kind of taxonomy filter. It really doesn't make sense to render views only for its filter and then never to use the view itself. – AltaGrade Mar 22 '18 at 19:34
  • I have a big catalog, with many categories, and also with many exposed filter paramters, and the taxonomy/term/% view is exactly what I need. But this feature with this kind redirect is our SEO managers requirement. If in exposed filter selected only one filter, then after form submit redirect to equivalent taxonomy term page if this exists. – Viktor Mar 22 '18 at 20:00
  • Hey Viktor, your profile shows you had been online much recently than the question was asked. Please do not leave your questions with no answer accepted if you want others keep helping you. – AltaGrade Mar 23 '18 at 11:30
0

Here is the code that should work for your use case. Create a custom module and replace YOUR_CUSTOM_MODULE part:

function YOUR_CUSTOM_MODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    // Exclude cases when query contains 'field_color_tid'.
    if (strpos($_SERVER['QUERY_STRING'],'field_color_tid') === FALSE) {
      // Act only if query string contains 'field_size_tid'
      if (strpos($_SERVER['QUERY_STRING'],'field_size_tid') !== FALSE) {
        $selected_tid = $form_state['view']->exposed_input['field_size_tid'];
        // Check if such taxonomy term exists.
        $term = taxonomy_term_load($selected_tid);
        if (!empty($term)) {
          // Redirect to taxonomy page.
          $path_alias = drupal_get_path_alias('taxonomy/term/'.$selected_tid);
          drupal_goto($path_alias);
        }
      }
    }
  }
}
0

I have a slightly different solution. Hope this helps too...

  • You can have as many taxonomy categories as you need as exposed filters.
  • Each vocabulary may have different pathauto patterns.
  • Other exposed filters should still work without any conflict.

Place this code in your custom module (CUSTOMMODULE.module):

/**
 * Implements hook_form_FORM_ID_alter().
 */
function CUSTOMMODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  // Replace `Size` filter.
  CUSTOMMODULE_jumpmenu($form, 'field_size_tid');

  // Replace `Color` filter.
  CUSTOMMODULE_jumpmenu($form, 'field_color_tid');
}

/**
 * Transform select filters into a simple jump menu.
 *
 * @param $form
 *   Nested array of form elements that comprise the form.
 * @param $field_name
 *   String representing the name of the field to be replaced.
 */
function CUSTOMMODULE_jumpmenu(&$form, $field_name) {
  if (
    isset($form[$field_name])
    && ($form[$field_name]['#type'] === 'select')
    && !empty($form[$field_name]['#options'])
  ) {
    $options = &$form[$field_name]['#options'];
    foreach ($options as $tid => $value) {
      if (is_int($tid)) {
        $path = drupal_get_path_alias('taxonomy/term/' . $tid);
        $options[url($path)] = $value;
        unset($options[$tid]);
      }
    }

    $form[$field_name]['#attributes']['onChange'] = 'javascript:if (this.value !== "All") { location.href = this.value; return false; }';

    return $form;
  }

  return [];
}

You can test this solution with this simple view:

$view = new view();
$view->name = 'example_view';
$view->description = '';
$view->tag = 'drupal_stackexchange';
$view->base_table = 'node';
$view->human_name = 'Example view';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; 

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Example view';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '50';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  'title' => 'title',
  'field_color' => 'field_color',
  'field_size' => 'field_size',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'title' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'field_color' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'field_size' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Field: Content: Color */
$handler->display->display_options['fields']['field_color']['id'] = 'field_color';
$handler->display->display_options['fields']['field_color']['table'] = 'field_data_field_color';
$handler->display->display_options['fields']['field_color']['field'] = 'field_color';
/* Field: Content: Size */
$handler->display->display_options['fields']['field_size']['id'] = 'field_size';
$handler->display->display_options['fields']['field_size']['table'] = 'field_data_field_size';
$handler->display->display_options['fields']['field_size']['field'] = 'field_size';
/* Sort criterion: Content: Title */
$handler->display->display_options['sorts']['title']['id'] = 'title';
$handler->display->display_options['sorts']['title']['table'] = 'node';
$handler->display->display_options['sorts']['title']['field'] = 'title';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'tshirt' => 'tshirt',
);
/* Filter criterion: Content: Size (field_size) */
$handler->display->display_options['filters']['field_size_tid']['id'] = 'field_size_tid';
$handler->display->display_options['filters']['field_size_tid']['table'] = 'field_data_field_size';
$handler->display->display_options['filters']['field_size_tid']['field'] = 'field_size_tid';
$handler->display->display_options['filters']['field_size_tid']['exposed'] = TRUE;
$handler->display->display_options['filters']['field_size_tid']['expose']['operator_id'] = 'field_size_tid_op';
$handler->display->display_options['filters']['field_size_tid']['expose']['label'] = 'Size';
$handler->display->display_options['filters']['field_size_tid']['expose']['operator'] = 'field_size_tid_op';
$handler->display->display_options['filters']['field_size_tid']['expose']['identifier'] = 'field_size_tid';
$handler->display->display_options['filters']['field_size_tid']['expose']['remember_roles'] = array(
  2 => '2',
  1 => 0,
  3 => 0,
);
$handler->display->display_options['filters']['field_size_tid']['type'] = 'select';
$handler->display->display_options['filters']['field_size_tid']['vocabulary'] = 'size';
/* Filter criterion: Content: Color (field_color) */
$handler->display->display_options['filters']['field_color_tid']['id'] = 'field_color_tid';
$handler->display->display_options['filters']['field_color_tid']['table'] = 'field_data_field_color';
$handler->display->display_options['filters']['field_color_tid']['field'] = 'field_color_tid';
$handler->display->display_options['filters']['field_color_tid']['exposed'] = TRUE;
$handler->display->display_options['filters']['field_color_tid']['expose']['operator_id'] = 'field_color_tid_op';
$handler->display->display_options['filters']['field_color_tid']['expose']['label'] = 'Color';
$handler->display->display_options['filters']['field_color_tid']['expose']['operator'] = 'field_color_tid_op';
$handler->display->display_options['filters']['field_color_tid']['expose']['identifier'] = 'field_color_tid';
$handler->display->display_options['filters']['field_color_tid']['expose']['remember_roles'] = array(
  2 => '2',
  1 => 0,
  3 => 0,
);
$handler->display->display_options['filters']['field_color_tid']['type'] = 'select';
$handler->display->display_options['filters']['field_color_tid']['vocabulary'] = 'color';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'example-view';
$handler->display->display_options['menu']['type'] = 'normal';
$handler->display->display_options['menu']['title'] = 'Example view';
$handler->display->display_options['menu']['name'] = 'main-menu';
0

In the end, I did this way:

    $params = drupal_get_query_parameters();
    $redirected_fields = array('field_size_tid','field_class_tid','field_color_tid', field_brand_tid);
    $checked_fields = array();

    foreach ($params as $key => $val) {
      if ($val == '' || $val == 'All') {} else { $checked_fields[] = $key; }
    }

    if ((count($checked_fields) == 1) && (in_array($checked_fields[0],$redirected_fields)) && (count($params[$checked_fields[0]]) == 1)) {
      $selected_tid = $form_state['view']->exposed_input[$checked_fields[0]];
      if (is_array($selected_tid)) {$selected_tid = implode('',$selected_tid);}
      // Check if such taxonomy term exists.
      $term = taxonomy_term_load($selected_tid);
      //dpm($term);
      if (!empty($term)) {
        // Redirect to taxonomy page.
        $path_alias = drupal_get_path_alias('taxonomy/term/'.$selected_tid);
        drupal_goto($path_alias);
      }
    }    
  • In principle, it's the same approach as I suggested. Besides you gave only size and color arguments, never mentioned class on your question. Anyways, don't leave your questions with no any answer accepted if you want keep getting help from others. – AltaGrade Mar 31 '18 at 14:02
  • Yes, I made my code based on yours, because In my question, the field structure was hardened. Your answer is very helpful, thank you. – Viktor Apr 5 '18 at 8:07
  • Viktor, that is why you have to be little bit more appreciative and accept my answer. Please understand that you discourage other users to work to help you if you just use their code without accepting their answers. – AltaGrade Apr 9 '18 at 16:06
  • But maybe you don't know how to ACCEPT an answer around here? Please read meta.stackexchange.com/questions/5234/… – AltaGrade Apr 9 '18 at 16:08
  • Sorry, I did not know about that. Thank you very much for your advice and help. – Viktor Apr 11 '18 at 11:34

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.