class MediaElementConfigForm in MediaElement 8
Configuration form for MediaElement.js module.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\mediaelement\Form\MediaElementConfigForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of MediaElementConfigForm
1 string reference to 'MediaElementConfigForm'
File
- src/
Form/ MediaElementConfigForm.php, line 13
Namespace
Drupal\mediaelement\FormView source
class MediaElementConfigForm extends ConfigFormBase {
/**
* The URL to CDNJS's API.
*
* @var string
*/
protected $cndjsUrl = 'https://api.cdnjs.com/libraries/mediaelement';
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'mediaelement_config';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'mediaelement.settings',
];
}
/**
* Get the results from the CDN.
*
* @param array $params
* Optional query paramaters to pass onto the URL.
*
* @return object
* The data from the API.
*/
protected function getApiData($params = []) {
// @todo: Use dependency injection for this.
$client = \Drupal::httpClient();
$url = empty($params) ? $this->cndjsUrl : $this->cndjsUrl . '?' . http_build_query($params);
try {
$res = $client
->get($url);
return json_decode($res
->getBody());
} catch (RequestException $e) {
watchdog_exception('mediaelement', $e
->getMessage());
}
}
/**
* Gets the list of available version numbers for the library.
*
* @return string[]
* The array of version strings.
*/
protected function getVersionList() {
$data = $this
->getApiData([
'fields' => 'assets',
]);
return array_map(function ($asset) {
return $asset->version;
}, $data->assets);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('mediaelement.settings');
// Create configurations for where the library is loaded from.
$library_config = $config
->get('library_settings');
$form['library_settings'] = [
'#type' => 'details',
'#title' => $this
->t('Library Settings'),
'#open' => TRUE,
];
$form['library_settings']['library_source'] = [
'#type' => 'radios',
'#title' => $this
->t('Library Source'),
'#options' => [
'local' => $this
->t('Local Download'),
'cdnjs' => $this
->t('CDN (Provided by CDNJS.com)'),
],
'#default_value' => $library_config['library_source'] ?? 'local',
'#required' => TRUE,
];
$form['library_settings']['cdnjs_settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('CDNJS Settings'),
'#states' => [
'visible' => [
':input[name="library_source"]' => [
'value' => 'cdnjs',
],
],
],
];
$version_options = $this
->getVersionList();
$form['library_settings']['cdnjs_settings']['library_version'] = [
'#type' => 'select',
'#title' => $this
->t('Library Version'),
'#options' => array_combine($version_options, $version_options),
'#default_value' => $library_config['cdnjs_settings']['library_version'] ?? $version_options[0],
];
// Global configuration items for player functionality.
$global_config = $config
->get('global_settings');
$form['global_settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Global Settings'),
];
$form['global_settings']['attach_sitewide'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable MediaElement.js site wide'),
'#description' => $this
->t('Attach the MediaElement.js library throughtout the entire site. Any <code>audio</code> or <code>video</code> HTML tag will have the player applied to them.'),
'#default_value' => $global_config['attach_sitewide'] ?? FALSE,
'#weight' => 0,
];
// Configuration that applies to all types of players.
$player_config = $config
->get('global_settings.player_settings');
$form['global_settings']['player_settings'] = [
'#type' => 'details',
'#title' => $this
->t('General Player Settings'),
'#weight' => 1,
];
$form['global_settings']['player_settings']['class_prefix'] = [
'#type' => 'textfield',
'#title' => $this
->t('Class Prefix'),
'#description' => $this
->t('Class prefix for player elements.'),
'#default_value' => $player_config['class_prefix'] ?? '',
'#placeholder' => 'mejs__',
];
$form['global_settings']['player_settings']['set_dimensions'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Set Dimensions'),
'#description' => $this
->t('Set dimensions via JS instead of CSS.'),
'#default_value' => $player_config['set_dimensions'] ?? TRUE,
];
// Configuration for video players.
$video_settings = $config
->get('global_settings.video_settings');
$form['global_settings']['video_settings'] = [
'#type' => 'details',
'#title' => $this
->t('Video Player Settings'),
'#weight' => 2,
];
$form['global_settings']['video_settings']['default_video_width'] = [
'#type' => 'number',
'#title' => $this
->t('Default Video Width'),
'#description' => $this
->t('Default width if the <code><video></code> width is not specified'),
'#default_value' => $video_settings['default_video_width'] ?? '',
'#placeholder' => '480',
];
$form['global_settings']['video_settings']['default_video_height'] = [
'#type' => 'number',
'#title' => $this
->t('Default Video Height'),
'#description' => $this
->t('Default width if the <code><video></code> height is not specified'),
'#default_value' => $video_settings['default_video_height'] ?? '',
'#placeholder' => '270',
];
$form['global_settings']['video_settings']['video_width'] = [
'#type' => 'number',
'#title' => $this
->t('Video Width'),
'#description' => $this
->t('If set, overrides <code><video></code> width'),
'#default_value' => $video_settings['video_width'] ?? '',
'#placeholder' => '-1',
];
$form['global_settings']['video_settings']['video_height'] = [
'#type' => 'number',
'#title' => $this
->t('Video Height'),
'#description' => $this
->t('If set, overrides <code><video></code> height'),
'#default_value' => $video_settings['video_height'] ?? '',
'#placeholder' => '-1',
];
// Configuration for audio players.
$audio_settings = $config
->get('global_settings.audio_settings');
$form['global_settings']['audio_settings'] = [
'#type' => 'details',
'#title' => $this
->t('Audio Player Settings'),
'#weight' => 3,
];
$form['global_settings']['audio_settings']['default_audio_width'] = [
'#type' => 'number',
'#title' => $this
->t('Default Audio Width'),
'#description' => $this
->t('Default width if the <code><audio></code> width is not specified'),
'#default_value' => $audio_settings['default_audio_width'] ?? '',
'#placeholder' => '400',
];
$form['global_settings']['audio_settings']['default_audio_height'] = [
'#type' => 'number',
'#title' => $this
->t('Default Audio Height'),
'#description' => $this
->t('Default width if the <code><audio></code> height is not specified'),
'#default_value' => $audio_settings['default_audio_height'] ?? '',
'#placeholder' => '30',
];
$form['global_settings']['audio_settings']['audio_width'] = [
'#type' => 'number',
'#title' => $this
->t('Audio Width'),
'#description' => $this
->t('If set, overrides <code><audio></code> width'),
'#default_value' => $audio_settings['audio_width'] ?? '',
'#placeholder' => '-1',
];
$form['global_settings']['audio_settings']['audio_height'] = [
'#type' => 'number',
'#title' => $this
->t('Audio Height'),
'#description' => $this
->t('If set, overrides <code><audio></code> height'),
'#default_value' => $audio_settings['audio_height'] ?? '',
'#placeholder' => '-1',
];
$api_link = Link::fromTextAndUrl($this
->t('API Documentation'), Url::fromUri('https://github.com/mediaelement/mediaelement/blob/master/docs/api.md#mediaelementplayer'));
$form['global_settings']['api_link'] = [
'#markup' => $this
->t('<small>For a full explaination of configuration options, see the @api_link.</small>', [
'@api_link' => $api_link
->toString(),
]),
'#weight' => 10,
];
return parent::buildForm($form, $form_state);
}
/**
* Prepare our configuration items for saving.
*
* @param array $fields
* The field names we want to parse.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The submitted form state.
*
* @return array
* The settings to save keyed by their name.
*/
private function getConfigurationValues(array $fields, FormStateInterface $form_state) {
$values = [];
foreach ($fields as $field) {
$value = $form_state
->getValue($field);
if (!empty($value) || $value === 0) {
$values[$field] = $value;
}
}
return $values;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory
->getEditable('mediaelement.settings');
$library_settings = [];
$library_settings['library_source'] = $form_state
->getValue('library_source');
// If connecting to a CDN, save their settings.
if ($library_settings['library_source'] != 'local') {
$cdn_name = $library_settings['library_source'];
$cdn_settings_fields = [
'library_version',
];
$library_settings["{$cdn_name}_settings"] = $this
->getConfigurationValues($cdn_settings_fields, $form_state);
}
$config
->set('library_settings', $library_settings);
$global_settings = [];
$global_settings['attach_sitewide'] = $form_state
->getValue('attach_sitewide');
$player_settings_fields = [
'class_prefix',
'set_dimensions',
];
$global_settings['player_settings'] = $this
->getConfigurationValues($player_settings_fields, $form_state);
$video_settings_fields = [
'default_video_width',
'default_video_height',
'video_width',
'video_height',
];
$global_settings['video_settings'] = $this
->getConfigurationValues($video_settings_fields, $form_state);
$audio_settings_fields = [
'default_audio_width',
'default_audio_height',
'audio_width',
'audio_height',
];
$global_settings['audio_settings'] = $this
->getConfigurationValues($audio_settings_fields, $form_state);
$config
->set('global_settings', $global_settings);
$config
->save();
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
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 | 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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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. | |
MediaElementConfigForm:: |
protected | property | The URL to CDNJS's API. | |
MediaElementConfigForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
MediaElementConfigForm:: |
protected | function | Get the results from the CDN. | |
MediaElementConfigForm:: |
private | function | Prepare our configuration items for saving. | |
MediaElementConfigForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
MediaElementConfigForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MediaElementConfigForm:: |
protected | function | Gets the list of available version numbers for the library. | |
MediaElementConfigForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
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. | |
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. |