From b579e2442504b97864363ea2c9987bfcb80f2565 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Tue, 1 Jul 2025 09:40:01 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20extra=20=E2=80=98for=E2=80=99=20attribute?= =?UTF-8?q?=20issue=20in=20InputfieldWrapper=20(that=20had=20value=20?= =?UTF-8?q?=E2=80=98{for}=E2=80=99),=20plus=20add=20new=20removeByName()?= =?UTF-8?q?=20method=20for=20removing=20an=20Inputfield=20from=20the=20for?= =?UTF-8?q?m=20by=20name.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wire/core/InputfieldWrapper.php | 41 ++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/wire/core/InputfieldWrapper.php b/wire/core/InputfieldWrapper.php index d0bb71b4..a58bf39a 100644 --- a/wire/core/InputfieldWrapper.php +++ b/wire/core/InputfieldWrapper.php @@ -559,6 +559,27 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre return $this; } + /** + * Remove an Inputfield from the form by name + * + * Note that this works the same as the getByName/getChildByName methods in that it + * will find (and remove) the field by name, even if nested within other wrappers + * or fieldsets. It returns the removed Inputfield when found, or null if not. + * + * @param string $name + * @return Inputfield|null Removed Inputfield object on success, or null if not found + * @since 3.0.250 + * + */ + public function removeByName($name) { + $f = $this->getByName((string) $name); + if(!$f) return null; + $parent = $f->getParent(); + if(!$parent instanceof InputfieldWrapper) return null; + $parent->remove($f); + return $f; + } + /** * Prepare children for rendering by creating any fieldset groups * @@ -902,7 +923,11 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre } else { // label always visible $label = str_replace('{out}', $icon . $label . $toggle, $markup['item_label']); - if($skipLabel !== Inputfield::skipLabelFor) $label = $this->setAttributeInMarkup('for', $for, $label, true); + if($skipLabel === Inputfield::skipLabelFor) { + $label = $this->removeAttributeFromMarkup('for', $label); + } else { + $label = $this->setAttributeInMarkup('for', $for, $label, true); + } } $headerClass = trim($inputfield->getSetting('headerClass') . " $classes[item_label]"); $label = $this->setAttributeInMarkup('class', $headerClass, $label); @@ -1053,6 +1078,20 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre return $markup; } + /** + * Remove named attribute from given markup + * + * @param string $name + * @param string $markup + * @return string + * @since 3.0.250 + * + */ + protected function removeAttributeFromMarkup($name, $markup) { + if(stripos($markup, " $name=") === false) return $markup; + return preg_replace('!\s' . $name . '=["\'][^"\']*["\']!i', '', $markup); + } + /** * Render Inputfield header actions *