Skip to content

Commit 55a10fd

Browse files
authored
FIX: Fetch jQuery from a CDN since it can't come from admin (#90)
1 parent d1032f3 commit 55a10fd

File tree

4 files changed

+60
-45
lines changed

4 files changed

+60
-45
lines changed

src/Fields/KeyValueField.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ public function __construct($name, $title = null, $sourceKeys = [], $sourceValue
8080

8181
public function Field($properties = [])
8282
{
83-
if (Controller::curr() instanceof ContentController) {
84-
Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js');
83+
if (Controller::has_curr()
84+
&& (Controller::curr() instanceof ContentController)
85+
&& MultiValueTextField::config()->get('output_jquery_on_frontend')
86+
) {
87+
Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js');
8588
}
8689
Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js');
8790
Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css');
8891

89-
$nameKey = $this->name.'[key][]';
90-
$nameVal = $this->name.'[val][]';
92+
$nameKey = $this->name . '[key][]';
93+
$nameVal = $this->name . '[val][]';
9194
$fields = [];
9295
$keyFieldPlaceholder = $this->getKeyFieldPlaceholder();
9396
$valueFieldPlaceholder = $this->getValueFieldPlaceholder();
@@ -96,20 +99,20 @@ public function Field($properties = [])
9699
foreach ($this->value as $i => $v) {
97100
if ($this->readonly) {
98101
$fieldAttr = [
99-
'class' => 'mventryfield mvkeyvalReadonly '.($this->extraClass() ? $this->extraClass() : ''),
100-
'id' => $this->id().MultiValueTextField::KEY_SEP.$i,
102+
'class' => 'mventryfield mvkeyvalReadonly ' . ($this->extraClass() ? $this->extraClass() : ''),
103+
'id' => $this->id() . MultiValueTextField::KEY_SEP . $i,
101104
'name' => $nameKey,
102105
'tabindex' => $this->getAttribute('tabindex')
103106
];
104107

105108
$keyField = HTML::createTag('span', $fieldAttr, Convert::raw2xml($i));
106-
$fieldAttr['id'] = $this->id().MultiValueTextField::KEY_SEP.$v;
109+
$fieldAttr['id'] = $this->id() . MultiValueTextField::KEY_SEP . $v;
107110
$valField = HTML::createTag('span', $fieldAttr, Convert::raw2xml($v));
108-
$fields[] = $keyField.$valField;
111+
$fields[] = $keyField . $valField;
109112
} else {
110113
$keyField = $this->createSelectList($i, $nameKey, $this->sourceKeys, $i, $keyFieldPlaceholder);
111114
$valField = $this->createSelectList($i, $nameVal, $this->sourceValues, $v, $valueFieldPlaceholder);
112-
$fields[] = $keyField.' '.$valField;
115+
$fields[] = $keyField . ' ' . $valField;
113116
}
114117
}
115118
} else {
@@ -119,14 +122,12 @@ public function Field($properties = [])
119122
if (!$this->readonly) {
120123
$keyField = $this->createSelectList('new', $nameKey, $this->sourceKeys, '', $keyFieldPlaceholder);
121124
$valField = $this->createSelectList('new', $nameVal, $this->sourceValues, '', $valueFieldPlaceholder);
122-
$fields[] = $keyField.' '.$valField;
123-
// $fields[] = $this->createSelectList('new', $name, $this->source);
125+
$fields[] = $keyField . ' ' . $valField;
126+
// $fields[] = $this->createSelectList('new', $name, $this->source);
124127
}
125128

126-
return '<ul id="'.$this->id().'" class="multivaluefieldlist mvkeyvallist '.$this->extraClass().'"><li>'.implode(
127-
'</li><li>',
128-
$fields
129-
).'</li></ul>';
129+
return '<ul id="' . $this->id() . '" class="multivaluefieldlist mvkeyvallist ' . $this->extraClass() . '"><li>'
130+
. implode('</li><li>', $fields) . '</li></ul>';
130131
}
131132

132133
protected function createSelectList($number, $name, $values, $selected = '', $placeholder = '')
@@ -136,7 +137,7 @@ protected function createSelectList($number, $name, $values, $selected = '', $pl
136137
[
137138
'selected' => $selected == '' ? 'selected' : '',
138139
'value' => ''
139-
],
140+
],
140141
''
141142
);
142143

@@ -150,8 +151,8 @@ protected function createSelectList($number, $name, $values, $selected = '', $pl
150151

151152
if (count($values ?? [])) {
152153
$attrs = [
153-
'class' => 'text mventryfield mvdropdown '.($this->extraClass() ? $this->extraClass() : ''),
154-
'id' => $this->id().MultiValueTextField::KEY_SEP.$number,
154+
'class' => 'text mventryfield mvdropdown ' . ($this->extraClass() ? $this->extraClass() : ''),
155+
'id' => $this->id() . MultiValueTextField::KEY_SEP . $number,
155156
'name' => $name,
156157
'tabindex' => $this->getAttribute('tabindex')
157158
];
@@ -163,8 +164,8 @@ protected function createSelectList($number, $name, $values, $selected = '', $pl
163164
return HTML::createTag('select', $attrs, $options);
164165
} else {
165166
$attrs = [
166-
'class' => 'text mventryfield mvtextfield '.($this->extraClass() ? $this->extraClass() : ''),
167-
'id' => $this->id().MultiValueTextField::KEY_SEP.$number,
167+
'class' => 'text mventryfield mvtextfield ' . ($this->extraClass() ? $this->extraClass() : ''),
168+
'id' => $this->id() . MultiValueTextField::KEY_SEP . $number,
168169
'value' => $selected,
169170
'name' => $name,
170171
'tabindex' => $this->getAttribute('tabindex'),

src/Fields/MultiValueDropdownField.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,26 @@ public function setSource(array $source)
4545

4646
public function Field($properties = [])
4747
{
48-
if (Controller::curr() instanceof ContentController) {
49-
Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js');
48+
if (Controller::has_curr()
49+
&& (Controller::curr() instanceof ContentController)
50+
&& MultiValueTextField::config()->get('output_jquery_on_frontend')
51+
) {
52+
Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js');
5053
}
5154
Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js');
5255
Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css');
5356

54-
$name = $this->name.'[]';
57+
$name = $this->name . '[]';
5558
$fields = [];
5659

5760

5861
if ($this->value) {
5962
foreach ($this->value as $i => $v) {
6063
if ($this->readonly) {
6164
$fieldAttr = [
62-
'class' => 'mventryfield mvdropdownReadonly '.($this->extraClass() ? $this->extraClass() : ''),
63-
'id' => $this->id().MultiValueTextField::KEY_SEP.$i,
65+
'class' => 'mventryfield mvdropdownReadonly '
66+
. ($this->extraClass() ? $this->extraClass() : ''),
67+
'id' => $this->id() . MultiValueTextField::KEY_SEP . $i,
6468
'name' => $name,
6569
'tabindex' => $this->getAttribute('tabindex')
6670
];
@@ -77,10 +81,10 @@ public function Field($properties = [])
7781
$fields[] = $this->createSelectList($i + 1, $name, $this->source);
7882
}
7983

80-
return '<ul id="'.$this->id().'" class="multivaluefieldlist '.$this->extraClass().'"><li>'.implode(
84+
return '<ul id="' . $this->id() . '" class="multivaluefieldlist ' . $this->extraClass() . '"><li>' . implode(
8185
'</li><li>',
8286
$fields
83-
).'</li></ul>';
87+
) . '</li></ul>';
8488
}
8589

8690
public function Type()
@@ -95,7 +99,7 @@ protected function createSelectList($number, $name, $values, $selected = '')
9599
[
96100
'selected' => $selected == '' ? 'selected' : '',
97101
'value' => ''
98-
],
102+
],
99103
''
100104
);
101105

@@ -108,8 +112,8 @@ protected function createSelectList($number, $name, $values, $selected = '')
108112
}
109113

110114
$attrs = [
111-
'class' => 'mventryfield mvdropdown '.($this->extraClass() ? $this->extraClass() : ''),
112-
'id' => $this->id().MultiValueTextField::KEY_SEP.$number,
115+
'class' => 'mventryfield mvdropdown ' . ($this->extraClass() ? $this->extraClass() : ''),
116+
'id' => $this->id() . MultiValueTextField::KEY_SEP . $number,
113117
'name' => $name,
114118
'tabindex' => $this->getAttribute('tabindex')
115119
];

src/Fields/MultiValueListField.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ public function __construct($name, $title = null, $source = [], $value = null)
2727

2828
public function Field($properties = [])
2929
{
30-
if (Controller::curr() instanceof ContentController) {
31-
Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js');
30+
if (Controller::has_curr()
31+
&& (Controller::curr() instanceof ContentController)
32+
&& MultiValueTextField::config()->get('output_jquery_on_frontend')
33+
) {
34+
Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js');
3235
}
3336
Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js');
3437
Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css');
3538

36-
$name = $this->name.'[]';
39+
$name = $this->name . '[]';
3740

3841
$options = '';
3942
if (!$this->value) {
@@ -49,7 +52,7 @@ public function Field($properties = [])
4952
}
5053

5154
$attrs = [
52-
'class' => 'mventryfield mvlistbox '.($this->extraClass() ? $this->extraClass() : ''),
55+
'class' => 'mventryfield mvlistbox ' . ($this->extraClass() ? $this->extraClass() : ''),
5356
'id' => $this->id(),
5457
'name' => $name,
5558
'tabindex' => $this->getAttribute('tabindex'),

src/Fields/MultiValueTextField.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,29 @@ class MultiValueTextField extends FormField
2222

2323
protected $tag = 'input';
2424

25+
/**
26+
* Determines whether jQuery should be added to the frontend via a CDN.
27+
* Set this to false if you already output your own jQuery.
28+
*/
29+
private static bool $output_jquery_on_frontend = true;
30+
2531
public function Field($properties = [])
2632
{
27-
if (Controller::curr() instanceof ContentController) {
28-
Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js');
33+
if (Controller::has_curr()
34+
&& (Controller::curr() instanceof ContentController)
35+
&& self::config()->get('output_jquery_on_frontend')
36+
) {
37+
Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js');
2938
}
3039
Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js');
3140
Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css');
3241

33-
$name = $this->name.'[]';
42+
$name = $this->name . '[]';
3443
$fields = [];
3544

3645
$attributes = [
3746
'type' => 'text',
38-
'class' => 'text mvtextfield mventryfield '.($this->extraClass() ? $this->extraClass() : ''),
47+
'class' => 'text mvtextfield mventryfield ' . ($this->extraClass() ? $this->extraClass() : ''),
3948
// 'id' => $this->id(),
4049
'name' => $name,
4150
// 'value' => $this->Value(),
@@ -48,7 +57,7 @@ public function Field($properties = [])
4857
$fieldAttr = $attributes;
4958
if ($this->value) {
5059
foreach ($this->value as $i => $v) {
51-
$fieldAttr['id'] = $this->id().MultiValueTextField::KEY_SEP.$i;
60+
$fieldAttr['id'] = $this->id() . MultiValueTextField::KEY_SEP . $i;
5261
$fieldAttr['value'] = $v;
5362
if ($this->readonly) {
5463
unset($fieldAttr['value']);
@@ -62,17 +71,15 @@ public function Field($properties = [])
6271
// add an empty row
6372
if (!$this->readonly) {
6473
// assume next pos equals to the number of existing fields which gives index+1 in a zero-indexed list
65-
$attributes['id'] = $this->id().MultiValueTextField::KEY_SEP.count($fields ?? []);
74+
$attributes['id'] = $this->id() . MultiValueTextField::KEY_SEP . count($fields ?? []);
6675
$fields[] = $this->createInput($attributes);
6776
}
6877

6978
if (count($fields ?? [])) {
70-
return '<ul id="'.$this->id().'" class="multivaluefieldlist '.$this->extraClass().'"><li>'.implode(
71-
'</li><li>',
72-
$fields
73-
).'</li></ul>';
79+
return '<ul id="' . $this->id() . '" class="multivaluefieldlist ' . $this->extraClass() . '"><li>'
80+
. implode('</li><li>', $fields) . '</li></ul>';
7481
} else {
75-
return '<div id="'.$this->id().'" class="multivaluefieldlist '.$this->extraClass().'"></div>';
82+
return '<div id="' . $this->id() . '" class="multivaluefieldlist ' . $this->extraClass() . '"></div>';
7683
}
7784
}
7885

0 commit comments

Comments
 (0)