You are here

protected function RestfulDataProviderEFQ::queryForListFilter in RESTful 7

Filter the query for list.

Parameters

\EntityFieldQuery $query: The query object.

Throws

\RestfulBadRequestException

See also

\RestfulEntityBase::getQueryForList

2 calls to RestfulDataProviderEFQ::queryForListFilter()
RestfulDataProviderEFQ::getQueryCount in plugins/restful/RestfulDataProviderEFQ.php
Prepare a query for RestfulEntityBase::getTotalCount().
RestfulDataProviderEFQ::getQueryForList in plugins/restful/RestfulDataProviderEFQ.php
Prepare a query for RestfulEntityBase::getList().

File

plugins/restful/RestfulDataProviderEFQ.php, line 166
Contains \RestfulDataProviderEFQ

Class

RestfulDataProviderEFQ
@file Contains \RestfulDataProviderEFQ

Code

protected function queryForListFilter(\EntityFieldQuery $query) {
  $public_fields = $this
    ->getPublicFields();
  foreach ($this
    ->parseRequestForListFilter() as $filter) {

    // Determine if filtering is by field or property.
    if (!($property_name = $public_fields[$filter['public_field']]['property'])) {
      throw new \RestfulBadRequestException('The current filter selection does not map to any entity property or Field API field.');
    }
    if (field_info_field($property_name)) {
      if (in_array(strtoupper($filter['operator'][0]), array(
        'IN',
        'NOT IN',
        'BETWEEN',
      ))) {
        if (is_array($filter['value']) && empty($filter['value'])) {
          if (strtoupper($filter['operator'][0]) == 'NOT IN') {

            // Skip filtering by an empty value when operator is 'NOT IN',
            // since it throws an SQL error.
            continue;
          }

          // Since Drupal doesn't know how to handle an empty array within a
          // condition we add the `NULL` as an element to the array.
          $filter['value'] = array(
            NULL,
          );
        }
        $query
          ->fieldCondition($public_fields[$filter['public_field']]['property'], $public_fields[$filter['public_field']]['column'], $filter['value'], $filter['operator'][0]);
        continue;
      }
      for ($index = 0; $index < count($filter['value']); $index++) {
        $query
          ->fieldCondition($public_fields[$filter['public_field']]['property'], $public_fields[$filter['public_field']]['column'], $filter['value'][$index], $filter['operator'][$index]);
      }
    }
    else {
      $column = $this
        ->getColumnFromProperty($property_name);
      if (in_array(strtoupper($filter['operator'][0]), array(
        'IN',
        'NOT IN',
        'BETWEEN',
      ))) {
        if (is_array($filter['value']) && empty($filter['value'])) {
          if (strtoupper($filter['operator'][0]) == 'NOT IN') {

            // Skip filtering by an empty value when operator is 'NOT IN',
            // since it throws an SQL error.
            continue;
          }

          // Since Drupal doesn't know how to handle an empty array within a
          // condition we add the `NULL` as an element to the array.
          $filter['value'] = array(
            NULL,
          );
        }
        $query
          ->propertyCondition($column, $filter['value'], $filter['operator'][0]);
        continue;
      }
      for ($index = 0; $index < count($filter['value']); $index++) {
        $query
          ->propertyCondition($column, $filter['value'][$index], $filter['operator'][$index]);
      }
    }
  }
}