You are here

protected function ContentEntityStorageTrait::buildQuery in Multiversion 8

Same name and namespace in other branches
  1. 8.2 src/Entity/Storage/ContentEntityStorageTrait.php \Drupal\multiversion\Entity\Storage\ContentEntityStorageTrait::buildQuery()

File

src/Entity/Storage/ContentEntityStorageTrait.php, line 53

Class

ContentEntityStorageTrait

Namespace

Drupal\multiversion\Entity\Storage

Code

protected function buildQuery($ids, $revision_ids = FALSE) {
  $query = parent::buildQuery($ids, $revision_ids);
  $enabled = \Drupal::state()
    ->get('multiversion.migration_done.' . $this
    ->getEntityTypeId(), FALSE);

  // Prevent to modify the query before entity type updates.
  if (!is_subclass_of($this->entityType
    ->getStorageClass(), ContentEntityStorageInterface::class) || !$enabled) {
    return $query;
  }
  $field_data_alias = 'base';
  $revision_data_alias = 'revision';
  if ($this->entityType
    ->isTranslatable()) {

    // Join the field data table in order to set the workspace condition.
    $field_data_table = $this
      ->getDataTable();
    $field_data_alias = 'field_data';
    $query
      ->join($field_data_table, $field_data_alias, "{$field_data_alias}.{$this->idKey} = base.{$this->idKey}");

    // Join the revision data table in order to set the delete condition.
    $revision_data_table = $this
      ->getRevisionDataTable();
    $revision_data_alias = 'revision_data';
    if ($revision_ids) {
      $query
        ->join($revision_data_table, $revision_data_alias, "{$revision_data_alias}.{$this->revisionKey} = revision.{$this->revisionKey} AND {$revision_data_alias}.{$this->revisionKey} IN (:revisionIds[])", [
        ':revisionIds[]' => (array) $revision_ids,
      ]);
    }
    else {
      $query
        ->join($revision_data_table, $revision_data_alias, "{$revision_data_alias}.{$this->revisionKey} = revision.{$this->revisionKey}");
    }
  }

  // Loading a revision is explicit. So when we try to load one we should do
  // so without a condition on the deleted flag.
  if (!$revision_ids) {
    $query
      ->condition("{$revision_data_alias}._deleted", (int) $this->isDeleted);
  }

  // Entities in other workspaces than the active one can only be queried with
  // the Entity Query API and not by the storage handler itself.
  // Just UserStorage can be queried in all workspaces by the storage handler.
  if (!$this instanceof UserStorageInterface) {

    // We have to join the data table to set a condition on the workspace.
    $query
      ->condition("{$field_data_alias}.workspace", $this
      ->getWorkspaceId());
  }
  return $query;
}