summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-07-31 10:28:10 (GMT)
committerAlex Pott2018-07-31 10:28:23 (GMT)
commit1dbb94162f3f58b98f23bf9b7a1cf4d566af2985 (patch)
tree2c368ca1e43b12eec54628cd678a3f913c7b8ef2
parent14026e145263a822f7a2c0ce23ca7a94c65309c3 (diff)
Issue #2988617 by samuel.mortenson, bkosborne, alexpott, phenaproxima, marcoscano, amateescu, chr.fritsch: Creating media with the media library upload is broken for unlimited cardinality
(cherry picked from commit 057b54e03f16d53b05fb2b9faffcb4b7196641e1)
-rw-r--r--core/modules/media_library/src/Form/MediaLibraryUploadForm.php21
-rw-r--r--core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php28
-rw-r--r--core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_form_display.node.basic_page.default.yml7
-rw-r--r--core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_view_display.node.basic_page.default.yml10
-rw-r--r--core/modules/media_library/tests/modules/media_library_test/config/install/field.field.node.basic_page.field_noadd_media.yml29
-rw-r--r--core/modules/media_library/tests/modules/media_library_test/config/install/field.field.node.basic_page.field_unlimited_media.yml1
-rw-r--r--core/modules/media_library/tests/modules/media_library_test/config/install/field.storage.node.field_noadd_media.yml19
-rw-r--r--core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php27
8 files changed, 126 insertions, 16 deletions
diff --git a/core/modules/media_library/src/Form/MediaLibraryUploadForm.php b/core/modules/media_library/src/Form/MediaLibraryUploadForm.php
index 3ed96b4..0209fda 100644
--- a/core/modules/media_library/src/Form/MediaLibraryUploadForm.php
+++ b/core/modules/media_library/src/Form/MediaLibraryUploadForm.php
@@ -8,6 +8,7 @@ use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\TypedData\FieldItemDataDefinition;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
@@ -358,7 +359,7 @@ class MediaLibraryUploadForm extends FormBase {
$element['#value'] = [];
}
$values = $form_state->getValue('upload', []);
- if (count($values['fids']) > $element['#cardinality']) {
+ if (count($values['fids']) > $element['#cardinality'] && $element['#cardinality'] !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
$form_state->setError($element, $this->t('A maximum of @count files can be uploaded.', [
'@count' => $element['#cardinality'],
]));
@@ -444,26 +445,36 @@ class MediaLibraryUploadForm extends FormBase {
/**
* Access callback to check that the user can create file based media.
*
+ * @param array $allowed_types
+ * (optional) The contextually allowed types.
+ *
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
+ *
+ * @todo Remove $allowed_types param in https://www.drupal.org/node/2956747
*/
- public function access() {
- return AccessResultAllowed::allowedIf(count($this->getTypes()))->mergeCacheMaxAge(0);
+ public function access(array $allowed_types = NULL) {
+ return AccessResultAllowed::allowedIf(count($this->getTypes($allowed_types)))->mergeCacheMaxAge(0);
}
/**
* Returns media types which use files that the current user can create.
*
+ * @param array $allowed_types
+ * (optional) The contextually allowed types.
+ *
* @todo Move in https://www.drupal.org/node/2987924
*
* @return \Drupal\media\MediaTypeInterface[]
* A list of media types that are valid for this form.
*/
- protected function getTypes() {
+ protected function getTypes(array $allowed_types = NULL) {
// Cache results if possible.
if (!isset($this->types)) {
$media_type_storage = $this->entityTypeManager->getStorage('media_type');
- $allowed_types = _media_library_get_allowed_types() ?: NULL;
+ if (!$allowed_types) {
+ $allowed_types = _media_library_get_allowed_types() ?: NULL;
+ }
$types = $media_type_storage->loadMultiple($allowed_types);
$types = $this->filterTypesWithFileSource($types);
$types = $this->filterTypesWithCreateAccess($types);
diff --git a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
index 2908002..e9362a0 100644
--- a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
+++ b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
@@ -14,6 +14,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\media\Entity\Media;
+use Drupal\media_library\Form\MediaLibraryUploadForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
@@ -42,6 +43,13 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
protected $entityTypeManager;
/**
+ * Indicates whether or not the add button should be shown.
+ *
+ * @var bool
+ */
+ protected $addAccess = FALSE;
+
+ /**
* Constructs a MediaLibraryWidget widget.
*
* @param string $plugin_id
@@ -56,23 +64,30 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
* Any third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager service.
+ * @param bool $add_access
+ * Indicates whether or not the add button should be shown.
*/
- public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
+ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, $add_access) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
+ $this->addAccess = $add_access;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+ $settings = $configuration['field_definition']->getSettings()['handler_settings'];
+ $target_bundles = isset($settings['target_bundles']) ? $settings['target_bundles'] : NULL;
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
- $container->get('entity_type.manager')
+ $container->get('entity_type.manager'),
+ // @todo Use URL access in https://www.drupal.org/node/2956747
+ MediaLibraryUploadForm::create($container)->access($target_bundles)->isAllowed()
);
}
@@ -248,14 +263,13 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
'#access' => $cardinality_unlimited || $remaining > 0,
];
- $add_url = Url::fromRoute('media_library.upload', [], [
- 'query' => $query,
- ]);
$element['media_library_add_button'] = [
'#type' => 'link',
'#title' => $this->t('Add media'),
'#name' => $field_name . '-media-library-add-button' . $id_suffix,
- '#url' => $add_url,
+ '#url' => Url::fromRoute('media_library.upload', [], [
+ 'query' => $query,
+ ]),
'#attributes' => [
'class' => ['button', 'use-ajax', 'media-library-add-button'],
'data-dialog-type' => 'modal',
@@ -263,7 +277,7 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
],
// Prevent errors in other widgets from preventing addition.
'#limit_validation_errors' => $limit_validation_errors,
- '#access' => $add_url->access() && ($cardinality_unlimited || $remaining > 0),
+ '#access' => $this->addAccess && ($cardinality_unlimited || $remaining > 0),
];
// This hidden field and button are used to add new items to the widget.
diff --git a/core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_form_display.node.basic_page.default.yml b/core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_form_display.node.basic_page.default.yml
index 0714863..e18981c 100644
--- a/core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_form_display.node.basic_page.default.yml
+++ b/core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_form_display.node.basic_page.default.yml
@@ -4,6 +4,7 @@ dependencies:
config:
- field.field.node.basic_page.field_twin_media
- field.field.node.basic_page.field_unlimited_media
+ - field.field.node.basic_page.field_noadd_media
- node.type.basic_page
module:
- media_library
@@ -30,6 +31,12 @@ content:
settings: { }
third_party_settings: { }
region: content
+ field_noadd_media:
+ type: media_library_widget
+ weight: 123
+ settings: { }
+ third_party_settings: { }
+ region: content
promote:
type: boolean_checkbox
settings:
diff --git a/core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_view_display.node.basic_page.default.yml b/core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_view_display.node.basic_page.default.yml
index b747e2a..a66daea 100644
--- a/core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_view_display.node.basic_page.default.yml
+++ b/core/modules/media_library/tests/modules/media_library_test/config/install/core.entity_view_display.node.basic_page.default.yml
@@ -4,6 +4,7 @@ dependencies:
config:
- field.field.node.basic_page.field_twin_media
- field.field.node.basic_page.field_unlimited_media
+ - field.field.node.basic_page.field_noadd_media
- node.type.basic_page
module:
- user
@@ -30,6 +31,15 @@ content:
link: false
third_party_settings: { }
region: content
+ field_noadd_media:
+ type: entity_reference_entity_view
+ weight: 103
+ label: above
+ settings:
+ view_mode: default
+ link: false
+ third_party_settings: { }
+ region: content
links:
weight: 100
settings: { }
diff --git a/core/modules/media_library/tests/modules/media_library_test/config/install/field.field.node.basic_page.field_noadd_media.yml b/core/modules/media_library/tests/modules/media_library_test/config/install/field.field.node.basic_page.field_noadd_media.yml
new file mode 100644
index 0000000..327e0fb
--- /dev/null
+++ b/core/modules/media_library/tests/modules/media_library_test/config/install/field.field.node.basic_page.field_noadd_media.yml
@@ -0,0 +1,29 @@
+langcode: en
+status: true
+dependencies:
+ config:
+ - field.storage.node.field_noadd_media
+ - media.type.type_one
+ - media.type.type_two
+ - node.type.basic_page
+id: node.basic_page.field_noadd_media
+field_name: field_noadd_media
+entity_type: node
+bundle: basic_page
+label: 'No add media'
+description: ''
+required: false
+translatable: false
+default_value: { }
+default_value_callback: ''
+settings:
+ handler: 'default:media'
+ handler_settings:
+ target_bundles:
+ type_one: type_one
+ type_two: type_two
+ sort:
+ field: _none
+ auto_create: false
+ auto_create_bundle: file
+field_type: entity_reference
diff --git a/core/modules/media_library/tests/modules/media_library_test/config/install/field.field.node.basic_page.field_unlimited_media.yml b/core/modules/media_library/tests/modules/media_library_test/config/install/field.field.node.basic_page.field_unlimited_media.yml
index c1564e1..69c0fc4 100644
--- a/core/modules/media_library/tests/modules/media_library_test/config/install/field.field.node.basic_page.field_unlimited_media.yml
+++ b/core/modules/media_library/tests/modules/media_library_test/config/install/field.field.node.basic_page.field_unlimited_media.yml
@@ -20,6 +20,7 @@ settings:
handler_settings:
target_bundles:
type_one: type_one
+ type_three: type_three
sort:
field: _none
auto_create: false
diff --git a/core/modules/media_library/tests/modules/media_library_test/config/install/field.storage.node.field_noadd_media.yml b/core/modules/media_library/tests/modules/media_library_test/config/install/field.storage.node.field_noadd_media.yml
new file mode 100644
index 0000000..80bb78a
--- /dev/null
+++ b/core/modules/media_library/tests/modules/media_library_test/config/install/field.storage.node.field_noadd_media.yml
@@ -0,0 +1,19 @@
+langcode: en
+status: true
+dependencies:
+ module:
+ - media
+ - node
+id: node.field_noadd_media
+field_name: field_noadd_media
+entity_type: node
+type: entity_reference
+settings:
+ target_type: media
+module: core
+locked: false
+cardinality: -1
+translatable: true
+indexes: { }
+persist_with_no_fields: false
+custom_storage: false
diff --git a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php
index b92a42e..21b2a46 100644
--- a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php
+++ b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php
@@ -317,9 +317,12 @@ class MediaLibraryTest extends WebDriverTestBase {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = $this->container->get('file_system');
+ // Ensure that the add button is not present if no media can be created.
+ $assert_session->elementNotExists('css', '.media-library-add-button[href*="field_noadd_media"]');
+
// Add to the twin media field using the add button directly on the widget.
- $unlimited_button = $assert_session->elementExists('css', '.media-library-add-button[href*="field_twin_media"]');
- $unlimited_button->click();
+ $twin_button = $assert_session->elementExists('css', '.media-library-add-button[href*="field_twin_media"]');
+ $twin_button->click();
$assert_session->assertWaitOnAjaxRequest();
$page->attachFileToField('Upload', $this->container->get('file_system')->realpath($png_image->uri));
@@ -348,10 +351,26 @@ class MediaLibraryTest extends WebDriverTestBase {
$assert_session->pageTextNotContains('Media library');
$assert_session->pageTextContains($png_image->filename);
- // Open the browser again to test type resolution.
- $unlimited_button = $assert_session->elementExists('css', '.media-library-open-button[href*="field_twin_media"]');
+ // Also make sure that we can upload to the unlimited cardinality field.
+ $unlimited_button = $assert_session->elementExists('css', '.media-library-add-button[href*="field_unlimited_media"]');
$unlimited_button->click();
$assert_session->assertWaitOnAjaxRequest();
+
+ $page->attachFileToField('Upload', $this->container->get('file_system')->realpath($png_image->uri));
+ $assert_session->assertWaitOnAjaxRequest();
+ $page->fillField('Name', 'Unlimited Cardinality Image');
+ $page->fillField('Alternative text', $this->randomString());
+ $assert_session->elementExists('css', '.ui-dialog-buttonpane')->pressButton('Save');
+ $assert_session->assertWaitOnAjaxRequest();
+
+ // Ensure the media item was added.
+ $assert_session->pageTextNotContains('Media library');
+ $assert_session->pageTextContains('Unlimited Cardinality Image');
+
+ // Open the browser again to test type resolution.
+ $twin_button = $assert_session->elementExists('css', '.media-library-open-button[href*="field_twin_media"]');
+ $twin_button->click();
+ $assert_session->assertWaitOnAjaxRequest();
$assert_session->pageTextContains('Media library');
$assert_session->elementExists('css', '#drupal-modal')->clickLink('Add media');
$assert_session->assertWaitOnAjaxRequest();