From c399fe8171e2948c64e8605c6f0947fa310e271a Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Thu, 17 Mar 2022 00:57:50 +0100 Subject: [PATCH 01/12] list-show-on --- layouts/joomla/form/field/list.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/layouts/joomla/form/field/list.php b/layouts/joomla/form/field/list.php index c827e314a99ac..42a528e031257 100644 --- a/layouts/joomla/form/field/list.php +++ b/layouts/joomla/form/field/list.php @@ -9,6 +9,7 @@ defined('_JEXEC') or die; +use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; extract($displayData); @@ -42,9 +43,10 @@ * @var array $options Options available for this field. * @var string $dataAttribute Miscellaneous data attributes preprocessed for HTML output * @var array $dataAttributes Miscellaneous data attribute for eg, data-* + * @var boolen $hasShowOn Is the list using the showon */ -$html = array(); +$html = []; $attr = ''; // Initialize the field attributes. @@ -57,6 +59,11 @@ $attr .= !empty($description) ? ' aria-describedby="' . ($id ?: $name) . '-desc"' : ''; $attr .= $dataAttribute; +if ($hasShowOn) +{ + Factory::getDocument()->getWebAssetManager()->useScript('showon'); +} + // To avoid user's confusion, readonly="readonly" should imply disabled="disabled". if ($readonly || $disabled) { From cb41ce089fd3d0f769d538fb3b7b648fd5581008 Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Thu, 17 Mar 2022 01:01:16 +0100 Subject: [PATCH 02/12] Update ListField.php --- libraries/src/Form/Field/ListField.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/libraries/src/Form/Field/ListField.php b/libraries/src/Form/Field/ListField.php index 00aeb511e4fda..a2862a01ebde1 100644 --- a/libraries/src/Form/Field/ListField.php +++ b/libraries/src/Form/Field/ListField.php @@ -45,6 +45,14 @@ class ListField extends FormField */ protected $layout = 'joomla.form.field.list'; + /** + * Does the list has showOn + * + * @var boolean + * @since 4.1.1 + */ + protected $hasShowOn = false; + /** * Method to get the field input markup for a generic list. * Use the multiple attribute to enable multiselect. @@ -55,9 +63,9 @@ class ListField extends FormField */ protected function getInput() { - $data = $this->getLayoutData(); - - $data['options'] = (array) $this->getOptions(); + $data = $this->getLayoutData(); + $data['options'] = (array) $this->getOptions(); + $data['hasShowOn'] = $this->hasShowOn; return $this->getRenderer($this->layout)->render($data); } @@ -72,7 +80,7 @@ protected function getInput() protected function getOptions() { $fieldname = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname); - $options = array(); + $options = []; foreach ($this->element->xpath('option') as $option) { @@ -123,14 +131,14 @@ protected function getOptions() $selected = (string) $option['selected']; $selected = ($selected === 'true' || $selected === 'selected' || $selected === '1'); - $tmp = array( + $tmp = [ 'value' => $value, 'text' => Text::alt($text, $fieldname), 'disable' => $disabled, 'class' => (string) $option['class'], 'selected' => ($checked || $selected), 'checked' => ($checked || $selected), - ); + ]; // Set some event handler attributes. But really, should be using unobtrusive js. $tmp['onclick'] = (string) $option['onclick']; @@ -138,6 +146,7 @@ protected function getOptions() if ((string) $option['showon']) { + $this->hasShowOn = true; $encodedConditions = json_encode( FormHelper::parseShowOnConditions((string) $option['showon'], $this->formControl, $this->group) ); From ac8bed219fa2e5848bfcc28f4190b168c3147aad Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Thu, 17 Mar 2022 11:42:36 +0100 Subject: [PATCH 03/12] Update libraries/src/Form/Field/ListField.php Co-authored-by: Richard Fath --- libraries/src/Form/Field/ListField.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/src/Form/Field/ListField.php b/libraries/src/Form/Field/ListField.php index a2862a01ebde1..f2924b2f3ffa1 100644 --- a/libraries/src/Form/Field/ListField.php +++ b/libraries/src/Form/Field/ListField.php @@ -46,10 +46,10 @@ class ListField extends FormField protected $layout = 'joomla.form.field.list'; /** - * Does the list has showOn + * Does the list have showOn * * @var boolean - * @since 4.1.1 + * @since __DEPLOY_VERSION__ */ protected $hasShowOn = false; From 6a7684ecad79912e6f618a5d55daa6ae797fa743 Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Fri, 18 Mar 2022 09:16:41 +0100 Subject: [PATCH 04/12] Update list.php --- layouts/joomla/form/field/list.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/layouts/joomla/form/field/list.php b/layouts/joomla/form/field/list.php index 42a528e031257..257284d6024e0 100644 --- a/layouts/joomla/form/field/list.php +++ b/layouts/joomla/form/field/list.php @@ -9,7 +9,6 @@ defined('_JEXEC') or die; -use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; extract($displayData); @@ -43,7 +42,6 @@ * @var array $options Options available for this field. * @var string $dataAttribute Miscellaneous data attributes preprocessed for HTML output * @var array $dataAttributes Miscellaneous data attribute for eg, data-* - * @var boolen $hasShowOn Is the list using the showon */ $html = []; @@ -59,11 +57,6 @@ $attr .= !empty($description) ? ' aria-describedby="' . ($id ?: $name) . '-desc"' : ''; $attr .= $dataAttribute; -if ($hasShowOn) -{ - Factory::getDocument()->getWebAssetManager()->useScript('showon'); -} - // To avoid user's confusion, readonly="readonly" should imply disabled="disabled". if ($readonly || $disabled) { From 93b2068b9010150e5d2a9bc68395e090260d49cb Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Fri, 18 Mar 2022 09:19:39 +0100 Subject: [PATCH 05/12] Update ListField.php --- libraries/src/Form/Field/ListField.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/src/Form/Field/ListField.php b/libraries/src/Form/Field/ListField.php index f2924b2f3ffa1..afd189b6ec4ba 100644 --- a/libraries/src/Form/Field/ListField.php +++ b/libraries/src/Form/Field/ListField.php @@ -63,9 +63,9 @@ class ListField extends FormField */ protected function getInput() { - $data = $this->getLayoutData(); - $data['options'] = (array) $this->getOptions(); - $data['hasShowOn'] = $this->hasShowOn; + $data = $this->getLayoutData(); + $data['options'] = (array) $this->getOptions(); + $options['showonEnabled'] = $this->hasShowOn; return $this->getRenderer($this->layout)->render($data); } From 3f44437cc89e902b34c2cd1560c82d79d1222194 Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Fri, 18 Mar 2022 09:20:16 +0100 Subject: [PATCH 06/12] Update list.php --- layouts/joomla/form/field/list.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/layouts/joomla/form/field/list.php b/layouts/joomla/form/field/list.php index 257284d6024e0..c827e314a99ac 100644 --- a/layouts/joomla/form/field/list.php +++ b/layouts/joomla/form/field/list.php @@ -44,7 +44,7 @@ * @var array $dataAttributes Miscellaneous data attribute for eg, data-* */ -$html = []; +$html = array(); $attr = ''; // Initialize the field attributes. From 723f805a7eba122353866ce77fa6d05ed52120a5 Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Fri, 18 Mar 2022 09:21:38 +0100 Subject: [PATCH 07/12] Update ListField.php --- libraries/src/Form/Field/ListField.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/src/Form/Field/ListField.php b/libraries/src/Form/Field/ListField.php index afd189b6ec4ba..149784394de3a 100644 --- a/libraries/src/Form/Field/ListField.php +++ b/libraries/src/Form/Field/ListField.php @@ -63,9 +63,9 @@ class ListField extends FormField */ protected function getInput() { - $data = $this->getLayoutData(); - $data['options'] = (array) $this->getOptions(); - $options['showonEnabled'] = $this->hasShowOn; + $data = $this->getLayoutData(); + $data['options'] = (array) $this->getOptions(); + $data['showonEnabled'] = $this->hasShowOn; return $this->getRenderer($this->layout)->render($data); } From 284aa6c72e7e8a3f912317929bb5ba04edf3a64a Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Sun, 20 Mar 2022 11:42:02 +0100 Subject: [PATCH 08/12] Update FormField.php --- libraries/src/Form/FormField.php | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/libraries/src/Form/FormField.php b/libraries/src/Form/FormField.php index d328421e3bf69..62072d4c8e286 100644 --- a/libraries/src/Form/FormField.php +++ b/libraries/src/Form/FormField.php @@ -1316,17 +1316,12 @@ public function postProcess($value, $group = null, Registry $input = null) */ protected function getLayoutData() { - // Label preprocess - $label = !empty($this->element['label']) ? (string) $this->element['label'] : null; - $label = $label && $this->translateLabel ? Text::_($label) : $label; - - // Description preprocess + $label = !empty($this->element['label']) ? (string) $this->element['label'] : null; + $label = $label && $this->translateLabel ? Text::_($label) : $label; $description = !empty($this->description) ? $this->description : null; $description = !empty($description) && $this->translateDescription ? Text::_($description) : $description; - - $alt = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname); - - return [ + $alt = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname); + $options = [ 'autocomplete' => $this->autocomplete, 'autofocus' => $this->autofocus, 'class' => $this->class, @@ -1356,6 +1351,20 @@ protected function getLayoutData() 'dataAttributes' => $this->dataAttributes, 'parentclass' => $this->parentclass, ]; + + // Enable showOn from nested options + if (!empty((array) $this->element->xpath('option'))) + { + foreach ($this->element->xpath('option') as $option) + { + if ((string) $option['showon']) + { + $options['showonEnabled'] = true; + } + } + } + + return $options; } /** From 113a68fb96db098c6c1f4b8328205aec2525d735 Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Sun, 20 Mar 2022 11:44:21 +0100 Subject: [PATCH 09/12] Update ListField.php --- libraries/src/Form/Field/ListField.php | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/libraries/src/Form/Field/ListField.php b/libraries/src/Form/Field/ListField.php index 149784394de3a..767b6e34f8f6b 100644 --- a/libraries/src/Form/Field/ListField.php +++ b/libraries/src/Form/Field/ListField.php @@ -45,14 +45,6 @@ class ListField extends FormField */ protected $layout = 'joomla.form.field.list'; - /** - * Does the list have showOn - * - * @var boolean - * @since __DEPLOY_VERSION__ - */ - protected $hasShowOn = false; - /** * Method to get the field input markup for a generic list. * Use the multiple attribute to enable multiselect. @@ -63,9 +55,8 @@ class ListField extends FormField */ protected function getInput() { - $data = $this->getLayoutData(); - $data['options'] = (array) $this->getOptions(); - $data['showonEnabled'] = $this->hasShowOn; + $data = $this->getLayoutData(); + $data['options'] = (array) $this->getOptions(); return $this->getRenderer($this->layout)->render($data); } @@ -80,7 +71,7 @@ protected function getInput() protected function getOptions() { $fieldname = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname); - $options = []; + $options = array(); foreach ($this->element->xpath('option') as $option) { @@ -131,14 +122,14 @@ protected function getOptions() $selected = (string) $option['selected']; $selected = ($selected === 'true' || $selected === 'selected' || $selected === '1'); - $tmp = [ + $tmp = array( 'value' => $value, 'text' => Text::alt($text, $fieldname), 'disable' => $disabled, 'class' => (string) $option['class'], 'selected' => ($checked || $selected), 'checked' => ($checked || $selected), - ]; + ); // Set some event handler attributes. But really, should be using unobtrusive js. $tmp['onclick'] = (string) $option['onclick']; @@ -146,7 +137,6 @@ protected function getOptions() if ((string) $option['showon']) { - $this->hasShowOn = true; $encodedConditions = json_encode( FormHelper::parseShowOnConditions((string) $option['showon'], $this->formControl, $this->group) ); From b761cf4fb84fbc2446f887b8a00eb4d96b9a3f0d Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Sun, 20 Mar 2022 11:59:39 +0100 Subject: [PATCH 10/12] Update ListField.php --- libraries/src/Form/Field/ListField.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/src/Form/Field/ListField.php b/libraries/src/Form/Field/ListField.php index 767b6e34f8f6b..00aeb511e4fda 100644 --- a/libraries/src/Form/Field/ListField.php +++ b/libraries/src/Form/Field/ListField.php @@ -55,7 +55,8 @@ class ListField extends FormField */ protected function getInput() { - $data = $this->getLayoutData(); + $data = $this->getLayoutData(); + $data['options'] = (array) $this->getOptions(); return $this->getRenderer($this->layout)->render($data); From 281b1c99ca8af34b943f649777abec49d8ac528c Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Sun, 20 Mar 2022 12:35:35 +0100 Subject: [PATCH 11/12] Tiny perf --- libraries/src/Form/FormField.php | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/src/Form/FormField.php b/libraries/src/Form/FormField.php index 62072d4c8e286..58b3987b8001e 100644 --- a/libraries/src/Form/FormField.php +++ b/libraries/src/Form/FormField.php @@ -1360,6 +1360,7 @@ protected function getLayoutData() if ((string) $option['showon']) { $options['showonEnabled'] = true; + break; } } } From 32997d579e865bc08cd639ebb420332024038ea4 Mon Sep 17 00:00:00 2001 From: Dimitris Grammatikogiannis Date: Sun, 20 Mar 2022 12:39:54 +0100 Subject: [PATCH 12/12] Update libraries/src/Form/FormField.php Co-authored-by: Richard Fath --- libraries/src/Form/FormField.php | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/src/Form/FormField.php b/libraries/src/Form/FormField.php index 58b3987b8001e..824594aef6816 100644 --- a/libraries/src/Form/FormField.php +++ b/libraries/src/Form/FormField.php @@ -1360,6 +1360,7 @@ protected function getLayoutData() if ((string) $option['showon']) { $options['showonEnabled'] = true; + break; } }