From 75f1692d2c8b6f2fb43c6b14e413735947426746 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Fri, 11 Dec 2015 23:58:56 +0000
Subject: [PATCH] Issue #2627852 by chx: hook_entity_create_access() is
 unusable

---
 core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php   | 1 +
 core/lib/Drupal/Core/Entity/entity.api.php                   | 3 ++-
 .../src/Tests/Entity/EntityAccessControlHandlerTest.php      | 5 +++++
 .../system/tests/modules/entity_test/entity_test.module      | 1 +
 4 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php b/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php
index 6d8d7fb3b752..27fda29eb1c0 100644
--- a/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php
+++ b/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php
@@ -206,6 +206,7 @@ public function resetCache() {
   public function createAccess($entity_bundle = NULL, AccountInterface $account = NULL, array $context = array(), $return_as_object = FALSE) {
     $account = $this->prepareUser($account);
     $context += array(
+      'entity_type_id' => $this->entityTypeId,
       'langcode' => LanguageInterface::LANGCODE_DEFAULT,
     );
 
diff --git a/core/lib/Drupal/Core/Entity/entity.api.php b/core/lib/Drupal/Core/Entity/entity.api.php
index be2a9038417d..d33b422be457 100644
--- a/core/lib/Drupal/Core/Entity/entity.api.php
+++ b/core/lib/Drupal/Core/Entity/entity.api.php
@@ -592,7 +592,8 @@ function hook_ENTITY_TYPE_access(\Drupal\Core\Entity\EntityInterface $entity, $o
  *   The account trying to access the entity.
  * @param array $context
  *   An associative array of additional context values. By default it contains
- *   language:
+ *   language and the entity type ID:
+ *   - entity_type_id - the entity type ID.
  *   - langcode - the current language code.
  * @param string $entity_bundle
  *   The entity bundle name.
diff --git a/core/modules/system/src/Tests/Entity/EntityAccessControlHandlerTest.php b/core/modules/system/src/Tests/Entity/EntityAccessControlHandlerTest.php
index ab73ee12ad32..38334766ab7a 100644
--- a/core/modules/system/src/Tests/Entity/EntityAccessControlHandlerTest.php
+++ b/core/modules/system/src/Tests/Entity/EntityAccessControlHandlerTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\system\Tests\Entity;
 
+use Drupal\Core\Language\LanguageInterface;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\Access\AccessibleInterface;
 use Drupal\Core\Entity\EntityAccessControlHandler;
@@ -155,6 +156,10 @@ public function testHooks() {
     // Test hook_entity_create_access() and hook_ENTITY_TYPE_create_access().
     $entity->access('create');
     $this->assertEqual($state->get('entity_test_entity_create_access'), TRUE);
+    $this->assertIdentical($state->get('entity_test_entity_create_access_context'), [
+      'entity_type_id' => 'entity_test',
+      'langcode' => LanguageInterface::LANGCODE_DEFAULT,
+    ]);
     $this->assertEqual($state->get('entity_test_entity_test_create_access'), TRUE);
 
     // Test hook_entity_access() and hook_ENTITY_TYPE_access().
diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module
index e172576f6908..59e44b6992b5 100644
--- a/core/modules/system/tests/modules/entity_test/entity_test.module
+++ b/core/modules/system/tests/modules/entity_test/entity_test.module
@@ -656,6 +656,7 @@ function entity_test_entity_test_access(EntityInterface $entity, $operation, Acc
  */
 function entity_test_entity_create_access(AccountInterface $account, $context, $entity_bundle) {
   \Drupal::state()->set('entity_test_entity_create_access', TRUE);
+  \Drupal::state()->set('entity_test_entity_create_access_context', $context);
 
   // No opinion.
   return AccessResult::neutral();
-- 
GitLab