From 85ca08146c87ad92471c3660559153bdfbe3cb7c Mon Sep 17 00:00:00 2001
From: Nejc <nejc.koporec@agiledrop.com>
Date: Tue, 27 Feb 2018 11:23:51 +0100
Subject: [PATCH] Replaced deprecated methods with recommended ones

---
 context.services.yml   |  2 +-
 src/ContextManager.php | 43 +++++++++++--------------------------------
 2 files changed, 12 insertions(+), 33 deletions(-)

diff --git a/context.services.yml b/context.services.yml
index 67aa049..918a1e8 100644
--- a/context.services.yml
+++ b/context.services.yml
@@ -1,7 +1,7 @@
 services:
   context.manager:
     class: Drupal\context\ContextManager
-    arguments: ['@entity.query', '@entity.manager', '@context.repository', '@context.handler', '@entity.form_builder', '@theme.manager']
+    arguments: ['@entity_type.manager', '@context.repository', '@context.handler', '@entity.form_builder', '@theme.manager']
   plugin.manager.context_reaction:
     class: Drupal\context\ContextReactionManager
     parent: default_plugin_manager
diff --git a/src/ContextManager.php b/src/ContextManager.php
index 42afff6..d035f39 100644
--- a/src/ContextManager.php
+++ b/src/ContextManager.php
@@ -4,8 +4,7 @@ namespace Drupal\context;
 
 use Drupal\context\Entity\Context;
 use Drupal\context\Plugin\ContextReaction\Blocks;
-use Drupal\Core\Entity\Query\QueryFactory;
-use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\EntityTypeManager;
 use Drupal\Core\Entity\EntityFormBuilderInterface;
 use Drupal\Core\Plugin\ContextAwarePluginInterface;
 use Drupal\Core\Condition\ConditionPluginCollection;
@@ -26,14 +25,9 @@ class ContextManager {
   use StringTranslationTrait;
 
   /**
-   * @var \Drupal\Core\Entity\Query\QueryFactory
+   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
    */
-  protected $entityQuery;
-
-  /**
-   * @var \Drupal\Core\Entity\EntityManagerInterface
-   */
-  protected $entityManager;
+  protected $entityTypeManager;
 
   /**
    * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
@@ -72,34 +66,26 @@ class ContextManager {
   /**
    * Construct.
    *
-   * @param QueryFactory $entityQuery
-   *   The Drupal entity query service.
-   *
-   * @param EntityManagerInterface $entityManager
+   * @param EntityTypeManager $entityTypeManager
    *   The Drupal entity manager service.
-   *
    * @param ContextRepositoryInterface $contextRepository
    *   The drupal context repository service.
-   *
    * @param ContextHandlerInterface $contextHandler
    *   The Drupal context handler service.
-   *
    * @param ThemeManagerInterface $themeManager
    *   The Drupal theme manager service.
-   *
-   * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entityFormBuilder
+   * @param EntityFormBuilderInterface $entityFormBuilder
+   *   The Drupal EntityFormBuilder service.
    */
-  function __construct(
-    QueryFactory $entityQuery,
-    EntityManagerInterface $entityManager,
+  public function __construct(
+    EntityTypeManager $entityTypeManager,
     ContextRepositoryInterface $contextRepository,
     ContextHandlerInterface $contextHandler,
     EntityFormBuilderInterface $entityFormBuilder,
     ThemeManagerInterface $themeManager
   )
   {
-    $this->entityQuery = $entityQuery;
-    $this->entityManager = $entityManager;
+    $this->entityTypeManager = $entityTypeManager;
     $this->contextRepository = $contextRepository;
     $this->contextHandler = $contextHandler;
     $this->entityFormBuilder = $entityFormBuilder;
@@ -112,13 +98,8 @@ class ContextManager {
    * @return Context[]
    */
   public function getContexts() {
-    $contextIds = $this->entityQuery
-      ->get('context')
-      ->execute();
 
-    $contexts = $this->entityManager
-      ->getStorage('context')
-      ->loadMultiple($contextIds);
+    $contexts = $this->entityTypeManager->getStorage('context')->loadByProperties();
 
     // Sort the contexts by their weight.
     uasort($contexts, [$this, 'sortContextsByWeight']);
@@ -160,9 +141,7 @@ class ContextManager {
    * @return bool
    */
   public function contextExists($name) {
-    $entity = $this->entityQuery->get('context')
-      ->condition('name', $name)
-      ->execute();
+    $entity = $this->entityTypeManager->getStorage('context')->loadByProperties(array('name' => $name));
 
     return (bool) $entity;
   }
-- 
2.15.1

