function MediaLibraryInceptionWidget::elementValidate

Same name in other branches
  1. 9 core/modules/media_library/tests/modules/media_library_test_widget/src/Plugin/Field/FieldWidget/MediaLibraryInceptionWidget.php \Drupal\media_library_test_widget\Plugin\Field\FieldWidget\MediaLibraryInceptionWidget::elementValidate()
  2. 8.9.x core/modules/media_library/tests/modules/media_library_test_widget/src/Plugin/Field/FieldWidget/MediaLibraryInceptionWidget.php \Drupal\media_library_test_widget\Plugin\Field\FieldWidget\MediaLibraryInceptionWidget::elementValidate()
  3. 10 core/modules/media_library/tests/modules/media_library_test_widget/src/Plugin/Field/FieldWidget/MediaLibraryInceptionWidget.php \Drupal\media_library_test_widget\Plugin\Field\FieldWidget\MediaLibraryInceptionWidget::elementValidate()

File

core/modules/media_library/tests/modules/media_library_test_widget/src/Plugin/Field/FieldWidget/MediaLibraryInceptionWidget.php, line 44

Class

MediaLibraryInceptionWidget
Plugin implementation of the 'media_library_inception_widget' widget.

Namespace

Drupal\media_library_test_widget\Plugin\Field\FieldWidget

Code

public function elementValidate($element, FormStateInterface $form_state, $form) {
    $field_name = $element['#field_name'];
    $entity = $form_state->getFormObject()
        ->getEntity();
    $input = $form_state->getUserInput();
    if (!empty($input['_triggering_element_name']) && str_contains($input['_triggering_element_name'], 'media-library-update')) {
        // This will validate a required field before an upload is completed.
        $display = EntityFormDisplay::collectRenderDisplay($entity, 'edit');
        $display->extractFormValues($entity, $form, $form_state);
        $display->validateFormValues($entity, $form, $form_state);
    }
    $form_value = $form_state->getValue($field_name);
    if (!empty($form_value['media_library_selection'])) {
        $entity->set($field_name, $form_value['media_library_selection']);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.