class CdnSettingsForm in CDN 8.3
Configure CDN settings for this site.
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\cdn_ui\Form\ValidatableConfigFormBase
- class \Drupal\cdn_ui\Form\CdnSettingsForm
- class \Drupal\cdn_ui\Form\ValidatableConfigFormBase
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of CdnSettingsForm
1 string reference to 'CdnSettingsForm'
- cdn_ui.routing.yml in cdn_ui/
cdn_ui.routing.yml - cdn_ui/cdn_ui.routing.yml
File
- cdn_ui/
src/ Form/ CdnSettingsForm.php, line 19
Namespace
Drupal\cdn_ui\FormView source
class CdnSettingsForm extends ValidatableConfigFormBase {
/**
* The 'nocssjs' conditions preset: all files except CSS and JS.
*
* @var array
*/
private const CONDITIONS_PRESET_NOCSSJS = [
'not' => [
'extensions' => [
'css',
'js',
],
],
];
/**
* The stream wrapper manager.
*
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
/**
* {@inheritdoc}
*/
public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager, StreamWrapperManagerInterface $streamWrapperManager) {
parent::__construct($config_factory, $typed_config_manager);
$this->streamWrapperManager = $streamWrapperManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('config.typed'), $container
->get('stream_wrapper_manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'cdn_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'cdn.settings',
];
}
/**
* {@inheritdoc}
*/
protected static function getMainConfigName() : string {
return 'cdn.settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('cdn.settings');
$form['cdn_settings'] = [
'#type' => 'vertical_tabs',
'#default_tab' => 'edit-mapping',
'#attached' => [
'library' => [
'cdn_ui/summaries',
],
],
];
$form['status'] = [
'#type' => 'details',
'#title' => $this
->t('Status'),
'#group' => 'cdn_settings',
];
$form['status']['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Serve files from CDN'),
'#description' => $this
->t('Better performance thanks to better caching of files by the visitor. When a file changes a different URL is used, to ensure instantaneous updates for your visitors.'),
'#default_value' => $config
->get('status'),
];
$form['mapping'] = [
'#type' => 'details',
'#title' => $this
->t('Mapping'),
'#group' => 'cdn_settings',
'#tree' => TRUE,
];
$mapping_type_ui_string = $this
->t('Use @mapping-type mapping');
[
$mapping_type_ui_string_prefix,
$mapping_type_ui_string_suffix,
] = explode('@mapping-type', (string) $mapping_type_ui_string, 2);
$form['mapping']['type'] = [
'#field_prefix' => $mapping_type_ui_string_prefix,
'#field_suffix' => $mapping_type_ui_string_suffix,
'#type' => 'select',
'#title' => $this
->t('Mapping type'),
'#title_display' => 'invisible',
'#options' => [
'simple' => $this
->t('simple'),
'advanced' => $this
->t('advanced'),
],
'#required' => TRUE,
'#wrapper_attributes' => [
'class' => [
'container-inline',
],
],
'#attributes' => [
'class' => [
'container-inline',
],
],
'#default_value' => $config
->get('mapping.type') === 'simple' ?: 'advanced',
];
$form['mapping']['simple'] = [
'#type' => 'container',
'#states' => [
'visible' => [
':input[name="mapping[type]"]' => [
'value' => 'simple',
],
],
],
'#attributes' => [
'class' => [
'container-inline',
],
],
];
$simple_mapping_ui_string = $this
->t('Serve @files-with-some-extension from @domain using @type-of-urls');
[
$simple_mapping_ui_string_part_one,
$simple_mapping_ui_string_part_two,
$simple_mapping_ui_string_part_three,
] = preg_split('/\\@[a-z\\-]+/', (string) $simple_mapping_ui_string, -1, PREG_SPLIT_NO_EMPTY);
$form['mapping']['simple']['extensions_condition_toggle'] = [
'#type' => 'select',
'#title' => $this
->t('Limit by file extension'),
'#title_display' => 'invisible',
'#field_prefix' => $simple_mapping_ui_string_part_one,
'#options' => [
'all' => $this
->t('all files'),
'nocssjs' => $this
->t('all files except CSS+JS'),
'limited' => $this
->t('only files'),
],
'#default_value' => $config
->get('mapping.conditions') === static::CONDITIONS_PRESET_NOCSSJS ? 'nocssjs' : (empty($config
->get('mapping.conditions.extensions')) ? 'all' : 'limited'),
];
$form['mapping']['simple']['extensions_condition_value'] = [
'#field_prefix' => $this
->t('with the extension'),
'#type' => 'textfield',
'#title' => $this
->t('Allowed file extensions'),
'#title_display' => 'invisible',
'#placeholder' => 'jpg jpeg png zip',
'#size' => 30,
'#default_value' => implode(' ', $config
->get('mapping.conditions.extensions') ?: []),
'#states' => [
'visible' => [
':input[name="mapping[simple][extensions_condition_toggle]"]' => [
'value' => 'limited',
],
],
],
];
$form['mapping']['simple']['domain'] = [
'#field_prefix' => $simple_mapping_ui_string_part_two,
'#type' => 'textfield',
'#placeholder' => 'example.com',
'#title' => $this
->t('Domain'),
'#title_display' => 'FALSE',
'#size' => 25,
'#default_value' => $config
->get('mapping.domain'),
];
$form['mapping']['advanced'] = [
'#type' => 'item',
'#markup' => '<em>' . $this
->t('Not configurable through the UI. Modify <code>cdn.settings.yml</code> directly, and <a href=":url">import it</a>. It is safe to edit all other settings via the UI.', [
':url' => 'https://www.drupal.org/documentation/administer/config',
]) . '</em>',
'#states' => [
'visible' => [
':input[name="mapping[type]"]' => [
'value' => 'advanced',
],
],
],
];
$form['mapping']['simple']['scheme'] = [
'#field_prefix' => $simple_mapping_ui_string_part_three,
'#type' => 'select',
'#title' => $this
->t('Type of URLs'),
'#title_display' => 'FALSE',
'#options' => [
'//' => $this
->t('scheme-relative URLs'),
'https://' => $this
->t('HTTPS URLs'),
'http://' => $this
->t('HTTP URLs'),
],
'#default_value' => $config
->get('scheme'),
];
$form['farfuture'] = [
'#type' => 'details',
'#title' => $this
->t('Forever cacheable files'),
'#group' => 'cdn_settings',
'#tree' => TRUE,
];
$form['farfuture']['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Make files cacheable forever'),
'#description' => $this
->t('Better performance thanks to better caching of files by the visitor. When a file changes a different URL is used, to ensure instantaneous updates for your visitors.'),
'#default_value' => $config
->get('farfuture.status'),
];
$visible_stream_wrappers = $this->streamWrapperManager
->getWrappers(StreamWrapperInterface::VISIBLE);
$non_core_visible_stream_wrappers = array_filter($visible_stream_wrappers, function (array $metadata) {
return strpos($metadata['class'], 'Drupal\\Core') !== 0;
});
$form['wrappers'] = [
'#type' => 'details',
'#title' => $this
->t('Stream wrappers'),
'#group' => 'cdn_settings',
'#tree' => TRUE,
'#access' => !empty($non_core_visible_stream_wrappers),
];
$checkboxes = $this
->buildStreamWrapperCheckboxes(array_keys($visible_stream_wrappers));
$form['wrappers']['stream_wrappers'] = [
'#type' => 'checkboxes',
'#options' => array_combine(array_keys($checkboxes), array_keys($checkboxes)),
'#default_value' => $config
->get('stream_wrappers'),
'#description' => $this
->t('Stream wrappers whose files to serve from CDN. Any stream wrapper generating local file URLs is eligible.'),
];
$form['wrappers']['stream_wrappers'] += $checkboxes;
// Special case: private://.
if (!empty($form['wrappers']['stream_wrappers']['private'])) {
$form['wrappers']['stream_wrappers']['private']['#disabled'] = TRUE;
$form['wrappers']['stream_wrappers']['private']['#title'] = '<del>' . $form['wrappers']['stream_wrappers']['private']['#title'] . '</del>';
$form['wrappers']['stream_wrappers']['private']['#description'] = $this
->t('Private files require authentication and hence cannot be served from a CDN.');
}
return parent::buildForm($form, $form_state);
}
/**
* Determines whether the stream wrapper generates external URLs.
*
* @param string $stream_wrapper_scheme
* A valid stream wrapper scheme.
* @param \Drupal\Core\StreamWrapper\StreamWrapperInterface $stream_wrapper
* A stream wrapper instance.
*
* @return bool
*/
protected function streamWrapperGeneratesExternalUrls($stream_wrapper_scheme, StreamWrapperInterface $stream_wrapper) : bool {
// Generate URL to imaginary file 'cdn.test'. Most stream wrappers don't
// check file existence, just concatenate strings.
$stream_wrapper
->setUri($stream_wrapper_scheme . '://cdn.test');
try {
$absolute_url = $stream_wrapper
->getExternalUrl();
$base_url = $this
->getRequest()
->getSchemeAndHttpHost() . $this
->getRequest()
->getBasePath();
$relative_url = str_replace($base_url, '', $absolute_url);
return UrlHelper::isExternal($relative_url);
} catch (\Exception $e) {
// In case of failure, assume this would have resulted in an external URL.
return TRUE;
}
}
/**
* Builds the stream wrapper checkboxes form array.
*
* @param string[] $stream_wrapper_schemes
* The stream wrapper schemes for which to generate form checkboxes.
*
* @return array
*/
protected function buildStreamWrapperCheckboxes(array $stream_wrapper_schemes) : array {
$checkboxes = [];
foreach ($stream_wrapper_schemes as $stream_wrapper_scheme) {
$wrapper = $this->streamWrapperManager
->getViaScheme($stream_wrapper_scheme);
$generates_external_urls = static::streamWrapperGeneratesExternalUrls($stream_wrapper_scheme, $wrapper);
$checkboxes[$stream_wrapper_scheme] = [
'#title' => $this
->t('@name → <code>@scheme://</code>', [
'@scheme' => $stream_wrapper_scheme,
'@name' => $wrapper
->getName(),
]),
'#disabled' => $generates_external_urls,
'#description' => !$generates_external_urls ? NULL : $this
->t('This stream wrapper generates external URLs, and hence cannot be served from a CDN.'),
];
}
return $checkboxes;
}
/**
* {@inheritdoc}
*/
protected static function mapFormValuesToConfig(FormStateInterface $form_state, Config $config) {
// Vertical tab: 'Status'.
$config
->set('status', (bool) $form_state
->getValue('status'));
// Vertical tab: 'Stream wrappers'.
$stream_wrappers = array_values(array_filter($form_state
->getValue([
'wrappers',
'stream_wrappers',
])));
$config
->set('stream_wrappers', $stream_wrappers);
// Vertical tab: 'Mapping'.
if ($form_state
->getValue([
'mapping',
'type',
]) === 'simple') {
$simple_mapping = $form_state
->getValue([
'mapping',
'simple',
]);
$config
->set('mapping', []);
$config
->set('mapping.type', 'simple');
$config
->set('mapping.domain', $simple_mapping['domain']);
// Only the 'extensions' condition is supported in this UI, to KISS.
if ($simple_mapping['extensions_condition_toggle'] === 'limited') {
// Set the 'extensions' condition unconditionally.
$config
->set('mapping.conditions.extensions', explode(' ', trim($simple_mapping['extensions_condition_value'])));
}
elseif ($simple_mapping['extensions_condition_toggle'] === 'nocssjs') {
$config
->set('mapping.conditions', static::CONDITIONS_PRESET_NOCSSJS);
}
else {
// Remove the 'not' or 'extensions' conditions if set.
$conditions = $config
->getOriginal('mapping.type') === 'simple' ? $config
->getOriginal('mapping.conditions') : [];
if (isset($conditions['not'])) {
unset($conditions['not']);
}
if (isset($conditions['extensions'])) {
unset($conditions['extensions']);
}
$config
->set('mapping.conditions', $conditions);
}
$config
->set('scheme', $simple_mapping['scheme']);
}
// Vertical tab: 'Forever cacheable files'.
$config
->set('farfuture.status', (bool) $form_state
->getValue([
'farfuture',
'status',
]));
return $config;
}
/**
* {@inheritdoc}
*/
protected static function mapViolationPropertyPathsToFormNames(string $property_path) : string {
switch ($property_path) {
case 'mapping.domain':
return 'mapping][simple][domain';
case 'scheme':
return 'mapping][simple][scheme';
default:
return parent::mapViolationPropertyPathsToFormNames($property_path);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CdnSettingsForm:: |
protected | property | The stream wrapper manager. | |
CdnSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
CdnSettingsForm:: |
protected | function | Builds the stream wrapper checkboxes form array. | |
CdnSettingsForm:: |
private | constant | The 'nocssjs' conditions preset: all files except CSS and JS. | |
CdnSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ValidatableConfigFormBase:: |
|
CdnSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
CdnSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
CdnSettingsForm:: |
protected static | function |
Overrides ValidatableConfigFormBase:: |
|
CdnSettingsForm:: |
protected static | function |
Overrides ValidatableConfigFormBase:: |
|
CdnSettingsForm:: |
protected static | function |
Overrides ValidatableConfigFormBase:: |
|
CdnSettingsForm:: |
protected | function | Determines whether the stream wrapper generates external URLs. | |
CdnSettingsForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase object. Overrides ValidatableConfigFormBase:: |
|
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. | |
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. | |
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. | |
ValidatableConfigFormBase:: |
protected | property | ||
ValidatableConfigFormBase:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
ValidatableConfigFormBase:: |
public | function |
Form validation handler. Overrides FormBase:: |