update drupal
This commit is contained in:
@@ -84,11 +84,21 @@ function media_library_help($route_name, RouteMatchInterface $route_match) {
|
||||
* Implements hook_media_source_info_alter().
|
||||
*/
|
||||
function media_library_media_source_info_alter(array &$sources) {
|
||||
$sources['audio_file']['forms']['media_library_add'] = FileUploadForm::class;
|
||||
$sources['file']['forms']['media_library_add'] = FileUploadForm::class;
|
||||
$sources['image']['forms']['media_library_add'] = FileUploadForm::class;
|
||||
$sources['video_file']['forms']['media_library_add'] = FileUploadForm::class;
|
||||
$sources['oembed:video']['forms']['media_library_add'] = OEmbedForm::class;
|
||||
if (empty($sources['audio_file']['forms']['media_library_add'])) {
|
||||
$sources['audio_file']['forms']['media_library_add'] = FileUploadForm::class;
|
||||
}
|
||||
if (empty($sources['file']['forms']['media_library_add'])) {
|
||||
$sources['file']['forms']['media_library_add'] = FileUploadForm::class;
|
||||
}
|
||||
if (empty($sources['image']['forms']['media_library_add'])) {
|
||||
$sources['image']['forms']['media_library_add'] = FileUploadForm::class;
|
||||
}
|
||||
if (empty($sources['video_file']['forms']['media_library_add'])) {
|
||||
$sources['video_file']['forms']['media_library_add'] = FileUploadForm::class;
|
||||
}
|
||||
if (empty($sources['oembed:video']['forms']['media_library_add'])) {
|
||||
$sources['oembed:video']['forms']['media_library_add'] = OEmbedForm::class;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -402,8 +412,7 @@ function _media_library_configure_form_display(MediaTypeInterface $type) {
|
||||
$display->removeComponent($name);
|
||||
}
|
||||
// Expose the name field when it is not mapped.
|
||||
$field_map = $type->getFieldMap();
|
||||
if (empty($field_map['name'])) {
|
||||
if (!in_array('name', $type->getFieldMap(), TRUE)) {
|
||||
$display->setComponent('name', [
|
||||
'type' => 'string_textfield',
|
||||
'settings' => [
|
||||
|
||||
@@ -69,7 +69,11 @@ class MediaLibraryFieldWidgetOpener implements MediaLibraryOpenerInterface {
|
||||
$storage = $this->entityTypeManager->getStorage($entity_type_id);
|
||||
$access_handler = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
|
||||
|
||||
if ($parameters['entity_id']) {
|
||||
if (!empty($parameters['revision_id'])) {
|
||||
$entity = $storage->loadRevision($parameters['revision_id']);
|
||||
$entity_access = $access_handler->access($entity, 'update', $account, TRUE);
|
||||
}
|
||||
elseif ($parameters['entity_id']) {
|
||||
$entity = $storage->load($parameters['entity_id']);
|
||||
$entity_access = $access_handler->access($entity, 'update', $account, TRUE);
|
||||
}
|
||||
|
||||
@@ -473,6 +473,10 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
|
||||
// tamper-proof hash in a consistent way.
|
||||
if (!$entity->isNew()) {
|
||||
$opener_parameters['entity_id'] = (string) $entity->id();
|
||||
|
||||
if ($entity->getEntityType()->isRevisionable()) {
|
||||
$opener_parameters['revision_id'] = (string) $entity->getRevisionId();
|
||||
}
|
||||
}
|
||||
$state = MediaLibraryState::create('media_library.opener.field_widget', $allowed_media_type_ids, $selected_type_id, $remaining, $opener_parameters);
|
||||
|
||||
@@ -508,8 +512,19 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
|
||||
// JavaScript by adding the 'data-disabled-focus' attribute.
|
||||
// @see Drupal.behaviors.MediaLibraryWidgetDisableButton
|
||||
if (!$cardinality_unlimited && $remaining === 0) {
|
||||
$element['open_button']['#attributes']['data-disabled-focus'] = 'true';
|
||||
$element['open_button']['#attributes']['class'][] = 'visually-hidden';
|
||||
$triggering_element = $form_state->getTriggeringElement();
|
||||
if ($triggering_element && ($trigger_parents = $triggering_element['#array_parents']) && end($trigger_parents) === 'media_library_update_widget') {
|
||||
// The widget is being rebuilt from a selection change.
|
||||
$element['open_button']['#attributes']['data-disabled-focus'] = 'true';
|
||||
$element['open_button']['#attributes']['class'][] = 'visually-hidden';
|
||||
}
|
||||
else {
|
||||
// The widget is being built without a selection change, so we can just
|
||||
// set the item to disabled now, there is no need to set the focus
|
||||
// first.
|
||||
$element['open_button']['#disabled'] = TRUE;
|
||||
$element['open_button']['#attributes']['class'][] = 'visually-hidden';
|
||||
}
|
||||
}
|
||||
|
||||
// This hidden field and button are used to add new items to the widget.
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
name: 'Media Library Form Overwrite test'
|
||||
type: module
|
||||
description: 'Test module for Media Library.'
|
||||
package: Testing
|
||||
dependencies:
|
||||
- drupal:image
|
||||
- drupal:media_library
|
||||
- drupal:menu_ui
|
||||
- drupal:node
|
||||
- drupal:path
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains.
|
||||
*/
|
||||
|
||||
use Drupal\media_library_form_overwrite_test\Form\TestAddForm;
|
||||
|
||||
function media_library_form_overwrite_test_media_source_info_alter(array &$sources) {
|
||||
$sources['image']['forms']['media_library_add'] = TestAddForm::class;
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\media_library_form_overwrite_test\Form;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\media_library\Form\AddFormBase;
|
||||
|
||||
/**
|
||||
* Test add form.
|
||||
*/
|
||||
class TestAddForm extends AddFormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function buildInputElement(array $form, FormStateInterface $form_state) {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'test_add_form';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -162,6 +162,22 @@ class MediaLibraryDisplayModeTest extends BrowserTestBase {
|
||||
$this->assertFormDisplay($type_eight_id, FALSE, TRUE);
|
||||
$this->assertViewDisplay($type_eight_id, 'medium');
|
||||
|
||||
// Create an oEmbed media type with a mapped name field in the UI.
|
||||
$type_id = 'pinto_bean';
|
||||
$edit = [
|
||||
'label' => $type_id,
|
||||
'id' => $type_id,
|
||||
'source' => 'oembed:video',
|
||||
];
|
||||
$this->drupalPostForm('admin/structure/media/add', $edit, 'Save');
|
||||
$edit = [
|
||||
'field_map[title]' => 'name',
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertSession()->pageTextContains("Media Library form and view displays have been created for the $type_id media type.");
|
||||
$this->assertFormDisplay($type_id, FALSE, FALSE);
|
||||
$this->assertViewDisplay($type_id, 'medium');
|
||||
|
||||
// Delete a form and view display.
|
||||
EntityFormDisplay::load('media.type_one.media_library')->delete();
|
||||
EntityViewDisplay::load('media.type_one.media_library')->delete();
|
||||
|
||||
+33
-1
@@ -14,6 +14,13 @@ class EntityReferenceWidgetTest extends MediaLibraryTestBase {
|
||||
*/
|
||||
protected static $modules = ['field_ui'];
|
||||
|
||||
/**
|
||||
* Test media items.
|
||||
*
|
||||
* @var \Drupal\media\MediaInterface[]
|
||||
*/
|
||||
protected $mediaItems = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -21,7 +28,7 @@ class EntityReferenceWidgetTest extends MediaLibraryTestBase {
|
||||
parent::setUp();
|
||||
|
||||
// Create a few example media items for use in selection.
|
||||
$this->createMediaItems([
|
||||
$this->mediaItems = $this->createMediaItems([
|
||||
'type_one' => [
|
||||
'Horse',
|
||||
'Bear',
|
||||
@@ -48,6 +55,31 @@ class EntityReferenceWidgetTest extends MediaLibraryTestBase {
|
||||
$this->drupalLogin($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that disabled media items don't capture focus on page load.
|
||||
*/
|
||||
public function testFocusNotAppliedWithoutSelectionChange() {
|
||||
// Create a node with the maximum number of values for the field_twin_media
|
||||
// field.
|
||||
$node = $this->drupalCreateNode([
|
||||
'type' => 'basic_page',
|
||||
'field_twin_media' => [
|
||||
$this->mediaItems['Horse'],
|
||||
$this->mediaItems['Bear'],
|
||||
],
|
||||
]);
|
||||
$this->drupalGet($node->toUrl('edit-form'));
|
||||
$open_button = $this->assertElementExistsAfterWait('css', '.js-media-library-open-button[name^="field_twin_media"]');
|
||||
// The open button should be disabled, but not have the
|
||||
// 'data-disabled-focus' attribute.
|
||||
$this->assertFalse($open_button->hasAttribute('data-disabled-focus'));
|
||||
$this->assertTrue($open_button->hasAttribute('disabled'));
|
||||
// The button should be disabled.
|
||||
$this->assertJsCondition('jQuery("#field_twin_media-media-library-wrapper .js-media-library-open-button").is(":disabled")');
|
||||
// The button should not have focus.
|
||||
$this->assertJsCondition('jQuery("#field_twin_media-media-library-wrapper .js-media-library-open-button").not(":focus")');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the Media library's widget works as expected.
|
||||
*/
|
||||
|
||||
@@ -25,8 +25,12 @@ abstract class MediaLibraryTestBase extends WebDriverTestBase {
|
||||
*
|
||||
* @param array $media_items
|
||||
* A nested array of media item names keyed by media type.
|
||||
*
|
||||
* @return \Drupal\media\MediaInterface[]
|
||||
* An array of media entities keyed by the names passed in.
|
||||
*/
|
||||
protected function createMediaItems(array $media_items) {
|
||||
$created_items = [];
|
||||
$time = time();
|
||||
foreach ($media_items as $type => $names) {
|
||||
foreach ($names as $name) {
|
||||
@@ -39,8 +43,10 @@ abstract class MediaLibraryTestBase extends WebDriverTestBase {
|
||||
->getSourceFieldDefinition($media->bundle->entity)
|
||||
->getName();
|
||||
$media->set($source_field, $name)->setCreatedTime(++$time)->save();
|
||||
$created_items[$name] = $media;
|
||||
}
|
||||
}
|
||||
return $created_items;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ class ViewsUiIntegrationTest extends MediaLibraryTestBase {
|
||||
$page->find('css', '.js-media-library-view .view-filters')->pressButton('Apply filters');
|
||||
$this->waitForElementsCount('css', '.js-media-library-item', 1);
|
||||
|
||||
// Test the same routine but in the view for the table wiget.
|
||||
// Test the same routine but in the view for the table widget.
|
||||
$this->drupalGet('/admin/structure/views/view/media_library/edit/widget_table');
|
||||
$this->waitForElementsCount('css', '.js-media-library-item', 8);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\media_library\Form\FileUploadForm;
|
||||
use Drupal\media_library\Form\OEmbedForm;
|
||||
use Drupal\media_library\MediaLibraryState;
|
||||
use Drupal\media_library_form_overwrite_test\Form\TestAddForm;
|
||||
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
|
||||
use Drupal\Tests\user\Traits\UserCreationTrait;
|
||||
|
||||
@@ -137,4 +138,19 @@ class MediaLibraryAddFormTest extends KernelTestBase {
|
||||
\Drupal::formBuilder()->buildForm(FileUploadForm::class, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests overwriting of the add form.
|
||||
*/
|
||||
public function testDifferentAddForm() {
|
||||
$this->enableModules(['media_library_form_overwrite_test']);
|
||||
|
||||
$entity_type_manager = \Drupal::entityTypeManager();
|
||||
$image = $entity_type_manager->getStorage('media_type')->load('image');
|
||||
|
||||
$image_source_definition = $image->getSource()->getPluginDefinition();
|
||||
|
||||
// Assert the overwritten form class is set to the media source.
|
||||
$this->assertSame(TestAddForm::class, $image_source_definition['forms']['media_library_add']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media_library\Kernel;
|
||||
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\entity_test\Entity\EntityTestRev;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\media\Entity\MediaType;
|
||||
use Drupal\Tests\user\Traits\UserCreationTrait;
|
||||
|
||||
/**
|
||||
* Tests the media library widget.
|
||||
*
|
||||
* @coversDefaultClass \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget
|
||||
* @group media_library
|
||||
*/
|
||||
class MediaLibraryWidgetTest extends KernelTestBase {
|
||||
|
||||
use UserCreationTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = [
|
||||
'media',
|
||||
'media_library',
|
||||
'field',
|
||||
'image',
|
||||
'system',
|
||||
'views',
|
||||
'user',
|
||||
'entity_test',
|
||||
];
|
||||
|
||||
/**
|
||||
* An admin user.
|
||||
*
|
||||
* @var \Drupal\user\Entity\User
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->baseField = BaseFieldDefinition::create('entity_reference')
|
||||
->setName('media')
|
||||
->setSetting('target_type', 'media')
|
||||
->setSetting('handler_settings', ['target_bundles' => ['test_type' => 'test_type']]);
|
||||
$this->container->get('state')->set('entity_test.additional_base_field_definitions', [
|
||||
'media' => $this->baseField,
|
||||
]);
|
||||
$this->container->get('state')->set('entity_test_rev.additional_base_field_definitions', [
|
||||
'media' => $this->baseField,
|
||||
]);
|
||||
|
||||
$this->installEntitySchema('entity_test');
|
||||
$this->installEntitySchema('entity_test_rev');
|
||||
$this->installEntitySchema('user');
|
||||
$this->installSchema('system', ['sequences', 'key_value_expire']);
|
||||
$this->installConfig([
|
||||
'system',
|
||||
'image',
|
||||
'media',
|
||||
'media_library',
|
||||
]);
|
||||
|
||||
MediaType::create([
|
||||
'id' => 'test_type',
|
||||
'label' => 'Test type',
|
||||
'source' => 'image',
|
||||
])->save();
|
||||
|
||||
// Create user 1 so the test user doesn't bypass access control.
|
||||
$this->createUser();
|
||||
|
||||
$this->adminUser = $this->createUser([
|
||||
'administer entity_test content',
|
||||
'view media',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the media library widget access.
|
||||
*/
|
||||
public function testWidgetAccess() {
|
||||
$entity = EntityTest::create([
|
||||
'name' => 'sample entity',
|
||||
]);
|
||||
$entity->save();
|
||||
$element = $this->buildWidgetForm($entity);
|
||||
$this->assertMediaLibraryStateAccess(TRUE, $this->adminUser, $element['open_button']['#media_library_state']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the media library widget access with a revisionable entity type.
|
||||
*/
|
||||
public function testRevisionableWidgetAccess() {
|
||||
$allowed_revision = EntityTestRev::create([
|
||||
'name' => 'allowed_access',
|
||||
]);
|
||||
$allowed_revision->save();
|
||||
|
||||
$denied_revision = clone $allowed_revision;
|
||||
$denied_revision->setNewRevision();
|
||||
$denied_revision->name = 'forbid_access';
|
||||
$denied_revision->save();
|
||||
|
||||
$element = $this->buildWidgetForm($allowed_revision);
|
||||
$this->assertMediaLibraryStateAccess(TRUE, $this->adminUser, $element['open_button']['#media_library_state']);
|
||||
|
||||
$element = $this->buildWidgetForm($denied_revision);
|
||||
$this->assertMediaLibraryStateAccess(FALSE, $this->adminUser, $element['open_button']['#media_library_state']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert if the given user has access to the given state.
|
||||
*
|
||||
* @param bool $access
|
||||
* The access result to assert.
|
||||
* @param \Drupal\Core\Session\AccountInterface $user
|
||||
* The user account.
|
||||
* @param \Drupal\media_library\MediaLibraryState $state
|
||||
* The media library state.
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function assertMediaLibraryStateAccess($access, $user, $state) {
|
||||
$ui_builder = $this->container->get('media_library.ui_builder');
|
||||
$access_result = $ui_builder->checkAccess($user, $state);
|
||||
$this->assertEquals($access, $access_result->isAllowed());
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the media library widget form.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The entity to build the form for.
|
||||
*
|
||||
* @return array
|
||||
* A built form array of the media library widget.
|
||||
*/
|
||||
protected function buildWidgetForm($entity) {
|
||||
$form = [
|
||||
'#parents' => [],
|
||||
];
|
||||
return $this->container->get('plugin.manager.field.widget')->createInstance('media_library_widget', [
|
||||
'field_definition' => $this->baseField,
|
||||
'settings' => [],
|
||||
'third_party_settings' => [],
|
||||
])->formElement($entity->media, 0, ['#description' => ''], $form, new FormState());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user