Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace Drupal\bootstrap\Plugin\Setting\Advanced\Cdn;
use Drupal\bootstrap\Plugin\Form\SystemThemeSettings;
use Drupal\bootstrap\Plugin\ProviderManager;
use Drupal\bootstrap\Utility\Element;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Form\FormStateInterface;
/**
* Due to BC reasons, this class cannot be moved.
*
* @todo Move namespace up one.
*/
/**
* The "cdn_custom" theme setting.
*
* @ingroup plugins_setting
*
* @BootstrapSetting(
* id = "cdn_custom",
* type = "textarea",
* description = @Translation("One complete URL per line. All URLs are validated and parsed to determine available version(s) and/or theme(s). A URL can be any file ending in <code>.css</code> or <code>.js</code> (with matching response MIME type). Minified URLs can also be supplied and the will be used automatically."),
* groups = {
* "cdn" = @Translation("CDN (Content Delivery Network)"),
* "cdn_provider" = false,
* "custom" = @Translation("Custom URLs"),
* },
* )
*/
class CdnCustom extends CdnProviderBase {
/**
* {@inheritdoc}
*/
public function access() {
return parent::access()->andIf(AccessResultAllowed::allowedIf($this->getProvider()->getPluginId() === 'custom'));
}
/**
* {@inheritdoc}
*/
public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
$group = $this->getGroupElement($form, $form_state);
$group->setProperty('weight', 99);
$group->access($this->access());
$setting = $this->getSettingElement($form, $form_state);
$setting->setProperty('smart_description', FALSE);
$rows = count(array_filter(array_map('trim', preg_split("/\r\n|\n/", $form_state->getValue('cdn_custom', '')))));
$setting->setProperty('rows', $rows > 20 ? 20 : $rows);
$group->apply = $this->setCdnProvidersAjax(Element::createStandalone([
'#weight' => 100,
'#type' => 'submit',
'#value' => $this->t('Apply'),
'#submit' => [
[get_class($this), 'submitApplyCss'],
],
]));
}
/**
* {@inheritdoc}
*/
public function getDefaultValue() {
return implode("\n", [
'https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.css',
'https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css',
'https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.js',
'https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js',
]);
}
/**
* {@inheritdoc}
*/
public function processDeprecatedValues(array $values, array $deprecated) {
// Merge the deprecated settings together to form a new line for each value.
// @todo Remove deprecated setting support in a future release.
return implode("\n", $values) ?: NULL;
}
/**
* Submit callback for resetting CDN Provider cache.
*
* @param array $form
* Nested array of form elements that comprise the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public static function submitApplyCss(array $form, FormStateInterface $form_state) {
$theme = SystemThemeSettings::getTheme(Element::create($form, $form_state), $form_state);
$theme->setSetting('cdn_custom', $form_state->getValue('cdn_custom'));
$form_state->setRebuild();
}
/**
* {@inheritdoc}
*/
public static function validateFormElement(Element $form, FormStateInterface $form_state) {
// Immediately return if this isn't the currently selected CDN Provider.
if ($form_state->getValue('cdn_provider') !== 'custom') {
return;
}
$theme = SystemThemeSettings::getTheme($form, $form_state);
/** @var \Drupal\bootstrap\Plugin\Provider\Custom $provider */
$provider = ProviderManager::load($theme, 'custom');
$urls = array_filter(array_map('trim', preg_split("/\r\n|\n/", $form_state->getValue('cdn_custom', ''))));
$invalid = [];
foreach ($urls as $url) {
try {
$provider->validateUrl($url);
}
catch (\Exception $e) {
$invalid[] = $e->getMessage();
}
}
if ($invalid) {
$form_state->setErrorByName('cdn_custom', t('Invalid Custom URLs: <ul><li>@invalid</li></ul>', [
'@invalid' => new FormattableMarkup(implode('</li><li>', $invalid), []),
]));
// Unfortunately, any errors set during validation prevents the form
// rebuilding functionality from working. This has to be changed here.
$form->cdn->cdn_provider->custom->setProperty('open', TRUE);
}
}
}