0

When taxonomy terms are combined, the values searched are term id instead of term name. So I tried to alter the query using views_query_alter. But it seems the values are coming from object.

function MYMODULE_views_query_alter(&$view, &$query) {
 if ($view->name == 'my_view') {
   if (is_object($query->where[1]['conditions'][0]['field'])) {
     foreach ($query->where[1]['conditions'][0]['field']->conditions() as $key => $value) {
        if (is_numeric($key)) {
          $possible_terms[] = $value['value'][':views_combine'];
        }
      }
     // here i was planning to load the term id from the partial
     // term search key from combined filter
     // once the term is identified, i would like to update the query.
   }
 }
}

The problem is $query->where[1]['conditions'][0]['field']->conditions() is a function and I don't know how to substitute value to it.

2
  • If it's a function, it returns something . So you can catch the returned value (array) in a variable. And then find your value in it using var_dump or dpm and substitute it. Then do the foreach on your newly created variable. But keep in mind that the database had an index on taxanomy id, searching by name will go slower. Can't you use id's instead? Feb 12, 2015 at 13:04
  • thanks @Neograph734. The problem is I don't know how to substitute it to the function. It does return value, but I'm looking for way to set manipulated values.
    – pown
    Feb 13, 2015 at 5:33

2 Answers 2

1

The example on the hook_views_query_alter page should get you started. Whet they do here is changing the node title into the node id, by redefining the $condition variable.

function hook_views_query_alter(&$view, &$query) {
  // (Example assuming a view with an exposed filter on node title.)
  // If the input for the title filter is a positive integer, filter against
  // node ID instead of node title.
  if ($view->name == 'my_view' && is_numeric($view->exposed_raw_input['title']) && $view->exposed_raw_input['title'] > 0) {
    // Traverse through the 'where' part of the query.
    foreach ($query->where as &$condition_group) {]

// Run through all conditions and pass them by reference. 
// (Altering the value here will change it for all things later on.)

      foreach ($condition_group['conditions'] as &$condition) {
        // If this is the part of the query filtering on title, chang the
        // condition to filter on node ID.

//If there is a condition on the title field

        if ($condition['field'] == 'node.title') {

// Overwrite the condition 

          $condition = array(
            'field' => 'node.nid',
            'value' => $view->exposed_raw_input['title'],
            'operator' => '=',
          );
        }
      }
    }
  }
}

You should be able to copy quite a lot of this function, tweak the initial if statement a bit so it does not filter on title, and substitute your own values.

I have not used a combined filter, but the Views API converts into similar to this. If you add some debug code to find out what the initial array is, you should be able to substitute that.

3
  • Thanks @Neograph734. But this doesn't answer me, how to assign value to the function nested in the $query object
    – pown
    Feb 19, 2015 at 4:52
  • You cannot assign a value to a function. But you can replace the function with a value. Feb 19, 2015 at 11:11
  • I tried that, but it doesnt work
    – pown
    Feb 19, 2015 at 11:25
0

Don't use ...query_alter. That will just lead to headaches...

Use HOOK_form_alter(&$form, &$form_state, $form_id) and in this, assign to $form your new value via something like $form['#new_value'] = "whatever your value is" (which you'll need to do to pass your value on to your validate callback). After that, you then indicate that you need a validation callback by doing the following:

$form['#validate'][] = 'nameofyourmodule_validate';

Now outside of your ..form_alter hook, setup your callback as so:

function nameofyourmodule_validate(&$form, &$form_state){}

...and in that, call form_set_value as follows:

form_set_value($form['<name of your field you're combine field>'], $form['#new_value'], $form_state);

Remember to keep clearing your cache during development / testing. It's so easy to forget doing that when you sometimes need to.

Best of luck and if you need more information, feel free to shoot me a message.

2
  • Thanks. I tried, but it doesn't work for combined filter.
    – pown
    Mar 28, 2015 at 4:49
  • Yes. It. Does. It's working in my Drupal 7.x install from within a custom module. You need to revisit it because it does work. You use HOOK_form_alter, setup a validate callback within it, and then use the validate callback to alter the value of your global combine.
    – Wolf_22
    Mar 28, 2015 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.