2

I am having a problem trying to change the query output on my view.

I am using this code:

function zed_views_query_alter(&$view, &$query){
  if ($view->name == 'questions_home' && $view->current_display == 'rates_related') {
    $or = db_or();
    dpr($query->where[1]['conditions'][2]['field']->condition());
  }
}

I am trying to alter the word %california%. The output is the following one.

DatabaseCondition Object
(
    [conditions:protected] => Array
        (
            [#conjunction] => OR
            [0] => Array
                (
                    [field] => node.title
                    [value] => %california%
                    [operator] => LIKE
                )

            [1] => Array
                (
                    [field] => 
                    [value] => 
                    [operator] => IS NULL
                )

        )

    [arguments:protected] => Array
        (
        )

    [changed:protected] => 1
    [queryPlaceholderIdentifier:protected] => 
)

How can I go about changing the value %california%?

3
function zed_views_query_alter(&$view, &$query) {
  if ($view->name == 'questions_home' && $view->current_display == 'rates_related') {
    foreach($query->where[1]['conditions'] as $idx => $condition) {
      if (!is_object($condition['field']) && $condition['field'] == 'node.title') {
        $query->where[1]['conditions'][$idx]['value'] = '%whateveryoulike%';
        // or you can unset this condition and write down a new one
        // unset($query->where[1]['conditions'][$idx]);
        // $query->add_where_expression('1', "$field LIKE %" . db_like("california") . "% OR field LIKE %" . db_like("somethingelse") . "%");
      }
    }
    dpr($query->where[1]['conditions']);
  }
}
0

To change query values in Filter criteria, you can use hook_views_query_alter in the following way:

/**
 * Implements hook_views_query_alter().
 */
function MYMODULE_views_query_alter(&$view, &$query) {
  if ($view->name == 'my_view') {

    // Assign my values.
    $value1 = 'This is my value for filter 1.';
    $value2 = 'This is my value for filter 2.';
    $value3 = 'This is my value for filter 3.';

    // Programatically update the filter criteria of a view.
    array_walk_recursive($query->where, create_function('&$v, $k', '$v = str_replace("_MY_TOKEN1_", "'.$value1.'", $v);'));
    array_walk_recursive($query->where, create_function('&$v, $k', '$v = str_replace("_MY_TOKEN2_", "'.$value2.'", $v);'));
    array_walk_recursive($query->where, create_function('&$v, $k', '$v = str_replace("_MY_TOKEN3_", "'.$value3.'", $v);'));
  }
}

Just change your value in your view to some custom token (_MY_TOKEN1_) and the function automatically find the right array key and it'll alter its value.

If you're using YQL View, use hook_yql_views_query_query_alter() instead.

0
if($view->name == "SomeView_Name"){
       $query->where[1]['conditions'][0] = array('field'=>"node.title","value"=>'%california%',"operator"=>"LIKE");
}

I think this will help you.

0

In Case of views query alter hooks we can use syntax similar below mentions

function hook_views_query_alter(&$view, &$query) {
  global $user;  
  if ($view->name == "Alerts") {    
     $query->where [0] ['conditions'] ['0'] ['value'] = '%' . $user->name . '%';
     $query->where [0] ['conditions'] ['0'] ['operator'] = "LIKE";
  }
}

Also you can change other $query->where positions data, This will impact in your query.

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .