diff --git a/core/modules/media/src/Form/MediaInlineForm.php b/core/modules/media/src/Form/MediaInlineForm.php index 3017089..18ff096 100644 --- a/core/modules/media/src/Form/MediaInlineForm.php +++ b/core/modules/media/src/Form/MediaInlineForm.php @@ -99,7 +99,7 @@ public function entityForm(array $entity_form, FormStateInterface $form_state) { // Build the media entity form. /** @var \Drupal\media\MediaInterface $entity */ $entity = $entity_form['#entity']; - $form_display = $this->getFormDisplay($entity, $entity_form['#form_mode']); + $form_display = EntityFormDisplay::collectRenderDisplay($entity, $entity_form['#form_mode']); $form_display->buildForm($entity, $entity_form, $form_state); $entity_form['#weight'] = 100; @@ -111,14 +111,10 @@ public function entityForm(array $entity_form, FormStateInterface $form_state) { if (!$field_definition->isRequired()) { $entity_form[$field_name]['#access'] = FALSE; } - elseif (!empty($entity_form[$field_name])) { - // Elements with "#required" key set will always be validated, even if - // 'limit_validation_errors' is set. Disable their validation here, we - // will enforce validation happens inside the real submit handler. - $entity_form[$field_name] = $this->disableElementChildrenValidation($entity_form[$field_name]); - } - } + // The media name will be automatically populated by the source plugin, we + // 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. @@ -180,7 +176,7 @@ public function entityFormValidate(array &$entity_form, FormStateInterface $form $entity = $media_form['#entity']; if ($entity instanceof ContentEntityInterface) { $this->buildEntity($media_form, $entity, $form_state); - $form_display = $this->getFormDisplay($entity, $media_form['#form_mode']); + $form_display = EntityFormDisplay::collectRenderDisplay($entity, $media_form['#form_mode']); $form_display->validateFormValues($entity, $media_form, $form_state); $entity->setValidationRequired(FALSE); @@ -200,30 +196,6 @@ public function entityFormValidate(array &$entity_form, FormStateInterface $form } /** - * Bypass validation in all children of a given element. - * - * @param array $element - * The element array. - * - * @return array - * The same element array, after recursively checking all its children and - * setting "#validated" => TRUE in all required elements. - */ - private function disableElementChildrenValidation(array $element) { - foreach (Element::children($element) as $key) { - if (isset($element[$key]) && $element[$key]) { - $element[$key] = $this->disableElementChildrenValidation($element[$key]); - } - } - - if (!empty($element['#needs_validation']) || !empty($element['#required'])) { - $element['#validated'] = TRUE; - } - - return $element; - } - - /** * Builds an updated entity object based upon the submitted form values. * * @param array $entity_form @@ -234,7 +206,7 @@ private function disableElementChildrenValidation(array $element) { * The current state of the form. */ protected function buildEntity(array $entity_form, ContentEntityInterface $entity, FormStateInterface $form_state) { - $form_display = $this->getFormDisplay($entity, $entity_form['#form_mode']); + $form_display = EntityFormDisplay::collectRenderDisplay($entity, $entity_form['#form_mode']); $form_display->extractFormValues($entity, $entity_form, $form_state); // Invoke all specified builders for copying form values to entity fields. if (isset($entity_form['#entity_builders'])) { @@ -245,21 +217,6 @@ protected function buildEntity(array $entity_form, ContentEntityInterface $entit } /** - * Gets the form display for the given entity. - * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The entity. - * @param string $form_mode - * The form mode. - * - * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface - * The form display. - */ - protected function getFormDisplay(ContentEntityInterface $entity, $form_mode) { - return EntityFormDisplay::collectRenderDisplay($entity, $form_mode); - } - - /** * Check if the form processing is due to a host submit. * * @param \Drupal\Core\Form\FormStateInterface $form_state diff --git a/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php b/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php index 6a0a4c5..2d4b074 100644 --- a/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php +++ b/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php @@ -187,15 +187,11 @@ public static function value($element, $input, FormStateInterface $form_state) { // to make the media. if (empty($input['mids']) && !empty($return['fids']) && !$form_state->isRebuilding() && $media_type) { foreach ($return['fids'] as $fid) { - $file = File::load($fid); /** @var \Drupal\media\MediaInterface $media_entity */ $media_entity = Media::create([ - // @TODO: Shouldn't media entities automatically deal with empty names? - 'name' => $file->getFilename(), 'bundle' => $media_type, 'uid' => \Drupal::currentUser()->id(), - // TODO: figure out langcode in this context. - // 'langcode' => !empty($element['#langcode']) ? $element['#langcode'] : LanguageInterface::LANGCODE_DEFAULT, + 'langcode' => $host_entity->language()->getId(), ]); // We create media items as unpublished to prevent "abandoned" uploads // from producing visible media items on the site. In @@ -296,6 +292,12 @@ public static function process($element, FormStateInterface $form_state, $form) '#type' => 'container', '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'] = []; } }