class OEmbedForm in Drupal 8
Same name and namespace in other branches
- 9 core/modules/media_library/src/Form/OEmbedForm.php \Drupal\media_library\Form\OEmbedForm
Creates a form to create media entities from oEmbed URLs.
@internal Form classes are internal.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\media_library\Form\AddFormBase implements BaseFormIdInterface, TrustedCallbackInterface
- class \Drupal\media_library\Form\OEmbedForm
- class \Drupal\media_library\Form\AddFormBase implements BaseFormIdInterface, TrustedCallbackInterface
Expanded class hierarchy of OEmbedForm
2 files declare their use of OEmbedForm
- MediaLibraryAddFormTest.php in core/
modules/ media_library/ tests/ src/ Kernel/ MediaLibraryAddFormTest.php - media_library.module in core/
modules/ media_library/ media_library.module - Contains hook implementations for the media_library module.
File
- core/
modules/ media_library/ src/ Form/ OEmbedForm.php, line 23
Namespace
Drupal\media_library\FormView source
class OEmbedForm extends AddFormBase {
/**
* The oEmbed URL resolver service.
*
* @var \Drupal\media\OEmbed\UrlResolverInterface
*/
protected $urlResolver;
/**
* The oEmbed resource fetcher service.
*
* @var \Drupal\media\OEmbed\ResourceFetcherInterface
*/
protected $resourceFetcher;
/**
* Constructs a new OEmbedForm.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\media_library\MediaLibraryUiBuilder $library_ui_builder
* The media library UI builder.
* @param \Drupal\media\OEmbed\UrlResolverInterface $url_resolver
* The oEmbed URL resolver service.
* @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher
* The oEmbed resource fetcher service.
* @param \Drupal\media_library\OpenerResolverInterface $opener_resolver
* The opener resolver.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, MediaLibraryUiBuilder $library_ui_builder, UrlResolverInterface $url_resolver, ResourceFetcherInterface $resource_fetcher, OpenerResolverInterface $opener_resolver = NULL) {
parent::__construct($entity_type_manager, $library_ui_builder, $opener_resolver);
$this->urlResolver = $url_resolver;
$this->resourceFetcher = $resource_fetcher;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('media_library.ui_builder'), $container
->get('media.oembed.url_resolver'), $container
->get('media.oembed.resource_fetcher'), $container
->get('media_library.opener_resolver'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return $this
->getBaseFormId() . '_oembed';
}
/**
* {@inheritdoc}
*/
protected function getMediaType(FormStateInterface $form_state) {
if ($this->mediaType) {
return $this->mediaType;
}
$media_type = parent::getMediaType($form_state);
if (!$media_type
->getSource() instanceof OEmbedInterface) {
throw new \InvalidArgumentException('Can only add media types which use an oEmbed source plugin.');
}
return $media_type;
}
/**
* {@inheritdoc}
*/
protected function buildInputElement(array $form, FormStateInterface $form_state) {
$media_type = $this
->getMediaType($form_state);
$providers = $media_type
->getSource()
->getProviders();
// Add a container to group the input elements for styling purposes.
$form['container'] = [
'#type' => 'container',
];
$form['container']['url'] = [
'#type' => 'url',
'#title' => $this
->t('Add @type via URL', [
'@type' => $this
->getMediaType($form_state)
->label(),
]),
'#description' => $this
->t('Allowed providers: @providers.', [
'@providers' => implode(', ', $providers),
]),
'#required' => TRUE,
'#attributes' => [
'placeholder' => 'https://',
],
];
$form['container']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Add'),
'#button_type' => 'primary',
'#validate' => [
'::validateUrl',
],
'#submit' => [
'::addButtonSubmit',
],
// @todo Move validation in https://www.drupal.org/node/2988215
'#ajax' => [
'callback' => '::updateFormCallback',
'wrapper' => 'media-library-wrapper',
// Add a fixed URL to post the form since AJAX forms are automatically
// posted to <current> instead of $form['#action'].
// @todo Remove when https://www.drupal.org/project/drupal/issues/2504115
// is fixed.
'url' => Url::fromRoute('media_library.ui'),
'options' => [
'query' => $this
->getMediaLibraryState($form_state)
->all() + [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
],
],
];
return $form;
}
/**
* Validates the oEmbed URL.
*
* @param array $form
* The complete form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*/
public function validateUrl(array &$form, FormStateInterface $form_state) {
$url = $form_state
->getValue('url');
if ($url) {
try {
$resource_url = $this->urlResolver
->getResourceUrl($url);
$this->resourceFetcher
->fetchResource($resource_url);
} catch (ResourceException $e) {
$form_state
->setErrorByName('url', $e
->getMessage());
}
}
}
/**
* Submit handler for the add button.
*
* @param array $form
* The form render array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function addButtonSubmit(array $form, FormStateInterface $form_state) {
$this
->processInputValues([
$form_state
->getValue('url'),
], $form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddFormBase:: |
protected | property | The entity type manager. | |
AddFormBase:: |
protected | property | The media library UI builder. | |
AddFormBase:: |
protected | property | The type of media items being created by this form. | |
AddFormBase:: |
protected | property | The opener resolver. | |
AddFormBase:: |
protected | property | The media view builder. | |
AddFormBase:: |
protected | function | Returns an array of supported actions for the form. | |
AddFormBase:: |
protected | function | Returns a render array containing the current selection. | |
AddFormBase:: |
protected | function | Builds the sub-form for setting required fields on a new media item. | 1 |
AddFormBase:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
AddFormBase:: |
protected | function | Build the render array of the media library UI. | |
AddFormBase:: |
protected | function | Returns a render array for a single pre-selected media item. | |
AddFormBase:: |
protected | function | Creates a new, unsaved media item from a source field value. | 1 |
AddFormBase:: |
protected | function | Get all added media items from the form state. | |
AddFormBase:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
|
AddFormBase:: |
protected | function | Get all pre-selected and added media items from the form state. | |
AddFormBase:: |
protected | function | Get the media library state from the form state. | |
AddFormBase:: |
protected | function | Get all pre-selected media items from the form state. | |
AddFormBase:: |
protected | function | Returns the name of the source field for a media type. | |
AddFormBase:: |
protected | function | Determines if the "advanced UI" of the Media Library is enabled. | |
AddFormBase:: |
protected | function | Prepares a created media item to be permanently saved. | 1 |
AddFormBase:: |
public | function | Converts the set of newly added media into an item list for rendering. | |
AddFormBase:: |
protected | function | Creates media items from source field input values. | |
AddFormBase:: |
public | function | Submit handler for the remove button. | 1 |
AddFormBase:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
AddFormBase:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
|
AddFormBase:: |
public | function | AJAX callback to update the entire form based on source field input. | |
AddFormBase:: |
public | function | AJAX callback to send the new media item(s) to the media library. | |
AddFormBase:: |
public | function | AJAX callback to send the new media item(s) to the calling code. | |
AddFormBase:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
AddFormBase:: |
protected | function | Validate a created media item. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OEmbedForm:: |
protected | property | The oEmbed resource fetcher service. | |
OEmbedForm:: |
protected | property | The oEmbed URL resolver service. | |
OEmbedForm:: |
public | function | Submit handler for the add button. | |
OEmbedForm:: |
protected | function |
Builds the element for submitting source field value(s). Overrides AddFormBase:: |
|
OEmbedForm:: |
public static | function |
Instantiates a new instance of this class. Overrides AddFormBase:: |
|
OEmbedForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
OEmbedForm:: |
protected | function |
Get the media type from the form state. Overrides AddFormBase:: |
|
OEmbedForm:: |
public | function | Validates the oEmbed URL. | |
OEmbedForm:: |
public | function |
Constructs a new OEmbedForm. Overrides AddFormBase:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. | ||
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |