Change record status: 
Project: 
Introduced in branch: 
8.3.x
Introduced in version: 
8.3.0
Description: 

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.

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

mikemadison’s picture

A working example to get all nids of a particular content type using the entity type manager (EntityTypeManager) instead of the query factory (QueryFactory):

$query = $this->entityTypeManager->getStorage('node')->getQuery();
$nids = $query->condition('type', '<content_type>')
  ->condition('status', '1')
  ->execute();
$nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);
Pauline G’s picture

$nids = \Drupal::entityTypeManager()
          ->getListBuilder('node')
          ->getStorage()
          ->loadByProperties([
            'type' => 'mytype',
            'status' => 1,
        ]);
oo0shiny’s picture

For non-node entities, make sure to use 'bundle' instead of 'type'

$media = \Drupal::entityTypeManager()
          ->getListBuilder('media')
          ->getStorage()
          ->loadByProperties([
            'bundle' => 'media_bundle_name',
            'status' => 1,
        ]);
GiorgosK’s picture

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

imclean’s picture

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.

Bhanu951’s picture

Basically both are correct. If you extend ControllerBase it will be

$node_storage = $this->entityTypeManager()->getStorage('node'); 

if you are using in form it will be

$node_storage = $this->entityTypeManager->getStorage('node');

source https://drupal.stackexchange.com/a/232911

jpschroeder’s picture

If you're extending Drupal\Core\Form\FormBase or the similar Drupal\Core\Form\ConfigFormBase there is no entityTypeManager available. You have to override the create method and do your own injection.

When extending a FormBase:

  /**
   * An instance of the entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
    parent::__construct();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

Or when extending a ConfigFormBase:

  /**
   * An instance of the entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
    parent::__construct($config_factory);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('entity_type.manager')
    );
  }
ianwesty’s picture

Thanks, I used this when extending formBase. I had to change it slightly to work.

  /**
   * An instance of the entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')
    );
  }