diff --git a/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php b/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php
index 4c8feb0..ef6f82e 100644
--- a/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php
+++ b/core/lib/Drupal/Component/Plugin/Discovery/DerivativeDiscoveryDecorator.php
@@ -155,7 +155,11 @@ protected function encodePluginId($base_plugin_id, $derivative_id) {
    *
    * @param string $base_plugin_id
    *   The base plugin id of the plugin.
+<<<<<<< HEAD
    * @param mixed $base_definition
+=======
+   * @param array|object $base_definition
+>>>>>>> applied patch
    *   The base plugin definition to build derivatives.
    *
    * @return \Drupal\Component\Plugin\Derivative\DerivativeInterface|null
diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php
index 5bfd687..b44c8b4 100644
--- a/core/lib/Drupal/Core/Entity/EntityManager.php
+++ b/core/lib/Drupal/Core/Entity/EntityManager.php
@@ -9,19 +9,15 @@
 
 use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
-use Drupal\Component\Plugin\PluginManagerBase;
 use Drupal\Component\Utility\NestedArray;
 use Drupal\Component\Utility\String;
 use Drupal\Core\Field\FieldDefinition;
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\Cache\CacheBackendInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
-use Drupal\Core\Language\LanguageManager;
 use Drupal\Core\Language\Language;
-use Drupal\Core\Plugin\Discovery\AlterDecorator;
-use Drupal\Core\Plugin\Discovery\CacheDecorator;
-use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
-use Drupal\Core\Plugin\Discovery\InfoHookDecorator;
+use Drupal\Core\Language\LanguageManagerInterface;
+use Drupal\Core\Plugin\DefaultPluginManager;
 use Drupal\Core\StringTranslation\TranslationInterface;
 use Drupal\Core\TypedData\TranslatableInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -40,7 +36,7 @@
  * @see \Drupal\Core\Entity\EntityTypeInterface
  * @see hook_entity_type_alter()
  */
-class EntityManager extends PluginManagerBase implements EntityManagerInterface {
+class EntityManager extends DefaultPluginManager implements EntityManagerInterface {
 
   /**
    * The injection container that should be passed into the controller factory.
@@ -57,27 +53,6 @@ class EntityManager extends PluginManagerBase implements EntityManagerInterface
   protected $controllers = array();
 
   /**
-   * The module handler.
-   *
-   * @var \Drupal\Core\Extension\ModuleHandlerInterface
-   */
-  protected $moduleHandler;
-
-  /**
-   * The cache backend to use.
-   *
-   * @var \Drupal\Core\Cache\CacheBackendInterface
-   */
-  protected $cache;
-
-  /**
-   * The language manager.
-   *
-   * @var \Drupal\Core\Language\LanguageManager
-   */
-  protected $languageManager;
-
-  /**
    * Static cache of base field definitions.
    *
    * @var array
@@ -92,15 +67,6 @@ class EntityManager extends PluginManagerBase implements EntityManagerInterface
   protected $fieldDefinitions;
 
   /**
-   * The root paths.
-   *
-   * @see self::__construct().
-   *
-   * @var \Traversable
-   */
-  protected $namespaces;
-
-  /**
    * The string translationManager.
    *
    * @var \Drupal\Core\StringTranslation\TranslationInterface
@@ -126,25 +92,18 @@ class EntityManager extends PluginManagerBase implements EntityManagerInterface
    *   The module handler.
    * @param \Drupal\Core\Cache\CacheBackendInterface $cache
    *   The cache backend to use.
-   * @param \Drupal\Core\Language\LanguageManager $language_manager
+   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
    *   The language manager.
    * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
    *   The string translationManager.
    */
-  public function __construct(\Traversable $namespaces, ContainerInterface $container, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManager $language_manager, TranslationInterface $translation_manager) {
-    // Allow the plugin definition to be altered by hook_entity_type_alter().
+  public function __construct(\Traversable $namespaces, ContainerInterface $container, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, TranslationInterface $translation_manager) {
+    parent::__construct('Entity', $namespaces, $module_handler, 'Drupal\Core\Entity\Annotation\EntityType');
+
+    $this->setCacheBackend($cache, $language_manager, 'entity_type:', array('entity_types' => TRUE));
+    $this->alterInfo('entity_type');
 
-    $this->moduleHandler = $module_handler;
-    $this->cache = $cache;
-    $this->languageManager = $language_manager;
-    $this->namespaces = $namespaces;
     $this->translationManager = $translation_manager;
-
-    $this->discovery = new AnnotatedClassDiscovery('Entity', $namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
-    $this->discovery = new InfoHookDecorator($this->discovery, 'entity_type_build');
-    $this->discovery = new AlterDecorator($this->discovery, 'entity_type');
-    $this->discovery = new CacheDecorator($this->discovery, 'entity_type:' . $this->languageManager->getCurrentLanguage()->id, 'cache', Cache::PERMANENT, array('entity_types' => TRUE));
-
     $this->container = $container;
   }
 
@@ -160,6 +119,31 @@ public function clearCachedDefinitions() {
   /**
    * {@inheritdoc}
    */
+  protected function findDefinitions() {
+    $definitions = $this->discovery->getDefinitions();
+    foreach ($this->moduleHandler->getImplementations('entity_type_build') as $module) {
+      $function = $module . '_' . 'entity_type_build';
+      $function($definitions);
+    }
+    foreach ($definitions as $plugin_id => $definition) {
+      $this->processDefinition($definition, $plugin_id);
+    }
+    if ($this->alterHook) {
+      $this->moduleHandler->alter($this->alterHook, $definitions);
+    }
+    // If this plugin was provided by a module that does not exist, remove the
+    // plugin definition.
+    foreach ($definitions as $plugin_id => $plugin_definition) {
+      if (!in_array($plugin_definition->getProvider(), array('Core', 'Component')) && !$this->moduleHandler->moduleExists($plugin_definition->getProvider())) {
+        unset($definitions[$plugin_id]);
+      }
+    }
+    return $definitions;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public function getDefinition($entity_type_id, $exception_on_invalid = FALSE) {
     if (($entity_type = parent::getDefinition($entity_type_id)) && class_exists($entity_type->getClass())) {
       return $entity_type;
@@ -299,13 +283,13 @@ public function getBaseFieldDefinitions($entity_type_id) {
     if (!isset($this->baseFieldDefinitions[$entity_type_id])) {
       // Not prepared, try to load from cache.
       $cid = 'entity_base_field_definitions:' . $entity_type_id . ':' . $this->languageManager->getCurrentLanguage()->id;
-      if ($cache = $this->cache->get($cid)) {
+      if ($cache = $this->cacheBackend->get($cid)) {
         $this->baseFieldDefinitions[$entity_type_id] = $cache->data;
       }
       else {
         // Rebuild the definitions and put it into the cache.
         $this->baseFieldDefinitions[$entity_type_id] = $this->buildBaseFieldDefinitions($entity_type_id);
-        $this->cache->set($cid, $this->baseFieldDefinitions[$entity_type_id], Cache::PERMANENT, array('entity_types' => TRUE, 'entity_field_info' => TRUE));
+        $this->cacheBackend->set($cid, $this->baseFieldDefinitions[$entity_type_id], Cache::PERMANENT, array('entity_types' => TRUE, 'entity_field_info' => TRUE));
        }
      }
     return $this->baseFieldDefinitions[$entity_type_id];
@@ -365,13 +349,13 @@ public function getFieldDefinitions($entity_type_id, $bundle) {
       $base_field_definitions = $this->getBaseFieldDefinitions($entity_type_id);
       // Not prepared, try to load from cache.
       $cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->id;
-      if ($cache = $this->cache->get($cid)) {
+      if ($cache = $this->cacheBackend->get($cid)) {
         $bundle_field_definitions = $cache->data;
       }
       else {
         // Rebuild the definitions and put it into the cache.
         $bundle_field_definitions = $this->buildBuildFieldDefinitions($entity_type_id, $bundle, $base_field_definitions);
-        $this->cache->set($cid, $bundle_field_definitions, Cache::PERMANENT, array('entity_types' => TRUE, 'entity_field_info' => TRUE));
+        $this->cacheBackend->set($cid, $bundle_field_definitions, Cache::PERMANENT, array('entity_types' => TRUE, 'entity_field_info' => TRUE));
       }
       // Field definitions consist of the bundle specific overrides and the
       // base fields, merge them together. Use array_replace() to replace base
@@ -445,7 +429,7 @@ public function getBundleInfo($entity_type) {
   public function getAllBundleInfo() {
     if (!isset($this->bundleInfo)) {
       $langcode = $this->languageManager->getCurrentLanguage()->id;
-      if ($cache = $this->cache->get("entity_bundle_info:$langcode")) {
+      if ($cache = $this->cacheBackend->get("entity_bundle_info:$langcode")) {
         $this->bundleInfo = $cache->data;
       }
       else {
@@ -457,7 +441,7 @@ public function getAllBundleInfo() {
           }
         }
         $this->moduleHandler->alter('entity_bundle_info', $this->bundleInfo);
-        $this->cache->set("entity_bundle_info:$langcode", $this->bundleInfo, Cache::PERMANENT, array('entity_types' => TRUE));
+        $this->cacheBackend->set("entity_bundle_info:$langcode", $this->bundleInfo, Cache::PERMANENT, array('entity_types' => TRUE));
       }
     }
 
diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
index 99abd89..341223a 100644
--- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
+++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
@@ -270,7 +270,7 @@ protected function findDefinitions() {
     // If this plugin was provided by a module that does not exist, remove the
     // plugin definition.
     foreach ($definitions as $plugin_id => $plugin_definition) {
-      if (isset($plugin_definition['provider']) && !in_array($plugin_definition['provider'], array('Core', 'Component')) && !$this->moduleHandler->moduleExists($plugin_definition['provider'])) {
+      if ((is_array($plugin_definition) || ($base_definition = (array) $plugin_definition)) && isset($plugin_definition['provider']) && !in_array($plugin_definition['provider'], array('Core', 'Component')) && !$this->moduleHandler->moduleExists($plugin_definition['provider'])) {
         unset($definitions[$plugin_id]);
       }
     }
diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
index 2f8c233..a74b755 100644
--- a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
+++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
@@ -80,13 +80,6 @@ class EntityManagerTest extends UnitTestCase {
   protected $translationManager;
 
   /**
-   * The form builder.
-   *
-   * @var \Drupal\Core\Form\FormBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
-   */
-  protected $formBuilder;
-
-  /**
    * {@inheritdoc}
    */
   public static function getInfo() {
@@ -108,23 +101,26 @@ protected function setUp() {
       ->method('getImplementations')
       ->with('entity_type_build')
       ->will($this->returnValue(array()));
+    $this->moduleHandler->expects($this->any())
+      ->method('moduleExists')
+      ->with('test_module')
+      ->will($this->returnValue(TRUE));
 
     $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
 
-    $this->languageManager = $this->getMockBuilder('Drupal\Core\Language\LanguageManager')
-      ->disableOriginalConstructor()
-      ->getMock();
+    $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
     $this->languageManager->expects($this->any())
       ->method('getCurrentLanguage')
       ->will($this->returnValue((object) array('id' => 'en')));
+    $this->languageManager->expects($this->any())
+      ->method('getLanguages')
+      ->will($this->returnValue(array('en' => (object) array('id' => 'en'))));
 
     $this->translationManager = $this->getStringTranslationStub();
 
-    $this->formBuilder = $this->getMock('Drupal\Core\Form\FormBuilderInterface');
-
     $this->container = $this->getContainerWithCacheBins($this->cache);
 
-    $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface');
+    $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
   }
 
   /**
@@ -140,6 +136,9 @@ protected function setUpEntityManager($definitions = array()) {
       $entity_type->expects($this->any())
         ->method('getClass')
         ->will($this->returnValue($class));
+      $entity_type->expects($this->any())
+        ->method('getProvider')
+        ->will($this->returnValue('test_module'));
       $definitions_map[] = array($entity_type_id, $entity_type);
     }
     $this->discovery->expects($this->any())
@@ -149,7 +148,7 @@ protected function setUpEntityManager($definitions = array()) {
       ->method('getDefinitions')
       ->will($this->returnValue($definitions));
 
-    $this->entityManager = new TestEntityManager(new \ArrayObject(), $this->container, $this->moduleHandler, $this->cache, $this->languageManager, $this->translationManager, $this->formBuilder);
+    $this->entityManager = new TestEntityManager(new \ArrayObject(), $this->container, $this->moduleHandler, $this->cache, $this->languageManager, $this->translationManager);
     $this->entityManager->setDiscovery($this->discovery);
   }
 
@@ -161,8 +160,9 @@ protected function setUpEntityManager($definitions = array()) {
    */
   public function testClearCachedDefinitions() {
     $this->setUpEntityManager();
-    $this->discovery->expects($this->once())
-      ->method('clearCachedDefinitions');
+    $this->cache->expects($this->once())
+      ->method('deleteTags')
+      ->with(array('entity_types' => TRUE));
 
     $this->entityManager->clearCachedDefinitions();
   }
@@ -489,9 +489,15 @@ public function testGetBaseFieldDefinitionsWithCaching() {
       ->method('get')
       ->with('entity_base_field_definitions:test_entity_type:en', FALSE)
       ->will($this->returnValue(FALSE));
-    $this->cache->expects($this->once())
-      ->method('set');
+    $this->cache->expects($this->at(1))
+      ->method('get')
+      ->with('entity_type::en', FALSE)
+      ->will($this->returnValue(FALSE));
     $this->cache->expects($this->at(2))
+      ->method('set');
+    $this->cache->expects($this->at(3))
+      ->method('set');
+    $this->cache->expects($this->at(4))
       ->method('get')
       ->with('entity_base_field_definitions:test_entity_type:en', FALSE)
       ->will($this->returnValue((object) array('data' => $expected)));
@@ -521,12 +527,18 @@ public function testGetFieldDefinitionsWithCaching() {
       ->with('entity_bundle_field_definitions:test_entity_type:test_bundle:en', FALSE)
       ->will($this->returnValue(FALSE));
     $this->cache->expects($this->at(2))
-      ->method('set');
+      ->method('get')
+      ->with('entity_type::en', FALSE)
+      ->will($this->returnValue(FALSE));
     $this->cache->expects($this->at(3))
+      ->method('set');
+    $this->cache->expects($this->at(4))
+      ->method('set');
+    $this->cache->expects($this->at(5))
       ->method('get')
       ->with('entity_base_field_definitions:test_entity_type:en', FALSE)
       ->will($this->returnValue((object) array('data' => $expected)));
-    $this->cache->expects($this->at(4))
+    $this->cache->expects($this->at(6))
       ->method('get')
       ->with('entity_bundle_field_definitions:test_entity_type:test_bundle:en', FALSE)
       ->will($this->returnValue((object) array('data' => $expected)));
@@ -679,14 +691,25 @@ public function testGetAllBundleInfo() {
       'apple' => $apple,
       'banana' => $banana,
     ));
+    $this->cache->expects($this->at(0))
+      ->method('get')
+      ->with("entity_bundle_info:en", FALSE)
+      ->will($this->returnValue(FALSE));
     $this->cache->expects($this->at(1))
       ->method('get')
+      ->with("entity_type::en", FALSE)
       ->will($this->returnValue(FALSE));
     $this->cache->expects($this->at(2))
+      ->method('set');
+    $this->cache->expects($this->at(3))
+      ->method('set');
+    $this->cache->expects($this->at(4))
+      ->method('deleteTags')
+      ->with(array('entity_types' => TRUE));
+    $this->cache->expects($this->at(5))
       ->method('get')
+      ->with("entity_bundle_info:en", FALSE)
       ->will($this->returnValue((object) array('data' => 'cached data')));
-    $this->cache->expects($this->once())
-      ->method('set');
 
     $expected = array(
       'apple' => array(
