I'm working on updating our sites to D9. One of our custom modules stopped working and I narrowed it down to this bit of code that modifies some query conditions for a view filter. It's trying to set a condition that always returns true (i.e. 1 = 1).

function views_taxonomy_filter_process_query_conditions(array &$conditions, $field) {
  foreach (Element::children($conditions) as $key) {
    if ($conditions[$key]['field'] instanceof ConditionInterface) {
      $child = & $conditions[$key]['field']->conditions();
      views_taxonomy_filter_process_query_conditions($child, $field);
    }
    elseif (strpos($conditions[$key]['field'], $field) !== FALSE) {
      // We cannot just unset the condition, because this action might create
      // incorrect query. Let's replace it with 1 = 1 condition.
      $conditions[$key] = [
        'field' => 1,
        'value' => 1,
        'operator' => '=',
      ];
    }
  }
}

This worked fine in D8. However I'm now getting this error as it seems to be looking for a column called 1 instead of evaluating that condition:

Drupal\Core\Database\DatabaseExceptionWrapper: 
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'where clause'

How can I change this to restore the old functionality? Thank you!

有帮助吗?

解决方案

I found out here that since Drupal 8.7.0 (at least) the field part of the condition can not be an expression.

Since you can't use "where" you must figure out another way to force the condition to always be true. Maybe you could mess around with the operator like this:

  1. Instead of overriding the entire condition, use the "LIKE" operator:

    $conditions[$key]['value'] = '%';
    $conditions[$key]['operator'] = 'like';
    
  2. Or if that doesn't work, the "NOT IN" operator:

    $conditions[$key]['value'] = array('w00t');
    $conditions[$key]['operator'] = 'NOT IN';
    

Hope this helps you!

许可以下: CC-BY-SA归因
scroll top