Loading core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php +4 −1 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Field\EntityReferenceFieldItemList; /** * The media library opener for field widgets. Loading Loading @@ -95,7 +96,9 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account) $items = $entity->get($field_name); $field_definition = $items->getFieldDefinition(); if ($field_definition->getType() !== 'entity_reference') { // Check that the field is an entity reference, or subclass of it, since we // need to check the target_type setting. if (!$items instanceof EntityReferenceFieldItemList) { throw new \LogicException('Expected the media library to be opened by an entity reference field.'); } if ($field_definition->getFieldStorageDefinition()->getSetting('target_type') !== 'media') { Loading core/modules/media_library/tests/modules/media_library_test/config/schema/media_library_test.yml 0 → 100644 +7 −0 Original line number Diff line number Diff line field.storage_settings.entity_reference_subclass: type: base_entity_reference_field_settings label: 'Entity reference subclass field storage settings' field.field_settings.entity_reference_subclass: type: field.field_settings.entity_reference label: 'Entity reference subclass field settings' core/modules/media_library/tests/modules/media_library_test/media_library_test.module +7 −0 Original line number Diff line number Diff line Loading @@ -39,3 +39,10 @@ function media_library_test_entity_type_alter(array &$entity_types) { $entity_types['node']->setFormClass('edit', TestNodeFormOverride::class); } } /** * Implements hook_field_widget_info_alter(). */ function media_library_test_field_widget_info_alter(array &$info) { $info['media_library_widget']['field_types'][] = 'entity_reference_subclass'; } core/modules/media_library/tests/modules/media_library_test/src/Plugin/Field/FieldType/EntityReferenceItemSubclass.php 0 → 100644 +21 −0 Original line number Diff line number Diff line <?php namespace Drupal\media_library_test\Plugin\Field\FieldType; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; /** * Plugin implementation of the 'entity_reference_subclass' field type. * * @FieldType( * id = "entity_reference_subclass", * label = @Translation("Entity reference subclass"), * description = @Translation("An entity field containing an entity reference."), * category = @Translation("Reference"), * default_widget = "entity_reference_autocomplete", * default_formatter = "entity_reference_label", * list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList" * ) */ class EntityReferenceItemSubclass extends EntityReferenceItem { } core/modules/media_library/tests/src/Kernel/MediaLibraryAccessTest.php +20 −2 Original line number Diff line number Diff line Loading @@ -256,12 +256,30 @@ public function testFieldWidgetEntityEditAccess() { $this->assertAccess($access_result, TRUE, NULL, Views::getView('media_library')->storage->getCacheTags(), ['url.query_args', 'user.permissions']); } /** * Data provider for ::testFieldWidgetEntityFieldAccess(). * * @return array[] * Sets of arguments to pass to the test method. */ public function providerFieldWidgetEntityFieldAccess(): array { return [ ['entity_reference'], ['entity_reference_subclass'], ]; } /** * Tests that the field widget opener respects entity field-level access. * * @param string $field_type * The field type. * * @dataProvider providerFieldWidgetEntityFieldAccess */ public function testFieldWidgetEntityFieldAccess() { public function testFieldWidgetEntityFieldAccess(string $field_type) { $field_storage = FieldStorageConfig::create([ 'type' => 'entity_reference', 'type' => $field_type, 'entity_type' => 'entity_test', // The media_library_test module will deny access to this field. // @see media_library_test_entity_field_access() Loading Loading
core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php +4 −1 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Field\EntityReferenceFieldItemList; /** * The media library opener for field widgets. Loading Loading @@ -95,7 +96,9 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account) $items = $entity->get($field_name); $field_definition = $items->getFieldDefinition(); if ($field_definition->getType() !== 'entity_reference') { // Check that the field is an entity reference, or subclass of it, since we // need to check the target_type setting. if (!$items instanceof EntityReferenceFieldItemList) { throw new \LogicException('Expected the media library to be opened by an entity reference field.'); } if ($field_definition->getFieldStorageDefinition()->getSetting('target_type') !== 'media') { Loading
core/modules/media_library/tests/modules/media_library_test/config/schema/media_library_test.yml 0 → 100644 +7 −0 Original line number Diff line number Diff line field.storage_settings.entity_reference_subclass: type: base_entity_reference_field_settings label: 'Entity reference subclass field storage settings' field.field_settings.entity_reference_subclass: type: field.field_settings.entity_reference label: 'Entity reference subclass field settings'
core/modules/media_library/tests/modules/media_library_test/media_library_test.module +7 −0 Original line number Diff line number Diff line Loading @@ -39,3 +39,10 @@ function media_library_test_entity_type_alter(array &$entity_types) { $entity_types['node']->setFormClass('edit', TestNodeFormOverride::class); } } /** * Implements hook_field_widget_info_alter(). */ function media_library_test_field_widget_info_alter(array &$info) { $info['media_library_widget']['field_types'][] = 'entity_reference_subclass'; }
core/modules/media_library/tests/modules/media_library_test/src/Plugin/Field/FieldType/EntityReferenceItemSubclass.php 0 → 100644 +21 −0 Original line number Diff line number Diff line <?php namespace Drupal\media_library_test\Plugin\Field\FieldType; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; /** * Plugin implementation of the 'entity_reference_subclass' field type. * * @FieldType( * id = "entity_reference_subclass", * label = @Translation("Entity reference subclass"), * description = @Translation("An entity field containing an entity reference."), * category = @Translation("Reference"), * default_widget = "entity_reference_autocomplete", * default_formatter = "entity_reference_label", * list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList" * ) */ class EntityReferenceItemSubclass extends EntityReferenceItem { }
core/modules/media_library/tests/src/Kernel/MediaLibraryAccessTest.php +20 −2 Original line number Diff line number Diff line Loading @@ -256,12 +256,30 @@ public function testFieldWidgetEntityEditAccess() { $this->assertAccess($access_result, TRUE, NULL, Views::getView('media_library')->storage->getCacheTags(), ['url.query_args', 'user.permissions']); } /** * Data provider for ::testFieldWidgetEntityFieldAccess(). * * @return array[] * Sets of arguments to pass to the test method. */ public function providerFieldWidgetEntityFieldAccess(): array { return [ ['entity_reference'], ['entity_reference_subclass'], ]; } /** * Tests that the field widget opener respects entity field-level access. * * @param string $field_type * The field type. * * @dataProvider providerFieldWidgetEntityFieldAccess */ public function testFieldWidgetEntityFieldAccess() { public function testFieldWidgetEntityFieldAccess(string $field_type) { $field_storage = FieldStorageConfig::create([ 'type' => 'entity_reference', 'type' => $field_type, 'entity_type' => 'entity_test', // The media_library_test module will deny access to this field. // @see media_library_test_entity_field_access() Loading