class FileUploadForm extends AddFormBase (View source)

internal  Form classes are internal.
 

Creates a form to create media entities from uploaded files.

Traits

Provides dependency injection friendly methods for serialization.

Wrapper methods for the logger factory service.

Provides a trait for the messenger service.

Wrapper methods for the Redirect Destination.

Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.

Properties

protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected LoggerChannelFactoryInterface $loggerFactory

The logger channel factory service.

from  LoggerChannelTrait
protected MessengerInterface $messenger

The messenger.

from  MessengerTrait
protected RedirectDestinationInterface $redirectDestination

The redirect destination service.

from  RedirectDestinationTrait
protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected RequestStack $requestStack

The request stack.

from  FormBase
protected ConfigFactoryInterface $configFactory

The config factory.

from  FormBase
protected RouteMatchInterface $routeMatch

The route match.

from  FormBase
protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

from  AddFormBase
protected MediaLibraryUiBuilder $libraryUiBuilder

The media library UI builder.

from  AddFormBase
protected MediaTypeInterface $mediaType

The type of media items being created by this form.

from  AddFormBase
protected EntityViewBuilderInterface $viewBuilder

The media view builder.

from  AddFormBase
protected OpenerResolverInterface $openerResolver

The opener resolver.

from  AddFormBase
protected ElementInfoManagerInterface $elementInfo

The element info manager.

protected ElementInfoManagerInterface $renderer

The renderer service.

protected FileSystemInterface $fileSystem

The file system service.

protected FileUsageInterface $fileUsage

The file usage service.

protected FileRepositoryInterface $fileRepository

The file repository service.

Methods

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

LoggerInterface
getLogger(string $channel)

Gets the logger for a specific channel.

$this
setLoggerFactory(LoggerChannelFactoryInterface $logger_factory)

Injects the logger channel factory.

setMessenger(MessengerInterface $messenger)

Sets the messenger.

messenger()

Gets the messenger.

array
getDestinationArray()

Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.

getRedirectDestination()

Returns the redirect destination service.

$this
setRedirectDestination(RedirectDestinationInterface $redirect_destination)

Sets the redirect destination service.

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

getStringTranslation()

Gets the string translation service.

$this
setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

static 
create(ContainerInterface $container)

Instantiates a new instance of this class.

validateForm(array $form, FormStateInterface $form_state)

Form validation handler.

config(string $name)

Retrieves a configuration object.

from  FormBase
configFactory()

Gets the config factory for this form.

from  FormBase
$this
setConfigFactory(ConfigFactoryInterface $config_factory)

Sets the config factory for this form.

from  FormBase
resetConfigFactory()

Resets the configuration factory.

from  FormBase
Request
getRequest()

Gets the request object.

from  FormBase
getRouteMatch()

Gets the route match.

from  FormBase
$this
setRequestStack(RequestStack $request_stack)

Sets the request stack object to use.

from  FormBase
currentUser()

Gets the current user.

from  FormBase
RedirectResponse
redirect(string $route_name, array $route_parameters = [], array $options = [], int $status = 302)

Returns a redirect response object for the specified route.

from  FormBase
LoggerInterface
logger(string $channel)

Gets the logger for a specific channel.

from  FormBase
__construct(EntityTypeManagerInterface $entity_type_manager, MediaLibraryUiBuilder $library_ui_builder, ElementInfoManagerInterface $element_info, RendererInterface $renderer, FileSystemInterface $file_system, OpenerResolverInterface $opener_resolver, FileUsageInterface $file_usage, FileRepositoryInterface $file_repository = NULL)

Constructs a new FileUploadForm.

string|null
getBaseFormId()

Returns a string identifying the base form.

getMediaType(FormStateInterface $form_state)

Get the media type from the form state.

array
buildForm(array $form, FormStateInterface $form_state)

Form constructor.

array
buildInputElement(array $form, FormStateInterface $form_state)

Builds the element for submitting source field value(s).

array
buildEntityFormElement(MediaInterface $media, array $form, FormStateInterface $form_state, int $delta)

Builds the sub-form for setting required fields on a new media item.

static string[]
trustedCallbacks()

Lists the trusted callbacks provided by the implementing class.

array
preRenderAddedMedia(array $element)

Converts the set of newly added media into an item list for rendering.

array
buildCurrentSelectionArea(array $form, FormStateInterface $form_state)

Returns a render array containing the current selection.

array
buildSelectedItemElement(MediaInterface $media, array $form, FormStateInterface $form_state)

Returns a render array for a single pre-selected media item.

array
buildActions(array $form, FormStateInterface $form_state)

Returns an array of supported actions for the form.

processInputValues(array $source_field_values, array $form, FormStateInterface $form_state)

Creates media items from source field input values.

createMediaFromValue(MediaTypeInterface $media_type, EntityStorageInterface $media_storage, string $source_field_name, $file)

Creates a new, unsaved media item from a source field value.

prepareMediaEntityForSave(MediaInterface $media)

Prepares a created media item to be permanently saved.

removeButtonSubmit(array $form, FormStateInterface $form_state)

Submit handler for the remove button.

AjaxResponse|array
updateFormCallback(array $form, FormStateInterface $form_state)

AJAX callback to update the entire form based on source field input.

validateMediaEntity(MediaInterface $media, array $form, FormStateInterface $form_state, int $delta)

Validate a created media item.

submitForm(array $form, FormStateInterface $form_state)

Form submission handler.

array|AjaxResponse
updateLibrary(array $form, FormStateInterface $form_state)

AJAX callback to send the new media item(s) to the media library.

array
buildMediaLibraryUi(FormStateInterface $form_state)

Build the render array of the media library UI.

array|AjaxResponse
updateWidget(array $form, FormStateInterface $form_state)

AJAX callback to send the new media item(s) to the calling code.

getMediaLibraryState(FormStateInterface $form_state)

Get the media library state from the form state.

string
getSourceFieldName(MediaTypeInterface $media_type)

Returns the name of the source field for a media type.

getPreSelectedMediaItems(FormStateInterface $form_state)

Get all pre-selected media items from the form state.

getAddedMediaItems(FormStateInterface $form_state)

Get all added media items from the form state.

getCurrentMediaItems(FormStateInterface $form_state)

Get all pre-selected and added media items from the form state.

bool
isAdvancedUi()

Determines if the "advanced UI" of the Media Library is enabled.

string
getFormId()

Returns a unique string identifying the form.

array
validateUploadElement(array $element, FormStateInterface $form_state)

Validates the upload element.

array
processUploadElement(array $element, FormStateInterface $form_state)

Processes an upload (managed_file) element.

static array
hideExtraSourceFieldComponents(array $element, FormStateInterface $form_state, $form)

Processes an image or file source field element.

uploadButtonSubmit(array $form, FormStateInterface $form_state)

Submit handler for the upload button, inside the managed_file element.

createFileItem(MediaTypeInterface $media_type)

Create a file field item.

Details

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

protected LoggerInterface getLogger(string $channel)

Gets the logger for a specific channel.

Require the use of injected services: https://www.drupal.org/node/2733703

Parameters

string $channel

The name of the channel. Can be any string, but the general practice is to use the name of the subsystem calling this.

Return Value

LoggerInterface

The logger for the given channel.

$this setLoggerFactory(LoggerChannelFactoryInterface $logger_factory)

Injects the logger channel factory.

Parameters

LoggerChannelFactoryInterface $logger_factory

The logger channel factory service.

Return Value

$this

setMessenger(MessengerInterface $messenger)

Sets the messenger.

Parameters

MessengerInterface $messenger

The messenger.

MessengerInterface messenger()

Gets the messenger.

Return Value

MessengerInterface

The messenger.

protected array getDestinationArray()

Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.

Return Value

array

An associative array containing the key:

  • destination: The value of the current request's 'destination' query parameter, if present. This can be either a relative or absolute URL. However, for security, redirection to external URLs is not performed. If the query parameter isn't present, then the URL of the current request is returned.

See also

RedirectDestinationInterface::getAsArray

protected RedirectDestinationInterface getRedirectDestination()

Returns the redirect destination service.

Return Value

RedirectDestinationInterface

The redirect destination helper.

$this setRedirectDestination(RedirectDestinationInterface $redirect_destination)

Sets the redirect destination service.

Parameters

RedirectDestinationInterface $redirect_destination

The redirect destination service.

Return Value

$this

protected TranslatableMarkup t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

static create(ContainerInterface $container)

Instantiates a new instance of this class.

This is a factory method that returns a new instance of this class. The factory should pass any needed dependencies into the constructor of this class, but not the container itself. Every call to this method must return a new instance of this class; that is, it may not implement a singleton.

Parameters

ContainerInterface $container

The service container this instance should use.

validateForm(array $form, FormStateInterface $form_state)

Form validation handler.

Parameters

array $form

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form.

protected ImmutableConfig config(string $name)

Retrieves a configuration object.

This is the main entry point to the configuration API. Calling

Parameters

string $name

The name of the configuration object to retrieve. The name corresponds to a configuration file. For @code \Drupal::config('book.admin') @endcode, the config object returned will contain the contents of book.admin configuration file.

Return Value

ImmutableConfig

A configuration object.

protected ConfigFactoryInterface configFactory()

Gets the config factory for this form.

When accessing configuration values, use $this->config(). Only use this when the config factory needs to be manipulated directly.

Return Value

ConfigFactoryInterface

$this setConfigFactory(ConfigFactoryInterface $config_factory)

Sets the config factory for this form.

Parameters

ConfigFactoryInterface $config_factory

The config factory.

Return Value

$this

resetConfigFactory()

Resets the configuration factory.

protected Request getRequest()

Gets the request object.

Return Value

Request

The request object.

protected RouteMatchInterface getRouteMatch()

Gets the route match.

Return Value

RouteMatchInterface

$this setRequestStack(RequestStack $request_stack)

Sets the request stack object to use.

Parameters

RequestStack $request_stack

The request stack object.

Return Value

$this

protected AccountInterface currentUser()

Gets the current user.

Return Value

AccountInterface

The current user.

protected RedirectResponse redirect(string $route_name, array $route_parameters = [], array $options = [], int $status = 302)

Returns a redirect response object for the specified route.

Parameters

string $route_name

The name of the route to which to redirect.

array $route_parameters

(optional) Parameters for the route.

array $options

(optional) An associative array of additional options.

int $status

(optional) The HTTP redirect status code for the redirect. The default is 302 Found.

Return Value

RedirectResponse

A redirect response object that may be returned by the controller.

protected LoggerInterface logger(string $channel)

Gets the logger for a specific channel.

This method exists for backward-compatibility between FormBase and LoggerChannelTrait. Use LoggerChannelTrait::getLogger() instead.

Parameters

string $channel

The name of the channel. Can be any string, but the general practice is to use the name of the subsystem calling this.

Return Value

LoggerInterface

The logger for the given channel.

__construct(EntityTypeManagerInterface $entity_type_manager, MediaLibraryUiBuilder $library_ui_builder, ElementInfoManagerInterface $element_info, RendererInterface $renderer, FileSystemInterface $file_system, OpenerResolverInterface $opener_resolver, FileUsageInterface $file_usage, FileRepositoryInterface $file_repository = NULL)

Constructs a new FileUploadForm.

Parameters

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

MediaLibraryUiBuilder $library_ui_builder

The media library UI builder.

ElementInfoManagerInterface $element_info

The element info manager.

RendererInterface $renderer

The renderer service.

FileSystemInterface $file_system

The file system service.

OpenerResolverInterface $opener_resolver

The opener resolver.

FileUsageInterface $file_usage

The file usage service.

FileRepositoryInterface $file_repository

The file repository service.

string|null getBaseFormId()

Returns a string identifying the base form.

Return Value

string|null

The string identifying the base form or NULL if this is not a base form.

protected MediaTypeInterface getMediaType(FormStateInterface $form_state)

Get the media type from the form state.

Parameters

FormStateInterface $form_state

The current form state.

Return Value

MediaTypeInterface

The media type.

Exceptions

InvalidArgumentException

array buildForm(array $form, FormStateInterface $form_state)

Form constructor.

Parameters

array $form

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form.

Return Value

array

The form structure.

protected array buildInputElement(array $form, FormStateInterface $form_state)

Builds the element for submitting source field value(s).

The input element needs to have a submit handler to create media items from the user input and store them in the form state using ::processInputValues().

Parameters

array $form

The complete form.

FormStateInterface $form_state

The current form state.

Return Value

array

The complete form, with the element added.

protected array buildEntityFormElement(MediaInterface $media, array $form, FormStateInterface $form_state, int $delta)

Builds the sub-form for setting required fields on a new media item.

Parameters

MediaInterface $media

A new, unsaved media item.

array $form

The complete form.

FormStateInterface $form_state

The current form state.

int $delta

The delta of the media item.

Return Value

array

The element containing the required fields sub-form.

static string[] trustedCallbacks()

Lists the trusted callbacks provided by the implementing class.

Trusted callbacks are public methods on the implementing class and can be invoked via \Drupal\Core\Security\DoTrustedCallbackTrait::doTrustedCallback().

Return Value

string[]

List of method names implemented by the class that can be used as trusted callbacks.

array preRenderAddedMedia(array $element)

Converts the set of newly added media into an item list for rendering.

Parameters

array $element

The render element to transform.

Return Value

array

The transformed render element.

protected array buildCurrentSelectionArea(array $form, FormStateInterface $form_state)

Returns a render array containing the current selection.

Parameters

array $form

The complete form.

FormStateInterface $form_state

The current form state.

Return Value

array

A render array containing the current selection.

protected array buildSelectedItemElement(MediaInterface $media, array $form, FormStateInterface $form_state)

Returns a render array for a single pre-selected media item.

Parameters

MediaInterface $media

The media item.

array $form

The complete form.

FormStateInterface $form_state

The current form state.

Return Value

array

A render array of a pre-selected media item.

protected array buildActions(array $form, FormStateInterface $form_state)

Returns an array of supported actions for the form.

Parameters

array $form

The complete form.

FormStateInterface $form_state

The current form state.

Return Value

array

An actions element containing the actions of the form.

protected processInputValues(array $source_field_values, array $form, FormStateInterface $form_state)

Creates media items from source field input values.

Parameters

array $source_field_values

The values for source fields of the media items.

array $form

The complete form.

FormStateInterface $form_state

The current form state.

protected MediaInterface createMediaFromValue(MediaTypeInterface $media_type, EntityStorageInterface $media_storage, string $source_field_name, $file)

Creates a new, unsaved media item from a source field value.

Parameters

MediaTypeInterface $media_type

The media type of the media item.

EntityStorageInterface $media_storage

The media storage.

string $source_field_name

The name of the media type's source field.

$file

Return Value

MediaInterface

An unsaved media entity.

protected prepareMediaEntityForSave(MediaInterface $media)

Prepares a created media item to be permanently saved.

Parameters

MediaInterface $media

The unsaved media item.

removeButtonSubmit(array $form, FormStateInterface $form_state)

Submit handler for the remove button.

Parameters

array $form

The form render array.

FormStateInterface $form_state

The form state.

AjaxResponse|array updateFormCallback(array $form, FormStateInterface $form_state)

AJAX callback to update the entire form based on source field input.

Parameters

array $form

The complete form.

FormStateInterface $form_state

The current form state.

Return Value

AjaxResponse|array

The form render array or an AJAX response object.

protected validateMediaEntity(MediaInterface $media, array $form, FormStateInterface $form_state, int $delta)

Validate a created media item.

Parameters

MediaInterface $media

The media item to validate.

array $form

The complete form.

FormStateInterface $form_state

The current form state.

int $delta

The delta of the media item.

submitForm(array $form, FormStateInterface $form_state)

Form submission handler.

Parameters

array $form

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form.

array|AjaxResponse updateLibrary(array $form, FormStateInterface $form_state)

AJAX callback to send the new media item(s) to the media library.

Parameters

array $form

The complete form.

FormStateInterface $form_state

The current form state.

Return Value

array|AjaxResponse

The form array if there are validation errors, or an AJAX response to add the created items to the current selection.

protected array buildMediaLibraryUi(FormStateInterface $form_state)

Build the render array of the media library UI.

Parameters

FormStateInterface $form_state

The current form state.

Return Value

array

The render array for the media library.

array|AjaxResponse updateWidget(array $form, FormStateInterface $form_state)

AJAX callback to send the new media item(s) to the calling code.

Parameters

array $form

The complete form.

FormStateInterface $form_state

The current form state.

Return Value

array|AjaxResponse

The form array when there are form errors or an AJAX response to select the created items in the media library.

protected MediaLibraryState getMediaLibraryState(FormStateInterface $form_state)

Get the media library state from the form state.

Parameters

FormStateInterface $form_state

The current form state.

Return Value

MediaLibraryState

The media library state.

Exceptions

InvalidArgumentException

protected string getSourceFieldName(MediaTypeInterface $media_type)

Returns the name of the source field for a media type.

Parameters

MediaTypeInterface $media_type

The media type to get the source field name for.

Return Value

string

The name of the media type's source field.

protected MediaInterface[] getPreSelectedMediaItems(FormStateInterface $form_state)

Get all pre-selected media items from the form state.

Parameters

FormStateInterface $form_state

The current form state.

Return Value

MediaInterface[]

An array containing the pre-selected media items keyed by ID.

protected MediaInterface[] getAddedMediaItems(FormStateInterface $form_state)

Get all added media items from the form state.

Parameters

FormStateInterface $form_state

The current form state.

Return Value

MediaInterface[]

An array containing the added media items keyed by delta. The media items won't have an ID until they are saved in ::submitForm().

protected MediaInterface[] getCurrentMediaItems(FormStateInterface $form_state)

Get all pre-selected and added media items from the form state.

Parameters

FormStateInterface $form_state

The current form state.

Return Value

MediaInterface[]

An array containing all pre-selected and added media items with renumbered numeric keys.

protected bool isAdvancedUi()

Determines if the "advanced UI" of the Media Library is enabled.

This exposes additional features that are useful to power users.

Return Value

bool

TRUE if the advanced UI is enabled, FALSE otherwise.

See also

\Drupal\media_library\Form\::buildActions()
\Drupal\media_library\Form\::buildCurrentSelectionArea()

string getFormId()

Returns a unique string identifying the form.

The returned ID should be a unique string that can be a valid PHP function name, since it's used in hook implementation names such as hook_form_FORM_ID_alter().

Return Value

string

The unique string identifying the form.

array validateUploadElement(array $element, FormStateInterface $form_state)

Validates the upload element.

Parameters

array $element

The upload element.

FormStateInterface $form_state

The form state.

Return Value

array

The processed upload element.

array processUploadElement(array $element, FormStateInterface $form_state)

Processes an upload (managed_file) element.

Parameters

array $element

The upload element.

FormStateInterface $form_state

The form state.

Return Value

array

The processed upload element.

static array hideExtraSourceFieldComponents(array $element, FormStateInterface $form_state, $form)

Processes an image or file source field element.

Parameters

array $element

The entity form source field element.

FormStateInterface $form_state

The current form state.

$form

The complete form.

Return Value

array

The processed form element.

uploadButtonSubmit(array $form, FormStateInterface $form_state)

Submit handler for the upload button, inside the managed_file element.

Parameters

array $form

The form render array.

FormStateInterface $form_state

The form state.

protected FileItem createFileItem(MediaTypeInterface $media_type)

Create a file field item.

Parameters

MediaTypeInterface $media_type

The media type of the media item.

Return Value

FileItem

A created file item.