MediaLibraryInceptionWidget.php

Same filename and directory in other branches
  1. 9 core/modules/media_library/tests/modules/media_library_test_widget/src/Plugin/Field/FieldWidget/MediaLibraryInceptionWidget.php
  2. 8.9.x core/modules/media_library/tests/modules/media_library_test_widget/src/Plugin/Field/FieldWidget/MediaLibraryInceptionWidget.php
  3. 10 core/modules/media_library/tests/modules/media_library_test_widget/src/Plugin/Field/FieldWidget/MediaLibraryInceptionWidget.php

Namespace

Drupal\media_library_test_widget\Plugin\Field\FieldWidget

File

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

View source
<?php

namespace Drupal\media_library_test_widget\Plugin\Field\FieldWidget;

use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget;

/**
 * Plugin implementation of the 'media_library_inception_widget' widget.
 *
 * This widget is used to simulate the media library widget nested inside
 * another widget that performs validation of required fields before there is
 * an opportunity to add media.
 */
class MediaLibraryInceptionWidget extends MediaLibraryWidget {
    
    /**
     * {@inheritdoc}
     */
    public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
        if (empty($element['#element_validate'])) {
            $element['#element_validate'] = [];
        }
        $element['#element_validate'][] = [
            $this,
            'elementValidate',
        ];
        return parent::formElement($items, $delta, $element, $form, $form_state);
    }
    
    /**
     * {@inheritdoc}
     */
    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']);
        }
    }

}

Classes

Title Deprecated Summary
MediaLibraryInceptionWidget Plugin implementation of the 'media_library_inception_widget' widget.

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