You are here

protected function DataProviderEntity::queryForListSort in RESTful 7.2

Sort the query for list.

Parameters

\EntityFieldQuery $query: The query object.

Throws

\Drupal\restful\Exception\BadRequestException

\EntityFieldQueryException

See also

\RestfulEntityBase::getQueryForList

1 call to DataProviderEntity::queryForListSort()
DataProviderEntity::getQueryForList in src/Plugin/resource/DataProvider/DataProviderEntity.php
Prepare a query for RestfulEntityBase::getList().

File

src/Plugin/resource/DataProvider/DataProviderEntity.php, line 593
Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntity.

Class

DataProviderEntity
Class DataProviderEntity.

Namespace

Drupal\restful\Plugin\resource\DataProvider

Code

protected function queryForListSort(\EntityFieldQuery $query) {
  $resource_fields = $this->fieldDefinitions;

  // Get the sorting options from the request object.
  $sorts = $this
    ->parseRequestForListSort();
  $sorts = $sorts ? $sorts : $this
    ->defaultSortInfo();
  foreach ($sorts as $public_field_name => $direction) {

    // Determine if sorting is by field or property.

    /* @var \Drupal\restful\Plugin\resource\Field\ResourceFieldEntityInterface $resource_field */
    if (!($resource_field = $resource_fields
      ->get($public_field_name))) {
      return;
    }
    $sort = array(
      'public_field' => $public_field_name,
      'direction' => $direction,
      'resource_id' => $this->pluginId,
    );
    $sort = $this
      ->alterSortQuery($sort, $query);
    if (!empty($sort['processed'])) {

      // If the sort was already processed by the alter filters, continue.
      continue;
    }
    if (!($property_name = $resource_field
      ->getProperty())) {
      if (!$resource_field instanceof ResourceFieldEntityAlterableInterface) {
        throw new BadRequestException('The current sort selection does not map to any entity property or Field API field.');
      }

      // If there was no property but the resource field was sortable, do
      // not add the default field filtering.
      // TODO: This is a workaround. The filtering logic should live in the resource field class.
      return;
    }
    if (ResourceFieldEntity::propertyIsField($property_name)) {
      $query
        ->fieldOrderBy($property_name, $resource_field
        ->getColumn(), $sort['direction']);
    }
    else {
      $column = $this
        ->getColumnFromProperty($property_name);
      $query
        ->propertyOrderBy($column, $sort['direction']);
    }
  }
}