diff --git a/core/lib/Drupal/Core/Entity/EntityAccessController.php b/core/lib/Drupal/Core/Entity/EntityAccessController.php index a2441c1..62fbf58 100644 --- a/core/lib/Drupal/Core/Entity/EntityAccessController.php +++ b/core/lib/Drupal/Core/Entity/EntityAccessController.php @@ -247,7 +247,7 @@ protected function prepareUser(AccountInterface $account = NULL) { } /** - * @todo add to interface. + * {@inheritdoc} */ public function fieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account = NULL, FieldInterface $field = NULL) { global $user; diff --git a/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php index c0296dd..7c31ddc 100644 --- a/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Entity; +use Drupal\Core\Entity\Field\FieldDefinitionInterface; +use Drupal\Core\Entity\Field\FieldInterface; use Drupal\Core\Language\Language; use Drupal\Core\Session\AccountInterface; @@ -58,4 +60,22 @@ public function createAccess($entity_bundle = NULL, AccountInterface $account = */ public function resetCache(); + /** + * Checks access to an operation on a given entity field. + * + * @param string $operation + * The operation access should be checked for. + * Usually one of "view" or "edit". + * @param \Drupal\Core\Entity\Field\FieldDefinitionInterface $field_definition + * The field definition. + * @param \Drupal\Core\Session\AccountInterface $account + * (optional) The user session for which to check access, or NULL to check + * access for the current user. Defaults to NULL. + * @param \Drupal\Core\Entity\Field\FieldInterface $field + * (optional) The field values for which to check access, or NULL if access + * is checked for the field definition, without any specific value + * available. Defaults to NULL. + */ + public function fieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account = NULL, FieldInterface $field = NULL); + } diff --git a/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php b/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php index 9559be7..0c118fb 100644 --- a/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php +++ b/core/modules/edit/lib/Drupal/edit/Access/EditEntityFieldAccessCheck.php @@ -42,7 +42,7 @@ public function access(Route $route, Request $request) { * {@inheritdoc} */ public function accessEditEntityField(EntityInterface $entity, $field_name) { - return $entity->access('update') && ($field = field_info_field($field_name)) && field_access('edit', $field, $entity->entityType(), $entity); + return $entity->access('update') && $entity->get($field_name)->access('edit'); } /** diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceController.php b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceController.php index b6099f1..a70fa12 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceController.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceController.php @@ -12,6 +12,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Drupal\Core\Controller\ControllerInterface; +use Drupal\Core\Entity\EntityManager; /** * Defines route controller for entity reference. @@ -26,13 +27,23 @@ class EntityReferenceController implements ControllerInterface { protected $entityReferenceAutocomplete; /** + * The entity manager. + * + * @var \Drupal\Core\Entity\EntityManager + */ + protected $entityManager; + + /** * Constructs a EntityReferenceController object. * * @param \Drupal\entity_reference\EntityReferenceAutocomplete $entity_reference_autcompletion - * The autocompletion helper for entity references + * The autocompletion helper for entity references. + * @param \Drupal\Core\Entity\EntityManager ĂȘntity_manager + * The entity manager. */ - public function __construct(EntityReferenceAutocomplete $entity_reference_autcompletion) { + public function __construct(EntityReferenceAutocomplete $entity_reference_autcompletion, EntityManager $entity_manager) { $this->entityReferenceAutocomplete = $entity_reference_autcompletion; + $this->entityManager = $entity_manager; } /** @@ -40,7 +51,8 @@ public function __construct(EntityReferenceAutocomplete $entity_reference_autcom */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity_reference.autocomplete') + $container->get('entity_reference.autocomplete'), + $container->get('entity.manager') ); } @@ -77,7 +89,8 @@ public function handleAutocomplete(Request $request, $type, $field_name, $entity throw new AccessDeniedHttpException(); } - if ($field['type'] != 'entity_reference' || !field_access('edit', $field, $entity_type)) { + $access_controller = $this->entityManager->getAccessController($entity_type); + if ($field['type'] != 'entity_reference' || !$access_controller->fieldAccess('edit', $instance)) { throw new AccessDeniedHttpException(); } diff --git a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php index a487a7a..ab81733 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php @@ -9,6 +9,7 @@ use Drupal\Core\Language\Language; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityManager; use Drupal\field\Plugin\Type\Formatter\FormatterPluginManager; use Drupal\views\ViewExecutable; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -69,6 +70,13 @@ class Field extends FieldPluginBase { protected $formatterOptions; /** + * The entity manager. + * + * @var \Drupal\Core\Entity\EntityManager + */ + protected $entityManager; + + /** * The field formatter plugin manager. * * @var \Drupal\field\Plugin\Type\Formatter\FormatterPluginManager @@ -84,12 +92,15 @@ class Field extends FieldPluginBase { * The plugin_id for the plugin instance. * @param array $plugin_definition * The plugin implementation definition. + * @param \Drupal\Core\Entity\EntityManager $entity_manager + * The field formatter plugin manager. * @param \Drupal\field\Plugin\Type\Formatter\FormatterPluginManager $formatter_plugin_manager * The field formatter plugin manager. */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, FormatterPluginManager $formatter_plugin_manager) { + public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManager $entity_manager, FormatterPluginManager $formatter_plugin_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->entityManager = $entity_manager; $this->formatterPluginManager = $formatter_plugin_manager; } @@ -101,6 +112,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, + $container->get('entity.manager'), $container->get('plugin.manager.field.formatter') ); } @@ -145,7 +157,8 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o */ public function access() { $base_table = $this->get_base_table(); - return field_access('view', $this->field_info, $this->definition['entity_tables'][$base_table]); + $access_controller = $this->entityManager->getAccessController($this->definition['entity_tables'][$base_table]); + return $access_controller->fieldAccess('view', $this->field_info); } /** diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php index 0f86ac5..d12a7a7 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php @@ -80,7 +80,7 @@ function setUp() { } /** - * Test that hook_field_access() is called. + * Test that hook_entity_field_access() is called. */ function testFieldAccess() { @@ -89,7 +89,7 @@ function testFieldAccess() { $this->assertText($this->test_view_field_value); // Assert the text is not visible for anonymous users. - // The field_test module implements hook_field_access() which will + // The field_test module implements hook_entity_field_access() which will // specifically target the 'test_view_field' field. $this->drupalLogout(); $this->drupalGet('node/' . $this->node->id()); diff --git a/core/modules/field/lib/Drupal/field/Tests/FormTest.php b/core/modules/field/lib/Drupal/field/Tests/FormTest.php index b263859..3cf77c5 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FormTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FormTest.php @@ -499,7 +499,8 @@ function testFieldFormAccess() { ->setComponent($field_name) ->save(); - // Create a field with no edit access - see field_test_field_access(). + // Create a field with no edit access. See + // field_test_entity_field_access(). $field_no_access = array( 'field_name' => 'field_no_edit_access', 'type' => 'test_field', diff --git a/core/modules/file/file.api.php b/core/modules/file/file.api.php index 2624623..eaf8ba4 100644 --- a/core/modules/file/file.api.php +++ b/core/modules/file/file.api.php @@ -216,7 +216,7 @@ function hook_file_delete(Drupal\file\FileInterface $file) { * that denial may be overridden by another entity controller, making this * grant permissive rather than restrictive. * - * @see hook_field_access(). + * @see hook_entity_field_access(). */ function hook_file_download_access($field, Drupal\Core\Entity\EntityInterface $entity, Drupal\file\FileInterface $file) { if ($entity->entityType() == 'node') { diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 3bf9f7b..7d7f0f6 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -652,7 +652,7 @@ function file_file_download($uri, $field_type = 'file') { foreach ($entities as $entity) { $field = field_info_field($field_name); // Check if access to this field is not disallowed. - if (!field_access('view', $field, $entity_type, $entity)) { + if (!$entity->get($field_name)->access('view')) { $denied = TRUE; continue; } diff --git a/core/modules/file/lib/Drupal/file/Tests/FilePrivateTest.php b/core/modules/file/lib/Drupal/file/Tests/FilePrivateTest.php index cfeee72..a63d692 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FilePrivateTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FilePrivateTest.php @@ -43,7 +43,8 @@ function testPrivateFile() { $field_name = strtolower($this->randomName()); $this->createFileField($field_name, $type_name, array('uri_scheme' => 'private')); - // Create a field with no view access - see field_test_field_access(). + // Create a field with no view access. See + // field_test_entity_field_access(). $no_access_field_name = 'field_no_view_access'; $this->createFileField($no_access_field_name, $type_name, array('uri_scheme' => 'private'));