From 55a10fd4960c4a2d7e0b928d21a0251f82dd5786 Mon Sep 17 00:00:00 2001 From: Sunny Side Up Date: Fri, 6 Oct 2023 14:19:37 +1300 Subject: [PATCH] FIX: Fetch jQuery from a CDN since it can't come from admin (#90) --- src/Fields/KeyValueField.php | 41 +++++++++++++------------- src/Fields/MultiValueDropdownField.php | 24 ++++++++------- src/Fields/MultiValueListField.php | 11 ++++--- src/Fields/MultiValueTextField.php | 29 +++++++++++------- 4 files changed, 60 insertions(+), 45 deletions(-) diff --git a/src/Fields/KeyValueField.php b/src/Fields/KeyValueField.php index 893037b..6ee366b 100755 --- a/src/Fields/KeyValueField.php +++ b/src/Fields/KeyValueField.php @@ -80,14 +80,17 @@ public function __construct($name, $title = null, $sourceKeys = [], $sourceValue public function Field($properties = []) { - if (Controller::curr() instanceof ContentController) { - Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js'); + if (Controller::has_curr() + && (Controller::curr() instanceof ContentController) + && MultiValueTextField::config()->get('output_jquery_on_frontend') + ) { + Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js'); } Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js'); Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css'); - $nameKey = $this->name.'[key][]'; - $nameVal = $this->name.'[val][]'; + $nameKey = $this->name . '[key][]'; + $nameVal = $this->name . '[val][]'; $fields = []; $keyFieldPlaceholder = $this->getKeyFieldPlaceholder(); $valueFieldPlaceholder = $this->getValueFieldPlaceholder(); @@ -96,20 +99,20 @@ public function Field($properties = []) foreach ($this->value as $i => $v) { if ($this->readonly) { $fieldAttr = [ - 'class' => 'mventryfield mvkeyvalReadonly '.($this->extraClass() ? $this->extraClass() : ''), - 'id' => $this->id().MultiValueTextField::KEY_SEP.$i, + 'class' => 'mventryfield mvkeyvalReadonly ' . ($this->extraClass() ? $this->extraClass() : ''), + 'id' => $this->id() . MultiValueTextField::KEY_SEP . $i, 'name' => $nameKey, 'tabindex' => $this->getAttribute('tabindex') ]; $keyField = HTML::createTag('span', $fieldAttr, Convert::raw2xml($i)); - $fieldAttr['id'] = $this->id().MultiValueTextField::KEY_SEP.$v; + $fieldAttr['id'] = $this->id() . MultiValueTextField::KEY_SEP . $v; $valField = HTML::createTag('span', $fieldAttr, Convert::raw2xml($v)); - $fields[] = $keyField.$valField; + $fields[] = $keyField . $valField; } else { $keyField = $this->createSelectList($i, $nameKey, $this->sourceKeys, $i, $keyFieldPlaceholder); $valField = $this->createSelectList($i, $nameVal, $this->sourceValues, $v, $valueFieldPlaceholder); - $fields[] = $keyField.' '.$valField; + $fields[] = $keyField . ' ' . $valField; } } } else { @@ -119,14 +122,12 @@ public function Field($properties = []) if (!$this->readonly) { $keyField = $this->createSelectList('new', $nameKey, $this->sourceKeys, '', $keyFieldPlaceholder); $valField = $this->createSelectList('new', $nameVal, $this->sourceValues, '', $valueFieldPlaceholder); - $fields[] = $keyField.' '.$valField; -// $fields[] = $this->createSelectList('new', $name, $this->source); + $fields[] = $keyField . ' ' . $valField; + // $fields[] = $this->createSelectList('new', $name, $this->source); } - return ''; + return ''; } protected function createSelectList($number, $name, $values, $selected = '', $placeholder = '') @@ -136,7 +137,7 @@ protected function createSelectList($number, $name, $values, $selected = '', $pl [ 'selected' => $selected == '' ? 'selected' : '', 'value' => '' - ], + ], '' ); @@ -150,8 +151,8 @@ protected function createSelectList($number, $name, $values, $selected = '', $pl if (count($values ?? [])) { $attrs = [ - 'class' => 'text mventryfield mvdropdown '.($this->extraClass() ? $this->extraClass() : ''), - 'id' => $this->id().MultiValueTextField::KEY_SEP.$number, + 'class' => 'text mventryfield mvdropdown ' . ($this->extraClass() ? $this->extraClass() : ''), + 'id' => $this->id() . MultiValueTextField::KEY_SEP . $number, 'name' => $name, 'tabindex' => $this->getAttribute('tabindex') ]; @@ -163,8 +164,8 @@ protected function createSelectList($number, $name, $values, $selected = '', $pl return HTML::createTag('select', $attrs, $options); } else { $attrs = [ - 'class' => 'text mventryfield mvtextfield '.($this->extraClass() ? $this->extraClass() : ''), - 'id' => $this->id().MultiValueTextField::KEY_SEP.$number, + 'class' => 'text mventryfield mvtextfield ' . ($this->extraClass() ? $this->extraClass() : ''), + 'id' => $this->id() . MultiValueTextField::KEY_SEP . $number, 'value' => $selected, 'name' => $name, 'tabindex' => $this->getAttribute('tabindex'), diff --git a/src/Fields/MultiValueDropdownField.php b/src/Fields/MultiValueDropdownField.php index 24703bc..3920cc5 100755 --- a/src/Fields/MultiValueDropdownField.php +++ b/src/Fields/MultiValueDropdownField.php @@ -45,13 +45,16 @@ public function setSource(array $source) public function Field($properties = []) { - if (Controller::curr() instanceof ContentController) { - Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js'); + if (Controller::has_curr() + && (Controller::curr() instanceof ContentController) + && MultiValueTextField::config()->get('output_jquery_on_frontend') + ) { + Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js'); } Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js'); Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css'); - $name = $this->name.'[]'; + $name = $this->name . '[]'; $fields = []; @@ -59,8 +62,9 @@ public function Field($properties = []) foreach ($this->value as $i => $v) { if ($this->readonly) { $fieldAttr = [ - 'class' => 'mventryfield mvdropdownReadonly '.($this->extraClass() ? $this->extraClass() : ''), - 'id' => $this->id().MultiValueTextField::KEY_SEP.$i, + 'class' => 'mventryfield mvdropdownReadonly ' + . ($this->extraClass() ? $this->extraClass() : ''), + 'id' => $this->id() . MultiValueTextField::KEY_SEP . $i, 'name' => $name, 'tabindex' => $this->getAttribute('tabindex') ]; @@ -77,10 +81,10 @@ public function Field($properties = []) $fields[] = $this->createSelectList($i + 1, $name, $this->source); } - return ''; } public function Type() @@ -95,7 +99,7 @@ protected function createSelectList($number, $name, $values, $selected = '') [ 'selected' => $selected == '' ? 'selected' : '', 'value' => '' - ], + ], '' ); @@ -108,8 +112,8 @@ protected function createSelectList($number, $name, $values, $selected = '') } $attrs = [ - 'class' => 'mventryfield mvdropdown '.($this->extraClass() ? $this->extraClass() : ''), - 'id' => $this->id().MultiValueTextField::KEY_SEP.$number, + 'class' => 'mventryfield mvdropdown ' . ($this->extraClass() ? $this->extraClass() : ''), + 'id' => $this->id() . MultiValueTextField::KEY_SEP . $number, 'name' => $name, 'tabindex' => $this->getAttribute('tabindex') ]; diff --git a/src/Fields/MultiValueListField.php b/src/Fields/MultiValueListField.php index 838b9e2..df78ad2 100755 --- a/src/Fields/MultiValueListField.php +++ b/src/Fields/MultiValueListField.php @@ -27,13 +27,16 @@ public function __construct($name, $title = null, $source = [], $value = null) public function Field($properties = []) { - if (Controller::curr() instanceof ContentController) { - Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js'); + if (Controller::has_curr() + && (Controller::curr() instanceof ContentController) + && MultiValueTextField::config()->get('output_jquery_on_frontend') + ) { + Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js'); } Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js'); Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css'); - $name = $this->name.'[]'; + $name = $this->name . '[]'; $options = ''; if (!$this->value) { @@ -49,7 +52,7 @@ public function Field($properties = []) } $attrs = [ - 'class' => 'mventryfield mvlistbox '.($this->extraClass() ? $this->extraClass() : ''), + 'class' => 'mventryfield mvlistbox ' . ($this->extraClass() ? $this->extraClass() : ''), 'id' => $this->id(), 'name' => $name, 'tabindex' => $this->getAttribute('tabindex'), diff --git a/src/Fields/MultiValueTextField.php b/src/Fields/MultiValueTextField.php index 02fdad3..f52567e 100755 --- a/src/Fields/MultiValueTextField.php +++ b/src/Fields/MultiValueTextField.php @@ -22,20 +22,29 @@ class MultiValueTextField extends FormField protected $tag = 'input'; + /** + * Determines whether jQuery should be added to the frontend via a CDN. + * Set this to false if you already output your own jQuery. + */ + private static bool $output_jquery_on_frontend = true; + public function Field($properties = []) { - if (Controller::curr() instanceof ContentController) { - Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js'); + if (Controller::has_curr() + && (Controller::curr() instanceof ContentController) + && self::config()->get('output_jquery_on_frontend') + ) { + Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js'); } Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js'); Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css'); - $name = $this->name.'[]'; + $name = $this->name . '[]'; $fields = []; $attributes = [ 'type' => 'text', - 'class' => 'text mvtextfield mventryfield '.($this->extraClass() ? $this->extraClass() : ''), + 'class' => 'text mvtextfield mventryfield ' . ($this->extraClass() ? $this->extraClass() : ''), // 'id' => $this->id(), 'name' => $name, // 'value' => $this->Value(), @@ -48,7 +57,7 @@ public function Field($properties = []) $fieldAttr = $attributes; if ($this->value) { foreach ($this->value as $i => $v) { - $fieldAttr['id'] = $this->id().MultiValueTextField::KEY_SEP.$i; + $fieldAttr['id'] = $this->id() . MultiValueTextField::KEY_SEP . $i; $fieldAttr['value'] = $v; if ($this->readonly) { unset($fieldAttr['value']); @@ -62,17 +71,15 @@ public function Field($properties = []) // add an empty row if (!$this->readonly) { // assume next pos equals to the number of existing fields which gives index+1 in a zero-indexed list - $attributes['id'] = $this->id().MultiValueTextField::KEY_SEP.count($fields ?? []); + $attributes['id'] = $this->id() . MultiValueTextField::KEY_SEP . count($fields ?? []); $fields[] = $this->createInput($attributes); } if (count($fields ?? [])) { - return ''; + return ''; } else { - return '
'; + return '
'; } }