QueryFactory.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Drupal\Core\Entity\Query;
  3. @trigger_error('The ' . __NAMESPACE__ . '\QueryFactory class is deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityStorageInterface::getQuery() or \Drupal\Core\Entity\EntityStorageInterface::getAggregateQuery() instead. See https://www.drupal.org/node/2849874.', E_USER_DEPRECATED);
  4. use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  7. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  8. /**
  9. * Factory class Creating entity query objects.
  10. *
  11. * Any implementation of this service must call getQuery()/getAggregateQuery()
  12. * of the corresponding entity storage.
  13. *
  14. * @deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use
  15. * \Drupal\Core\Entity\EntityStorageInterface::getQuery() or
  16. * \Drupal\Core\Entity\EntityStorageInterface::getAggregateQuery() instead.
  17. *
  18. * @see https://www.drupal.org/node/2849874
  19. * @see \Drupal\Core\Entity\EntityStorageBase::getQuery()
  20. */
  21. class QueryFactory implements ContainerAwareInterface {
  22. use ContainerAwareTrait;
  23. use DeprecatedServicePropertyTrait;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
  28. /**
  29. * The entity type manager service.
  30. *
  31. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  32. */
  33. protected $entityTypeManager;
  34. /**
  35. * Constructs a QueryFactory object.
  36. *
  37. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  38. * The entity type manager service.
  39. */
  40. public function __construct(EntityTypeManagerInterface $entity_type_manager) {
  41. $this->entityTypeManager = $entity_type_manager;
  42. }
  43. /**
  44. * Returns a query object for a given entity type.
  45. *
  46. * @param string $entity_type_id
  47. * The entity type ID.
  48. * @param string $conjunction
  49. * - AND: all of the conditions on the query need to match.
  50. * - OR: at least one of the conditions on the query need to match.
  51. *
  52. * @return \Drupal\Core\Entity\Query\QueryInterface
  53. * The query object that can query the given entity type.
  54. */
  55. public function get($entity_type_id, $conjunction = 'AND') {
  56. return $this->entityTypeManager->getStorage($entity_type_id)->getQuery($conjunction);
  57. }
  58. /**
  59. * Returns an aggregated query object for a given entity type.
  60. *
  61. * @param string $entity_type_id
  62. * The entity type ID.
  63. * @param string $conjunction
  64. * - AND: all of the conditions on the query need to match.
  65. * - OR: at least one of the conditions on the query need to match.
  66. *
  67. * @return \Drupal\Core\Entity\Query\QueryAggregateInterface
  68. * The aggregated query object that can query the given entity type.
  69. */
  70. public function getAggregate($entity_type_id, $conjunction = 'AND') {
  71. return $this->entityTypeManager->getStorage($entity_type_id)->getAggregateQuery($conjunction);
  72. }
  73. }