The entity.query
service has been deprecated in favor of directly using the getQuery() method on the entity storage handler for the given entity type.
The entity query internally already does that and using it directly simplifies the injected services as the entity storage is needed in 95% of the cases anyway to load the queried entity IDs.
For non-OOP code, \Drupal::entityQuery()
and \Drupal::entityQueryAggregate()
are not deprecated and can still be used.
For services, forms and controllers, change $this->entityQueryFactory->get('node')
to $this->entityTypeManager->getStorage('node')->getQuery()
or $this->nodeStorage->getQuery()
if the storage handler is already available.
Comments
Query for all nids of a content type
A working example to get all nids of a particular content type using the entity type manager (EntityTypeManager) instead of the query factory (QueryFactory):
This way seems better
For entities other than nodes
For non-node entities, make sure to use 'bundle' instead of 'type'
From the original post $this-
From the original post
$this->entityTypeManager->getStorage('node')->getQuery()
at least in Drupal 8.6+ entityTypeManager is a method
so should be called like this
$this->entityTypeManager()->getStorage('node')->getQuery()
------
GiorgosK
Web Development
Only if you're using it
Only if you're using it directly. You can also inject the service
entity_type.manager
and assign it to a property. See:Make sure you use the interface
Drupal\Core\Entity\EntityTypeManagerInterface
and not a specific implementation.Basically both are correct.
Basically both are correct. If you extend ControllerBase it will be
if you are using in form it will be
$node_storage = $this->entityTypeManager->getStorage('node');
source https://drupal.stackexchange.com/a/232911
Drupal\Core\Form\FormBase has no entityTypeManager
If you're extending
Drupal\Core\Form\FormBase
or the similarDrupal\Core\Form\ConfigFormBase
there is no entityTypeManager available. You have to override the create method and do your own injection.When extending a FormBase:
Or when extending a ConfigFormBase:
Thanks, I used this when
Thanks, I used this when extending formBase. I had to change it slightly to work.