.../modules/editor/src/Tests/QuickEditIntegrationTest.php | 3 ++- core/modules/quickedit/quickedit.services.yml | 2 +- core/modules/quickedit/src/MetadataGenerator.php | 15 +++++++++++++-- .../modules/quickedit/src/Tests/MetadataGeneratorTest.php | 15 ++++++++++++--- .../tests/modules/src/MockEditEntityFieldAccessCheck.php | 3 ++- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/core/modules/editor/src/Tests/QuickEditIntegrationTest.php b/core/modules/editor/src/Tests/QuickEditIntegrationTest.php index 923ed68..cfcda09 100644 --- a/core/modules/editor/src/Tests/QuickEditIntegrationTest.php +++ b/core/modules/editor/src/Tests/QuickEditIntegrationTest.php @@ -10,6 +10,7 @@ use Drupal\Component\Serialization\Json; use Drupal\Core\EventSubscriber\AjaxResponseSubscriber; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Session\AnonymousUserSession; use Drupal\quickedit\EditorSelector; use Drupal\quickedit\MetadataGenerator; use Drupal\quickedit\Plugin\InPlaceEditorManager; @@ -160,7 +161,7 @@ public function testMetadata() { $this->editorManager = $this->container->get('plugin.manager.quickedit.editor'); $this->accessChecker = new MockEditEntityFieldAccessCheck(); $this->editorSelector = $this->container->get('quickedit.editor.selector'); - $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager); + $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager, new AnonymousUserSession()); // Create an entity with values for the field. $entity = entity_create('entity_test'); diff --git a/core/modules/quickedit/quickedit.services.yml b/core/modules/quickedit/quickedit.services.yml index 692ba2f..bc90dbd 100644 --- a/core/modules/quickedit/quickedit.services.yml +++ b/core/modules/quickedit/quickedit.services.yml @@ -11,4 +11,4 @@ services: arguments: ['@plugin.manager.quickedit.editor', '@plugin.manager.field.formatter'] quickedit.metadata.generator: class: Drupal\quickedit\MetadataGenerator - arguments: ['@access_check.quickedit.entity_field', '@quickedit.editor.selector', '@plugin.manager.quickedit.editor'] + arguments: ['@access_check.quickedit.entity_field', '@quickedit.editor.selector', '@plugin.manager.quickedit.editor', '@current_user'] diff --git a/core/modules/quickedit/src/MetadataGenerator.php b/core/modules/quickedit/src/MetadataGenerator.php index 1de5fc6..7566263 100644 --- a/core/modules/quickedit/src/MetadataGenerator.php +++ b/core/modules/quickedit/src/MetadataGenerator.php @@ -11,6 +11,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface; use Drupal\Core\Entity\Entity\EntityViewDisplay; @@ -41,6 +42,13 @@ class MetadataGenerator implements MetadataGeneratorInterface { protected $editorManager; /** + * The current user. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** * Constructs a new MetadataGenerator. * * @param \Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface $access_checker @@ -49,11 +57,14 @@ class MetadataGenerator implements MetadataGeneratorInterface { * An object that determines which editor to attach to a given field. * @param \Drupal\Component\Plugin\PluginManagerInterface * The manager for editor plugins. + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user. */ - public function __construct(EditEntityFieldAccessCheckInterface $access_checker, EditorSelectorInterface $editor_selector, PluginManagerInterface $editor_manager) { + public function __construct(EditEntityFieldAccessCheckInterface $access_checker, EditorSelectorInterface $editor_selector, PluginManagerInterface $editor_manager, AccountInterface $current_user) { $this->accessChecker = $access_checker; $this->editorSelector = $editor_selector; $this->editorManager = $editor_manager; + $this->currentUser = $current_user; } /** @@ -73,7 +84,7 @@ public function generateFieldMetadata(FieldItemListInterface $items, $view_mode) $field_name = $items->getFieldDefinition()->getName(); // Early-return if user does not have access. - $access = $this->accessChecker->accessEditEntityField($entity, $field_name); + $access = $this->accessChecker->accessEditEntityField($entity, $field_name, $this->currentUser); if (!$access) { return array('access' => FALSE); } diff --git a/core/modules/quickedit/src/Tests/MetadataGeneratorTest.php b/core/modules/quickedit/src/Tests/MetadataGeneratorTest.php index 9fd47bc..e3b3d76 100644 --- a/core/modules/quickedit/src/Tests/MetadataGeneratorTest.php +++ b/core/modules/quickedit/src/Tests/MetadataGeneratorTest.php @@ -8,6 +8,7 @@ namespace Drupal\quickedit\Tests; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Session\AnonymousUserSession; use Drupal\quickedit\EditorSelector; use Drupal\quickedit\MetadataGenerator; use Drupal\quickedit\Plugin\InPlaceEditorManager; @@ -47,6 +48,13 @@ class MetadataGeneratorTest extends QuickEditTestBase { protected $editorSelector; /** + * The mocked current user. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** * The access checker object to be used by the metadata generator object. * * @var \Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface @@ -59,7 +67,8 @@ protected function setUp() { $this->editorManager = $this->container->get('plugin.manager.quickedit.editor'); $this->accessChecker = new MockEditEntityFieldAccessCheck(); $this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter')); - $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager); + $this->currentUser = new AnonymousUserSession(); + $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager, $this->currentUser); } /** @@ -129,11 +138,11 @@ public function testEditorWithCustomMetadata() { $this->editorManager = $this->container->get('plugin.manager.quickedit.editor'); $this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter')); - $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager); + $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager, $this->currentUser); $this->editorManager = $this->container->get('plugin.manager.quickedit.editor'); $this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter')); - $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager); + $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager, $this->currentUser); // Create a rich text field. $field_name = 'field_rich'; diff --git a/core/modules/quickedit/tests/modules/src/MockEditEntityFieldAccessCheck.php b/core/modules/quickedit/tests/modules/src/MockEditEntityFieldAccessCheck.php index 83bddaf..4c6a1b7 100644 --- a/core/modules/quickedit/tests/modules/src/MockEditEntityFieldAccessCheck.php +++ b/core/modules/quickedit/tests/modules/src/MockEditEntityFieldAccessCheck.php @@ -8,6 +8,7 @@ namespace Drupal\quickedit_test; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface; /** @@ -18,7 +19,7 @@ class MockEditEntityFieldAccessCheck implements EditEntityFieldAccessCheckInterf /** * {@inheritdoc} */ - public function accessEditEntityField(EntityInterface $entity, $field_name) { + public function accessEditEntityField(EntityInterface $entity, $field_name, AccountInterface $account) { return TRUE; }