From a3e67b299b8a03a3b33ded36d9dfa0fcf83da783 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 30 Sep 2022 11:57:08 -0400 Subject: [PATCH] Add support for a data-field-name attribute on Inputfields that use names that differ from the relevant field name (likes inputfields in repeaters). This makes the source field discoverable from JS in cases where it previously wasn't. --- wire/core/Field.php | 4 ++++ wire/core/InputfieldWrapper.php | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/wire/core/Field.php b/wire/core/Field.php index 790fef82..cd986316 100644 --- a/wire/core/Field.php +++ b/wire/core/Field.php @@ -978,6 +978,10 @@ class Field extends WireData implements Saveable, Exportable { // predefined field settings $inputfield->attr('name', $this->name . $contextStr); $inputfield->set('label', $this->label); + if($contextStr) { + // keep track of original field name in Inputfields that are are renamed by context + if(!$inputfield->attr('data-field-name')) $inputfield->attr('data-field-name', $this->name); + } // just in case an Inputfield needs to know its Fieldtype/Field context, or lack of it $inputfield->set('hasFieldtype', $this->type); diff --git a/wire/core/InputfieldWrapper.php b/wire/core/InputfieldWrapper.php index 5f4a4ed9..d5969469 100644 --- a/wire/core/InputfieldWrapper.php +++ b/wire/core/InputfieldWrapper.php @@ -937,6 +937,12 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre if(!isset($ffAttrs['id'])) $ffAttrs['id'] = 'wrap_' . $inputfield->attr('id'); $ffAttrs['class'] = str_replace('Inputfield_ ', '', $ffAttrs['class']); $wrapClass = $inputfield->getSetting('wrapClass'); + $fieldName = $inputfield->attr('data-field-name'); + if($fieldName && $fieldName != $inputfield->attr('name')) { + // ensures that Inputfields renamed by context retain the original field-name based class + $wrapClass = "Inputfield_$fieldName $wrapClass"; + if(!isset($ffAttrs['data-id'])) $ffAttrs['data-id'] = "wrap_Inputfield_$fieldName"; + } if($wrapClass) $ffAttrs['class'] = trim("$ffAttrs[class] $wrapClass"); foreach($inputfield->wrapAttr() as $k => $v) { if($k === 'class' && !empty($ffAttrs[$k])) {