diff --git a/core/modules/media/src/Element/MediaManagedFile.php b/core/modules/media/src/Element/MediaManagedFile.php index 74a5e93..4a9a28b 100644 --- a/core/modules/media/src/Element/MediaManagedFile.php +++ b/core/modules/media/src/Element/MediaManagedFile.php @@ -5,6 +5,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\file\Element\ManagedFile; use Drupal\media\Entity\Media; +use Symfony\Component\HttpFoundation\Request; /** * Extends the managed_file element to include the associated Media information. @@ -75,4 +76,20 @@ public static function validateEntityForm(array &$entity_form, FormStateInterfac ->entityFormValidate($entity_form, $form_state); } + /** + * {@inheritdoc} + */ + public static function uploadAjaxCallback(&$form, FormStateInterface &$form_state, Request $request) { + // Clear our fake error. + $errors = drupal_get_messages('error'); + $request->getSession()->getBag('flashes')->clear(); + foreach ($errors['error'] as $error) { + if ($error !== 'Dummy inline form error') { + drupal_set_message($error, 'error'); + } + } + + return parent::uploadAjaxCallback($form, $form_state, $request); + } + } diff --git a/core/modules/media/src/Form/MediaInlineForm.php b/core/modules/media/src/Form/MediaInlineForm.php index 18ff096..b268f6c 100644 --- a/core/modules/media/src/Form/MediaInlineForm.php +++ b/core/modules/media/src/Form/MediaInlineForm.php @@ -116,8 +116,8 @@ public function entityForm(array $entity_form, FormStateInterface $form_state) { // are OK with the default name in this case. $entity_form['name']['#access'] = FALSE; - // We've already set the just-uploaded file as the value of the source - // field, so hide that too. + // By this point it is expected that the source field has already been + // populated, so hide it too. An example for the file widget can be found in // @see \Drupal\media\Plugin\Field\FieldWidget\MediaFileWidget::value(). $source_field = $entity->getSource() ->getSourceFieldDefinition($entity->bundle->entity) diff --git a/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php b/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php index 2d4b074..5d87cd2 100644 --- a/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php +++ b/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Form\FormBuilder; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\ElementInfoManagerInterface; use Drupal\Core\Render\RendererInterface; @@ -293,11 +294,19 @@ public static function process($element, FormStateInterface $form_state, $form) 'form' => $media_form, ]; - // Prevent the media form elements that were added from failing - // required field validation when the form is first processed. - // @TODO Finish this. -// $element['upload_button']['#limit_validation_errors'] = []; -// $element['remove_button']['#limit_validation_errors'] = []; + // Prevent the added form elements from failing the required fields + // validation when the form is first processed. + $element['upload_button']['#limit_validation_errors'] = []; + $element['remove_button']['#limit_validation_errors'] = []; + // We set a "fake" error here to prevent the form rebuilding process, + // and we remove this error message later in + // \Drupal\media\Element\MediaManagedFile::uploadAjaxCallback(). + // @TODO Fgure out a better way of doing this. + $remove_operation = !empty($form_state->getUserInput()['_triggering_element_value']) && $form_state->getUserInput()['_triggering_element_value'] === 'Remove'; + if (\Drupal::request()->query->has(FormBuilder::AJAX_FORM_REQUEST) && !$remove_operation) { + $non_existent_element = ['#parents' => []]; + $form_state->setError($non_existent_element, 'Dummy inline form error'); + } } } diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaInlineFileWidgetTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaInlineFileWidgetTest.php index b109fc7..f82b523 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaInlineFileWidgetTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaInlineFileWidgetTest.php @@ -164,9 +164,7 @@ public function testInlineFileWidget() { $result = $assert_session->waitForButton('Remove'); $this->assertNotEmpty($result); $media_field = $assert_session->elementExists('css', 'details[data-drupal-selector="edit-field-media-reference"]'); - // The name field should be present and required. - $assert_session->elementExists('css', '.field--name-name input.required', $media_field); - // The additional field sould be present and required. + // The required field sould be present and required. $assert_session->elementExists('css', '.field--name-field-media-required input.required', $media_field); // The non-required field should not be there. $assert_session->elementNotExists('css', '.field--name-field-media-nonrequired input', $media_field);