diff --git a/core/includes/entity.api.php b/core/includes/entity.api.php index 332a66d..c4aafd1 100644 --- a/core/includes/entity.api.php +++ b/core/includes/entity.api.php @@ -413,25 +413,22 @@ function hook_entity_field_info_alter(&$info, $entity_type) { /** * Alters the default access behaviour for a given field. * - * This hook is invoked from Field::access() to let modules alter access to - * operations on fields. + * This hook is invoked from \Drupal\Core\Entity\Field\Type\Field::access() to + * let modules alter access to operations on fields. As soon as one hook + * implementation changes $access to FALSE the access() method will return FALSE + * and the remaining hook implementations will not be invoked. * * @param bool $access * Access flag reference that can be altered. TRUE if the operation is * allowed, and FALSE if the operation is denied. - * @param array $context - * Context array on the performed operation with the following keys: - * - access: The original access flag from the default implementation - * (boolean). - * - operation: Either "view" or "edit" (string). - * - field: The entity field object (\Drupal\Core\Entity\Field\Type\Field). - * - account: The user account to check access for - * (Drupal\user\Plugin\Core\Entity\User). + * @param string $operation + * The operation to be performed. Possible values: 'edit', 'view'. + * @param \Drupal\Core\Entity\Field\Type\Field $field + * The entity field object on which the operation is to be performed. + * @param \Drupal\user\Plugin\Core\Entity\User $account + * The user account to check. */ -function hook_entity_field_access_alter(&$access, array $context) { - $field = $context['field']; - $operation = $context['operation']; - $account = $context['account']; +function hook_entity_field_access_alter(&$access, $operation, $field, $account) { if ($field->getName() == 'field_of_interest' && $operation == 'edit') { $access = user_access('edit field of interest', $account); } diff --git a/core/lib/Drupal/Core/Entity/Field/Type/Field.php b/core/lib/Drupal/Core/Entity/Field/Type/Field.php index fcde772..8989d54 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/Field.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/Field.php @@ -296,14 +296,17 @@ public function access($operation = 'view', User $account = NULL) { } // Grant access per default. $access = TRUE; - $context = array( - 'access' => $access, - 'operation' => $operation, - 'field' => $this, - 'account' => $account, - ); - drupal_alter('entity_field_access', $access, $context); - + // We do not use drupal_alter() here because it only allows two context + // parameters which would make the signature of the hook very ugly. + foreach (module_implements('entity_field_access_alter') as $module) { + $function = $module . '_entity_field_access_alter'; + $function($access, $operation, $this, $account); + // If a hook implementation altered $access to FALSE we can return + // immediately. + if ($access === FALSE) { + return FALSE; + } + } return $access; } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php index 0130440..623fb44 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php @@ -15,7 +15,7 @@ class FieldAccessTest extends DrupalUnitTestBase { /** - * Modules to enable. + * Modules to load code from (no schema installation needed). * * @var array */ 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 7f47da7..56daa96 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -257,8 +257,7 @@ function entity_test_entity_test_insert($entity) { * * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess() */ -function entity_test_entity_field_access_alter(&$access, $context) { - $field = $context['field']; +function entity_test_entity_field_access_alter(&$access, $operation, $field, $account) { if ($field->getName() == 'field_test_text' && $field->value == 'no access value') { $access = FALSE; }