Commit 629d4cbf authored by alexpott's avatar alexpott

Issue #2404739 by pfrenssen, Berdir, plach: language_entity_field_access()...

Issue #2404739 by pfrenssen, Berdir, plach: language_entity_field_access() doesn't work if $items isn't present
parent e1f3f990
...@@ -42,7 +42,6 @@ class TranslationTest extends FieldUnitTestBase { ...@@ -42,7 +42,6 @@ class TranslationTest extends FieldUnitTestBase {
*/ */
protected $entity_type = 'test_entity'; protected $entity_type = 'test_entity';
/** /**
* An array defining the field storage to use in this test. * An array defining the field storage to use in this test.
* *
...@@ -71,6 +70,9 @@ class TranslationTest extends FieldUnitTestBase { ...@@ -71,6 +70,9 @@ class TranslationTest extends FieldUnitTestBase {
*/ */
protected $field; protected $field;
/**
* {@inheritdoc}
*/
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
...@@ -187,4 +189,17 @@ function testTranslatableFieldSaveLoad() { ...@@ -187,4 +189,17 @@ function testTranslatableFieldSaveLoad() {
} }
} }
/**
* Tests field access.
*
* Regression test to verify that fieldAccess() can be called while only
* passing the required parameters.
*
* @see https://www.drupal.org/node/2404739
*/
public function testFieldAccess() {
$access_control_handler = \Drupal::entityManager()->getAccessControlHandler($this->entity_type);
$this->assertTrue($access_control_handler->fieldAccess('view', $this->field));
}
} }
...@@ -511,11 +511,18 @@ function language_field_info_alter(&$info) { ...@@ -511,11 +511,18 @@ function language_field_info_alter(&$info) {
* Implements hook_entity_field_access() * Implements hook_entity_field_access()
*/ */
function language_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) { function language_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
$langcode_key = $items->getEntity()->getEntityType()->getKey('langcode'); // Only allow access to a langcode field if the entity it is attached to is
if ($field_definition->getName() == $langcode_key) { // configured to have an alterable language.
$entity = $items->getEntity(); // Without items we can not decide whether or not to allow access.
$config = ContentLanguageSettings::loadByEntityTypeBundle($entity->getEntityTypeId(), $entity->bundle()); if ($items) {
return AccessResult::forbiddenIf(!$config->isLanguageAlterable()); // Check if we are dealing with a langcode field.
$langcode_key = $items->getEntity()->getEntityType()->getKey('langcode');
if ($field_definition->getName() == $langcode_key) {
// Grant access depending on whether the entity language can be altered.
$entity = $items->getEntity();
$config = ContentLanguageSettings::loadByEntityTypeBundle($entity->getEntityTypeId(), $entity->bundle());
return AccessResult::forbiddenIf(!$config->isLanguageAlterable());
}
} }
return AccessResult::neutral(); return AccessResult::neutral();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment