You are here

protected function LingotekManagementForm::getFilteredEntities in Lingotek Translation 3.2.x

Same name and namespace in other branches
  1. 8.2 src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()
  2. 4.0.x src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()
  3. 3.0.x src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()
  4. 3.1.x src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()
  5. 3.3.x src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()
  6. 3.4.x src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()
  7. 3.5.x src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()
  8. 3.6.x src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()
  9. 3.7.x src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()
  10. 3.8.x src/Form/LingotekManagementForm.php \Drupal\lingotek\Form\LingotekManagementForm::getFilteredEntities()

Gets the entities that needs to be displayed based on the current filters.

Return value

\Drupal\Core\Entity\EntityInterface[] The entities

Overrides LingotekManagementFormBase::getFilteredEntities

File

src/Form/LingotekManagementForm.php, line 47

Class

LingotekManagementForm
Form for bulk management of content.

Namespace

Drupal\lingotek\Form

Code

protected function getFilteredEntities() {
  $items_per_page = $this
    ->getItemsPerPage();

  /** @var PrivateTempStore $temp_store */
  $temp_store = $this->tempStoreFactory
    ->get($this
    ->getTempStorageFilterKey());

  /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
  $entity_type = $this->entityTypeManager
    ->getDefinition($this->entityTypeId);

  /** @var \Drupal\Core\Database\Query\SelectInterface $query */
  $query = $this->connection
    ->select($entity_type
    ->getBaseTable(), 'entity_table')
    ->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender');
  $query
    ->fields('entity_table', [
    $entity_type
      ->getKey('id'),
  ]);
  $union = NULL;
  $union2 = NULL;
  $has_bundles = $entity_type
    ->get('bundle_entity_type') != 'bundle';
  $groupsExists = $this->moduleHandler
    ->moduleExists('group') && $this->entityTypeId === 'node';

  // Filter results
  // Default options
  $labelFilter = $temp_store
    ->get('label');
  $bundleFilter = $temp_store
    ->get('bundle');
  $groupFilter = $groupsExists ? $temp_store
    ->get('group') : NULL;
  $jobFilter = $temp_store
    ->get('job');

  // Advanced options
  $documentIdFilter = $temp_store
    ->get('document_id');
  $entityIdFilter = $temp_store
    ->get('entity_id');
  $sourceLanguageFilter = $temp_store
    ->get('source_language');
  $sourceStatusFilter = $temp_store
    ->get('source_status');
  $targetStatusFilter = $temp_store
    ->get('target_status');
  $profileFilter = $temp_store
    ->get('profile');
  if ($sourceStatusFilter) {
    if ($sourceStatusFilter === 'UPLOAD_NEEDED') {

      // We consider that "Upload Needed" includes those never uploaded or
      // disassociated, edited, or with error on last upload.
      $needingUploadStatuses = [
        Lingotek::STATUS_EDITED,
        Lingotek::STATUS_REQUEST,
        Lingotek::STATUS_CANCELLED,
        Lingotek::STATUS_ERROR,
      ];
      $metadata_type = $this->entityTypeManager
        ->getDefinition('lingotek_content_metadata');
      $query
        ->innerJoin($metadata_type
        ->getBaseTable(), 'metadata_source', 'entity_table.' . $entity_type
        ->getKey('id') . '= metadata_source.content_entity_id AND metadata_source.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $query
        ->innerJoin('lingotek_content_metadata__translation_status', 'translation_status', 'metadata_source.id = translation_status.entity_id AND translation_status.translation_status_language = entity_table.' . $entity_type
        ->getKey('langcode'));
      $query
        ->condition('translation_status.translation_status_value', $needingUploadStatuses, 'IN');

      // No metadata.
      $no_metadata_query = $this->connection
        ->select($metadata_type
        ->getBaseTable(), 'mt');
      $no_metadata_query
        ->fields('mt', [
        $metadata_type
          ->getKey('id'),
      ]);
      $no_metadata_query
        ->where('entity_table.' . $entity_type
        ->getKey('id') . ' = mt.content_entity_id');
      $no_metadata_query
        ->condition('mt.content_entity_type_id', $entity_type
        ->id());
      $union = $this->connection
        ->select($entity_type
        ->getBaseTable(), 'entity_table');
      $union
        ->fields('entity_table', [
        $entity_type
          ->getKey('id'),
      ]);
      $union
        ->condition('entity_table.' . $entity_type
        ->getKey('langcode'), LanguageInterface::LANGCODE_NOT_SPECIFIED, '!=');
      $union
        ->notExists($no_metadata_query);

      // No target statuses.
      $no_statuses_query = $this->connection
        ->select('lingotek_content_metadata__translation_status', 'tst');
      $no_statuses_query
        ->fields('tst', [
        'entity_id',
      ]);
      $no_statuses_query
        ->where('mt2.' . $metadata_type
        ->getKey('id') . ' = tst.entity_id');
      $union2 = $this->connection
        ->select($entity_type
        ->getBaseTable(), 'entity_table');
      $union2
        ->innerJoin($metadata_type
        ->getBaseTable(), 'mt2', 'entity_table.' . $entity_type
        ->getKey('id') . '= mt2.content_entity_id AND mt2.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $union2
        ->fields('entity_table', [
        $entity_type
          ->getKey('id'),
      ]);
      $union2
        ->condition('entity_table.' . $entity_type
        ->getKey('langcode'), LanguageInterface::LANGCODE_NOT_SPECIFIED, '!=');
      $union2
        ->notExists($no_statuses_query);
      $query
        ->union($union);
      $query
        ->union($union2);
    }
    else {
      $metadata_type = $this->entityTypeManager
        ->getDefinition('lingotek_content_metadata');
      $query
        ->innerJoin($metadata_type
        ->getBaseTable(), 'metadata_source', 'entity_table.' . $entity_type
        ->getKey('id') . '= metadata_source.content_entity_id AND metadata_source.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $query
        ->innerJoin('lingotek_content_metadata__translation_status', 'translation_status', 'metadata_source.id = translation_status.entity_id AND translation_status.translation_status_language = entity_table.' . $entity_type
        ->getKey('langcode'));
      $query
        ->condition('translation_status.translation_status_value', $sourceStatusFilter);
    }
  }

  // Default queries
  if ($has_bundles && $bundleFilter) {
    if (!in_array("", $bundleFilter, TRUE)) {
      $query
        ->condition('entity_table.' . $entity_type
        ->getKey('bundle'), $bundleFilter, 'IN');
      if ($union !== NULL) {
        $union
          ->condition('entity_table.' . $entity_type
          ->getKey('bundle'), $bundleFilter, 'IN');
      }
      if ($union2 !== NULL) {
        $union2
          ->condition('entity_table.' . $entity_type
          ->getKey('bundle'), $bundleFilter, 'IN');
      }
    }
  }
  if ($labelFilter) {
    $labelKey = $entity_type
      ->getKey('label');
    if ($labelKey) {
      $query
        ->innerJoin($entity_type
        ->getDataTable(), 'entity_data', 'entity_table.' . $entity_type
        ->getKey('id') . '= entity_data.' . $entity_type
        ->getKey('id'));
      $query
        ->condition('entity_data.' . $labelKey, '%' . $labelFilter . '%', 'LIKE');
      if ($union !== NULL) {
        $union
          ->innerJoin($entity_type
          ->getDataTable(), 'entity_data', 'entity_table.' . $entity_type
          ->getKey('id') . '= entity_data.' . $entity_type
          ->getKey('id'));
        $union
          ->condition('entity_data.' . $labelKey, '%' . $labelFilter . '%', 'LIKE');
      }
      if ($union2 !== NULL) {
        $union2
          ->innerJoin($entity_type
          ->getDataTable(), 'entity_data', 'entity_table.' . $entity_type
          ->getKey('id') . '= entity_data.' . $entity_type
          ->getKey('id'));
        $union2
          ->condition('entity_data.' . $labelKey, '%' . $labelFilter . '%', 'LIKE');
      }
    }
  }
  if ($groupFilter) {

    /** @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface $groupContentEnablers */
    $groupType = Group::load($groupFilter)
      ->getGroupType();
    $groupContentEnablers = \Drupal::service('plugin.manager.group_content_enabler');
    $definitions = $groupContentEnablers
      ->getDefinitions();
    $definitions = array_filter($definitions, function ($definition) {
      return $definition['entity_type_id'] === 'node';
    });
    $valid_values = [];
    foreach ($definitions as $node_definition) {
      $valid_values[] = $groupType
        ->id() . '-' . $node_definition['id'] . '-' . $node_definition['entity_bundle'];
    }
    $query
      ->innerJoin('group_content_field_data', 'group_content', 'entity_table.' . $entity_type
      ->getKey('id') . '= group_content.entity_id');
    $query
      ->condition('group_content.gid', $groupFilter);
    $query
      ->condition('group_content.type', $valid_values, 'IN');
    if ($union !== NULL) {
      $union
        ->innerJoin('group_content_field_data', 'group_content', 'entity_table.' . $entity_type
        ->getKey('id') . '= group_content.entity_id');
      $union
        ->condition('group_content.gid', $groupFilter);
      $union
        ->condition('group_content.type', $valid_values, 'IN');
    }
    if ($union2 !== NULL) {
      $union2
        ->innerJoin('group_content_field_data', 'group_content', 'entity_table.' . $entity_type
        ->getKey('id') . '= group_content.entity_id');
      $union2
        ->condition('group_content.gid', $groupFilter);
      $union2
        ->condition('group_content.type', $valid_values, 'IN');
    }
  }
  if ($jobFilter) {

    /** @var \Drupal\Core\Entity\EntityTypeInterface $metadata_type */
    $metadata_type = $this->entityTypeManager
      ->getDefinition('lingotek_content_metadata');
    $query
      ->innerJoin($metadata_type
      ->getBaseTable(), 'metadata', 'entity_table.' . $entity_type
      ->getKey('id') . '= metadata.content_entity_id AND metadata.content_entity_type_id = \'' . $entity_type
      ->id() . '\'');
    $query
      ->condition('metadata.job_id', '%' . $jobFilter . '%', 'LIKE');
    if ($union !== NULL) {
      $union
        ->innerJoin($metadata_type
        ->getBaseTable(), 'metadata', 'entity_table.' . $entity_type
        ->getKey('id') . '= metadata.content_entity_id AND metadata.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $union
        ->condition('metadata.job_id', '%' . $jobFilter . '%', 'LIKE');
    }
    if ($union2 !== NULL) {
      $union2
        ->innerJoin($metadata_type
        ->getBaseTable(), 'metadata', 'entity_table.' . $entity_type
        ->getKey('id') . '= metadata.content_entity_id AND metadata.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $union2
        ->condition('metadata.job_id', '%' . $jobFilter . '%', 'LIKE');
    }
  }

  // Advanced queries
  if ($documentIdFilter) {
    $documentIdArray = explode(',', $documentIdFilter);
    array_walk($documentIdArray, function (&$value) {
      $value = trim($value);
    });
    $documentIdOperator = count($documentIdArray) > 1 ? 'IN' : 'LIKE';
    $documentIdValue = count($documentIdArray) > 1 ? $documentIdArray : '%' . $documentIdFilter . '%';
    $metadata_type = $this->entityTypeManager
      ->getDefinition('lingotek_content_metadata');
    $query
      ->innerJoin($metadata_type
      ->getBaseTable(), 'metadata', 'entity_table.' . $entity_type
      ->getKey('id') . '= metadata.content_entity_id AND metadata.content_entity_type_id = \'' . $entity_type
      ->id() . '\'');
    $query
      ->condition('metadata.document_id', $documentIdValue, $documentIdOperator);
    if ($union !== NULL) {
      $union
        ->innerJoin($metadata_type
        ->getBaseTable(), 'metadata', 'entity_table.' . $entity_type
        ->getKey('id') . '= metadata.content_entity_id AND metadata.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $union
        ->condition('metadata.document_id', $documentIdValue, $documentIdOperator);
    }
    if ($union2 !== NULL) {
      $union2
        ->innerJoin($metadata_type
        ->getBaseTable(), 'metadata', 'entity_table.' . $entity_type
        ->getKey('id') . '= metadata.content_entity_id AND metadata.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $union2
        ->condition('metadata.document_id', $documentIdValue, $documentIdOperator);
    }
  }
  if ($entityIdFilter) {
    $entityIdArray = explode(',', $entityIdFilter);
    array_walk($entityIdArray, function (&$value) {
      $value = trim($value);
    });
    $entityIdOperator = count($entityIdArray) > 1 ? 'IN' : '=';
    $entityIdValue = count($entityIdArray) > 1 ? $entityIdArray : $entityIdFilter;
    $query
      ->innerJoin($entity_type
      ->getDataTable(), 'entity_data', 'entity_table.' . $entity_type
      ->getKey('id') . '= entity_data.' . $entity_type
      ->getKey('id'));
    $query
      ->condition('entity_table.' . $entity_type
      ->getKey('id'), $entityIdValue, $entityIdOperator);
    if ($union !== NULL) {
      $union
        ->innerJoin($entity_type
        ->getDataTable(), 'entity_data', 'entity_table.' . $entity_type
        ->getKey('id') . '= entity_data.' . $entity_type
        ->getKey('id'));
      $union
        ->condition('entity_table.' . $entity_type
        ->getKey('id'), $entityIdValue, $entityIdOperator);
    }
    if ($union2 !== NULL) {
      $union2
        ->innerJoin($entity_type
        ->getDataTable(), 'entity_data', 'entity_table.' . $entity_type
        ->getKey('id') . '= entity_data.' . $entity_type
        ->getKey('id'));
      $union2
        ->condition('entity_table.' . $entity_type
        ->getKey('id'), $entityIdValue, $entityIdOperator);
    }
  }
  if ($profileFilter) {
    $profileOptions = $this->lingotekConfiguration
      ->getProfileOptions();
    if (is_string($profileFilter)) {
      $profileFilter = [
        $profileFilter,
      ];
    }
    if (!in_array("", $profileFilter, TRUE)) {
      $metadata_type = $this->entityTypeManager
        ->getDefinition('lingotek_content_metadata');
      $query
        ->innerJoin($metadata_type
        ->getBaseTable(), 'metadata', 'entity_table.' . $entity_type
        ->getKey('id') . '= metadata.content_entity_id AND metadata.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $query
        ->condition('metadata.profile', $profileFilter, 'IN');
      if ($union !== NULL) {
        $union
          ->innerJoin($metadata_type
          ->getBaseTable(), 'metadata', 'entity_table.' . $entity_type
          ->getKey('id') . '= metadata.content_entity_id AND metadata.content_entity_type_id = \'' . $entity_type
          ->id() . '\'');
        $union
          ->condition('metadata.profile', $profileFilter, 'IN');
      }
      if ($union2 !== NULL) {
        $union2
          ->innerJoin($metadata_type
          ->getBaseTable(), 'metadata', 'entity_table.' . $entity_type
          ->getKey('id') . '= metadata.content_entity_id AND metadata.content_entity_type_id = \'' . $entity_type
          ->id() . '\'');
        $union2
          ->condition('metadata.profile', $profileFilter, 'IN');
      }
    }
  }
  if ($sourceLanguageFilter) {
    $query
      ->innerJoin($entity_type
      ->getDataTable(), 'entity_data', 'entity_table.' . $entity_type
      ->getKey('id') . '= entity_data.' . $entity_type
      ->getKey('id'));
    $query
      ->condition('entity_table.' . $entity_type
      ->getKey('langcode'), $sourceLanguageFilter);
    $query
      ->condition('entity_data.default_langcode', 1);
    if ($union !== NULL) {
      $union
        ->innerJoin($entity_type
        ->getDataTable(), 'entity_data', 'entity_table.' . $entity_type
        ->getKey('id') . '= entity_data.' . $entity_type
        ->getKey('id'));
      $union
        ->condition('entity_table.' . $entity_type
        ->getKey('langcode'), $sourceLanguageFilter);
      $union
        ->condition('entity_data.default_langcode', 1);
    }
    if ($union2 !== NULL) {
      $union2
        ->innerJoin($entity_type
        ->getDataTable(), 'entity_data', 'entity_table.' . $entity_type
        ->getKey('id') . '= entity_data.' . $entity_type
        ->getKey('id'));
      $union2
        ->condition('entity_table.' . $entity_type
        ->getKey('langcode'), $sourceLanguageFilter);
      $union2
        ->condition('entity_data.default_langcode', 1);
    }
  }

  // We don't want items with language undefined.
  $query
    ->condition('entity_table.' . $entity_type
    ->getKey('langcode'), LanguageInterface::LANGCODE_NOT_SPECIFIED, '!=');
  if ($targetStatusFilter) {
    $metadata_type = $this->entityTypeManager
      ->getDefinition('lingotek_content_metadata');
    $query
      ->innerJoin($metadata_type
      ->getBaseTable(), 'metadata_target', 'entity_table.' . $entity_type
      ->getKey('id') . '= metadata_target.content_entity_id AND metadata_target.content_entity_type_id = \'' . $entity_type
      ->id() . '\'');
    $query
      ->innerJoin('lingotek_content_metadata__translation_status', 'translation_target_status', 'metadata_target.id = translation_target_status.entity_id AND translation_target_status.translation_status_language <> entity_table.' . $entity_type
      ->getKey('langcode'));
    $query
      ->condition('translation_target_status.translation_status_value', $targetStatusFilter);
    if ($union !== NULL) {
      $union
        ->innerJoin($metadata_type
        ->getBaseTable(), 'metadata_target', 'entity_table.' . $entity_type
        ->getKey('id') . '= metadata_target.content_entity_id AND metadata_target.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $union
        ->innerJoin('lingotek_content_metadata__translation_status', 'translation_target_status', 'metadata_target.id = translation_target_status.entity_id AND translation_target_status.translation_status_language <> entity_table.' . $entity_type
        ->getKey('langcode'));
      $union
        ->condition('translation_target_status.translation_status_value', $targetStatusFilter);
    }
    if ($union2 !== NULL) {
      $union2
        ->innerJoin($metadata_type
        ->getBaseTable(), 'metadata_target', 'entity_table.' . $entity_type
        ->getKey('id') . '= metadata_target.content_entity_id AND metadata_target.content_entity_type_id = \'' . $entity_type
        ->id() . '\'');
      $union2
        ->innerJoin('lingotek_content_metadata__translation_status', 'translation_target_status', 'metadata_target.id = translation_target_status.entity_id AND translation_target_status.translation_status_language <> entity_table.' . $entity_type
        ->getKey('langcode'));
      $union2
        ->condition('translation_target_status.translation_status_value', $targetStatusFilter);
    }
  }
  $ids = $query
    ->limit($items_per_page)
    ->execute()
    ->fetchCol(0);
  $entities = $this->entityTypeManager
    ->getStorage($this->entityTypeId)
    ->loadMultiple($ids);
  return $entities;
}