class VideoEmbedWidget in Video 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldWidget/VideoEmbedWidget.php \Drupal\video\Plugin\Field\FieldWidget\VideoEmbedWidget
Plugin implementation of the 'video_embed' widget.
Plugin annotation
@FieldWidget(
id = "video_embed",
label = @Translation("Video Embed"),
field_types = {
"video"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface uses AllowedTagsXssTrait
- class \Drupal\file\Plugin\Field\FieldWidget\FileWidget implements ContainerFactoryPluginInterface
- class \Drupal\video\Plugin\Field\FieldWidget\VideoEmbedWidget
- class \Drupal\file\Plugin\Field\FieldWidget\FileWidget implements ContainerFactoryPluginInterface
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface uses AllowedTagsXssTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of VideoEmbedWidget
File
- src/
Plugin/ Field/ FieldWidget/ VideoEmbedWidget.php, line 36
Namespace
Drupal\video\Plugin\Field\FieldWidgetView source
class VideoEmbedWidget extends FileWidget {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings = [
'file_directory' => 'video-thumbnails/[date:custom:Y]-[date:custom:m]',
'allowed_providers' => [
"youtube" => "youtube",
],
'uri_scheme' => 'public',
];
return $settings;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$settings = $this
->getSettings();
$provider_manager = \Drupal::service('video.provider_manager');
$element['allowed_providers'] = [
'#title' => t('Video Providers'),
'#type' => 'checkboxes',
'#default_value' => $this
->getSetting('allowed_providers'),
'#options' => $provider_manager
->getProvidersOptionList(),
];
$element['file_directory'] = [
'#type' => 'textfield',
'#title' => t('Thumbnail directory'),
'#default_value' => $settings['file_directory'],
'#description' => t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
'#element_validate' => [
[
get_class($this),
'validateDirectory',
],
],
'#weight' => 3,
];
$scheme_options = \Drupal::service('stream_wrapper_manager')
->getNames(StreamWrapperInterface::WRITE_VISIBLE);
$element['uri_scheme'] = [
'#type' => 'radios',
'#title' => t('Thumbnail destination'),
'#options' => $scheme_options,
'#default_value' => $this
->getSetting('uri_scheme'),
'#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'),
'#weight' => 6,
];
return $element;
}
/**
* Form API callback
*
* Removes slashes from the beginning and end of the destination value and
* ensures that the file directory path is not included at the beginning of the
* value.
*
* This function is assigned as an #element_validate callback in
* settingsForm().
*/
public static function validateDirectory($element, FormStateInterface $form_state) {
// Strip slashes from the beginning and end of $element['file_directory'].
$value = trim($element['#value'], '\\/');
$form_state
->setValueForElement($element, $value);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = t('Providers : @allowed_providers<br/>Thumbnail directory : @file_directory', [
'@allowed_providers' => implode(', ', array_filter($this
->getSetting('allowed_providers'))),
'@file_directory' => $this
->getSetting('uri_scheme') . '://' . $this
->getSetting('file_directory'),
]);
return $summary;
}
/**
* {@inheritdoc}
*/
public function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition
->getName();
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
$parents = $form['#parents'];
// Load the items for form rebuilds from the field state as they might not
// be in $form_state->getValues() because of validation limitations. Also,
// they are only passed in as $items when editing existing entities.
$field_state = static::getWidgetState($parents, $field_name, $form_state);
if (isset($field_state['items'])) {
$items
->setValue($field_state['items']);
}
// Determine the number of widgets to display.
switch ($cardinality) {
case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$max = $field_state['items_count'];
$is_multiple = TRUE;
break;
default:
$max = $cardinality - 1;
$is_multiple = $cardinality > 1;
break;
}
$title = $this->fieldDefinition
->getLabel();
$description = FieldFilteredMarkup::create(\Drupal::token()
->replace($this->fieldDefinition
->getDescription()));
$elements = [];
for ($delta = 0; $delta <= $max; $delta++) {
// Add a new empty item if it doesn't exist yet at this delta.
if (!isset($items[$delta])) {
$items
->appendItem();
}
// For multiple fields, title and description are handled by the wrapping
// table.
if ($is_multiple) {
$element = [
'#title' => $this
->t('@title (value @number)', [
'@title' => $title,
'@number' => $delta + 1,
]),
'#title_display' => 'invisible',
'#description' => '',
];
}
else {
$element = [
'#title' => $title,
'#title_display' => 'before',
'#description' => $description,
];
}
$element = $this
->formSingleElement($items, $delta, $element, $form, $form_state);
if ($element) {
// Input field for the delta (drag-n-drop reordering).
if ($is_multiple) {
// We name the element '_weight' to avoid clashing with elements
// defined by widget.
$element['_weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for row @number', [
'@number' => $delta + 1,
]),
'#title_display' => 'invisible',
// Note: this 'delta' is the FAPI #type 'weight' element's property.
'#delta' => $max,
'#default_value' => $items[$delta]->_weight ?: $delta,
'#weight' => 100,
];
}
$elements[$delta] = $element;
}
}
if ($elements) {
$elements += [
'#theme' => 'field_multiple_value_form',
'#field_name' => $field_name,
'#cardinality' => $cardinality,
'#cardinality_multiple' => $this->fieldDefinition
->getFieldStorageDefinition()
->isMultiple(),
'#required' => $this->fieldDefinition
->isRequired(),
'#title' => $title,
'#description' => $description,
'#max_delta' => $max,
];
// Add 'add more' button, if not working with a programmed form.
if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && !$form_state
->isProgrammed()) {
$id_prefix = implode('-', array_merge($parents, [
$field_name,
]));
$wrapper_id = Html::getUniqueId($id_prefix . '-add-more-wrapper');
// $elements['#prefix'] = '<div id="' . $wrapper_id . '">';
// $elements['#suffix'] = '</div>';
$elements['add_more'] = [
'#type' => 'submit',
'#name' => strtr($id_prefix, '-', '_') . '_add_more',
'#value' => t('Add another item'),
'#attributes' => [
'class' => [
'field-add-more-submit',
],
],
'#limit_validation_errors' => [
array_merge($parents, [
$field_name,
]),
],
'#submit' => [
[
get_class($this),
'addMoreSubmit',
],
],
'#ajax' => [
'callback' => [
get_class($this),
'addMoreAjax',
],
'effect' => 'fade',
],
'#weight' => 1000,
];
}
}
if ($is_multiple) {
// The group of elements all-together need some extra functionality after
// building up the full list (like draggable table rows).
$elements['#file_upload_delta'] = $delta;
$elements['#process'] = [
[
get_class($this),
'processMultiple',
],
];
$elements['#field_name'] = $field_name;
$elements['#language'] = $items
->getLangcode();
}
return $elements;
}
/**
* Form API callback: Processes a group of file_generic field elements.
*
* Adds the weight field to each row so it can be ordered and adds a new Ajax
* wrapper around the entire group so it can be replaced all at once.
*
* This method on is assigned as a #process callback in formMultipleElements()
* method.
*/
public static function processMultiple($element, FormStateInterface $form_state, $form) {
$element_children = Element::children($element, TRUE);
$count = count($element_children);
// Count the number of already uploaded files, in order to display new
// items in \Drupal\file\Element\ManagedFile::uploadAjaxCallback().
if (!$form_state
->isRebuilding()) {
$count_items_before = 0;
foreach ($element_children as $children) {
if (!empty($element[$children]['#default_value']['fids'])) {
$count_items_before++;
}
}
$form_state
->set('file_upload_delta_initial', $count_items_before);
}
foreach ($element_children as $delta => $key) {
if ($delta != $element['#file_upload_delta']) {
$description = static::getDescriptionFromElement($element[$key]);
$element[$key]['_weight'] = [
'#type' => 'weight',
'#title' => $description ? t('Weight for @title', [
'@title' => $description,
]) : t('Weight for new file'),
'#title_display' => 'invisible',
'#delta' => $count,
'#default_value' => $delta,
];
}
else {
// The title needs to be assigned to the upload field so that validation
// errors include the correct widget label.
$element[$key]['#title'] = $element['#title'];
$element[$key]['_weight'] = [
'#type' => 'hidden',
'#default_value' => $delta,
];
}
}
// Add a new wrapper around all the elements for Ajax replacement.
$element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
$element['#suffix'] = '</div>';
$element['add_more']['#ajax']['wrapper'] = $element['#id'] . '-ajax-wrapper';
return $element;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
if (isset(NestedArray::getValue($form_state
->getUserInput(), $element['#field_parents'])[$items
->getName()][0]['value'])) {
$value = NestedArray::getValue($form_state
->getUserInput(), $element['#field_parents'])[$items
->getName()][0]['value'];
}
if (empty($items[$delta]
->getValue()) || !empty($value)) {
$element['value'] = $element + [
'#type' => 'textfield',
'#attributes' => [
'class' => [
'js-text-full',
'text-full',
],
],
'#default_value' => empty($value) ? '' : $value,
'#element_validate' => [
[
get_class($this),
'validateFormElement',
],
],
'#allowed_providers' => $this
->getSetting('allowed_providers'),
];
}
else {
$element += parent::formElement($items, $delta, $element, $form, $form_state);
}
return $element;
}
/**
* Form API callback: Processes a file_generic field element.
*
* Expands the file_generic type to include the description and display
* fields.
*
* This method is assigned as a #process callback in formElement() method.
*/
public static function process($element, FormStateInterface $form_state, $form) {
$element = parent::process($element, $form_state, $form);
$item = $element['#value'];
$element['data']['#value'] = $item['data'];
$element['data']['#type'] = 'hidden';
return $element;
}
/**
* Form element validation handler for URL alias form element.
*
* @param array $element
* The form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public static function validateFormElement(array &$element, FormStateInterface $form_state) {
$value = $element['#value'];
if (empty($value)) {
return;
}
$provider_manager = \Drupal::service('video.provider_manager');
$enabled_providers = $provider_manager
->loadDefinitionsFromOptionList($element['#allowed_providers']);
if (!$provider_manager
->loadApplicableDefinitionMatches($enabled_providers, $value)) {
$form_state
->setError($element, t('Could not find a video provider to handle the given URL.'));
}
}
/**
* {@inheritdoc}
*/
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition
->getName();
// Extract the values from $form_state->getValues().
$path = array_merge($form['#parents'], [
$field_name,
]);
$key_exists = NULL;
$values = NestedArray::getValue($form_state
->getValues(), $path, $key_exists);
if ($key_exists) {
// Account for drag-and-drop reordering if needed.
if (!$this
->handlesMultipleValues()) {
// Remove the 'value' of the 'add more' button.
unset($values['add_more']);
// The original delta, before drag-and-drop reordering, is needed to
// route errors to the correct form element.
foreach ($values as $delta => &$value) {
$value['_original_delta'] = $delta;
}
usort($values, function ($a, $b) {
return SortArray::sortByKeyInt($a, $b, '_weight');
});
}
// Let the widget massage the submitted values.
foreach ($values as $delta => &$value) {
if (!empty($value['value']) && empty($value['fids'])) {
// ready to save the file
$provider_manager = \Drupal::service('video.provider_manager');
$allowed_providers = $this
->getSetting('allowed_providers');
$enabled_providers = $provider_manager
->loadDefinitionsFromOptionList($allowed_providers);
if ($provider_matches = $provider_manager
->loadApplicableDefinitionMatches($enabled_providers, $value['value'])) {
$definition = $provider_matches['definition'];
$matches = $provider_matches['matches'];
$uri = $definition['stream_wrapper'] . '://' . $matches['id'];
$storage = \Drupal::entityManager()
->getStorage('file');
$results = $storage
->getQuery()
->condition('uri', $uri)
->execute();
if (!(count($results) > 0)) {
$user = \Drupal::currentUser();
$file = File::Create([
'uri' => $uri,
'filemime' => $definition['mimetype'],
'filesize' => 1,
'uid' => $user
->id(),
]);
$file
->save();
unset($values[$delta]);
$values[] = [
'fids' => [
$file
->id(),
],
'data' => serialize($matches),
];
}
else {
unset($values[$delta]);
$values[] = [
'fids' => [
reset($results),
],
'data' => serialize($matches),
];
}
}
}
if (!isset($value['fids'])) {
// If fids is still not set, remove this value, otherwise massageFormValues() will fail.
unset($values[$delta]);
}
}
$values = $this
->massageFormValues($values, $form, $form_state);
// Assign the values and remove the empty ones.
$items
->setValue($values);
$items
->filterEmptyItems();
// Put delta mapping in $form_state, so that flagErrors() can use it.
$field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
foreach ($items as $delta => $item) {
$field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
unset($item->_original_delta, $item->_weight);
}
static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AllowedTagsXssTrait:: |
public | function | Returns a list of tags allowed by AllowedTagsXssTrait::fieldFilterXss(). | |
AllowedTagsXssTrait:: |
public | function | Returns a human-readable list of allowed tags for display in help texts. | |
AllowedTagsXssTrait:: |
public | function | Filters an HTML string to prevent XSS vulnerabilities. | |
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 | |
FileWidget:: |
public static | function |
Creates an instance of the plugin. Overrides WidgetBase:: |
|
FileWidget:: |
public | function |
Reports field-level validation errors against actual form elements. Overrides WidgetBase:: |
|
FileWidget:: |
protected static | function | Retrieves the file description from a field field element. | |
FileWidget:: |
public | function |
Massages the form values into the format expected for field values. Overrides WidgetBase:: |
|
FileWidget:: |
public static | function | Form submission handler for upload/remove button of formElement(). | |
FileWidget:: |
public static | function | Form element validation callback for upload element on file widget. Checks if user has uploaded more files than allowed. | |
FileWidget:: |
public static | function | Form API callback. Retrieves the value for the file_generic field element. | |
FileWidget:: |
public | function |
Constructs a WidgetBase object. Overrides WidgetBase:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
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. | |
VideoEmbedWidget:: |
public static | function |
Defines the default settings for this plugin. Overrides FileWidget:: |
|
VideoEmbedWidget:: |
public | function |
Extracts field values from submitted form values. Overrides FileWidget:: |
|
VideoEmbedWidget:: |
public | function |
Returns the form for a single field widget. Overrides FileWidget:: |
|
VideoEmbedWidget:: |
public | function |
Overrides \Drupal\Core\Field\WidgetBase::formMultipleElements(). Overrides FileWidget:: |
|
VideoEmbedWidget:: |
public static | function |
Form API callback: Processes a file_generic field element. Overrides FileWidget:: |
|
VideoEmbedWidget:: |
public static | function |
Form API callback: Processes a group of file_generic field elements. Overrides FileWidget:: |
|
VideoEmbedWidget:: |
public | function |
Returns a form to configure settings for the widget. Overrides FileWidget:: |
|
VideoEmbedWidget:: |
public | function |
Returns a short summary for the current widget settings. Overrides FileWidget:: |
|
VideoEmbedWidget:: |
public static | function | Form API callback | |
VideoEmbedWidget:: |
public static | function | Form element validation handler for URL alias form element. | |
WidgetBase:: |
protected | property | The field definition. | |
WidgetBase:: |
protected | property |
The widget settings. Overrides PluginSettingsBase:: |
|
WidgetBase:: |
public static | function | Ajax callback for the "Add another item" button. | |
WidgetBase:: |
public static | function | Submission handler for the "Add another item" button. | |
WidgetBase:: |
public static | function | After-build handler for field elements in a form. | |
WidgetBase:: |
public | function |
Assigns a field-level validation error to the right widget sub-element. Overrides WidgetInterface:: |
8 |
WidgetBase:: |
public | function |
Creates a form element for a field. Overrides WidgetBaseInterface:: |
3 |
WidgetBase:: |
protected | function | Generates the form element for a single copy of the widget. | |
WidgetBase:: |
protected | function | Returns the value of a field setting. | |
WidgetBase:: |
protected | function | Returns the array of field settings. | |
WidgetBase:: |
protected | function | Returns the filtered field description. | |
WidgetBase:: |
public static | function |
Retrieves processing information about the widget from $form_state. Overrides WidgetBaseInterface:: |
|
WidgetBase:: |
protected static | function | Returns the location of processing information within $form_state. | |
WidgetBase:: |
protected | function | Returns whether the widget handles multiple values. | |
WidgetBase:: |
public static | function |
Returns if the widget can be used for the provided field. Overrides WidgetInterface:: |
4 |
WidgetBase:: |
protected | function | Returns whether the widget used for default value form. | |
WidgetBase:: |
public static | function |
Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface:: |