Skip to content

Commit f2573d4

Browse files
committed
feat: Update field ID handling for conditional logic and improve data attributes across components
1 parent 6eb13f0 commit f2573d4

5 files changed

Lines changed: 39 additions & 18 deletions

File tree

admin/src/pages/FieldsetEditor/components/ConditionalLogicPanel.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ export function ConditionalLogicPanel({
181181
<SelectTrigger className="w-[140px]">
182182
<SelectValue placeholder="Select field" />
183183
</SelectTrigger>
184-
<SelectContent>
185-
{otherFields.map((f) => (
186-
<SelectItem key={f.id} value={f.name}>
187-
{f.label}
188-
</SelectItem>
189-
))}
190-
</SelectContent>
184+
<SelectContent>
185+
{otherFields.map((f) => (
186+
<SelectItem key={f.id} value={f.id}>
187+
{f.label}
188+
</SelectItem>
189+
))}
190+
</SelectContent>
191191
</Select>
192192

193193
{/* Operator selector */}

plugin/assets/admin/js/fields.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -566,17 +566,24 @@
566566
return rules.every(rule => {
567567
if (!rule || !rule.field) return true;
568568

569-
// Find the field element by name.
570-
// Try multiple patterns since prefix may or may not be present.
571-
const fieldName = rule.field;
572-
const fieldElement = document.querySelector(
573-
`[name="${fieldName}"], [name="${fieldName}[]"], ` +
574-
`[name="openfields_${fieldName}"], [name="openfields_${fieldName}[]"], ` +
575-
`[data-field="${fieldName}"], [id="${fieldName}"]`
576-
);
569+
// rule.field is now a FIELD ID (immutable UUID/identifier)
570+
// Look for any field with data-field-id attribute matching this ID
571+
// This works for: root fields, repeater subfields, group subfields, anywhere
572+
const fieldId = rule.field;
573+
let fieldElement = document.querySelector(`[data-field-id="${fieldId}"]`);
574+
575+
// Fallback: try to find by field name (backwards compatibility)
576+
if (!fieldElement) {
577+
const fieldName = rule.field;
578+
fieldElement = document.querySelector(
579+
`[name="${fieldName}"], [name="${fieldName}[]"], ` +
580+
`[name="openfields_${fieldName}"], [name="openfields_${fieldName}[]"], ` +
581+
`[data-field="${fieldName}"], [id="${fieldName}"]`
582+
);
583+
}
577584

578585
if (!fieldElement) {
579-
console.warn(`[OpenFields] Conditional field not found: ${fieldName}`);
586+
console.warn(`[OpenFields] Conditional field not found: ${fieldId}`);
580587
return true; // If field not found, don't block.
581588
}
582589

plugin/includes/admin/field-renderers/group.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ function openfields_render_group_subfield( $sub_field, $parent_name, $object_id,
123123

124124
?>
125125
<div class="openfields-group-subfield openfields-field-wrapper openfields-field-wrapper--width-<?php echo intval( $wrapper_width ); ?><?php echo $wrapper_class ? ' ' . esc_attr( $wrapper_class ) : ''; ?>"
126-
style="width: <?php echo intval( $wrapper_width ); ?>%;"
126+
style="--of-field-width: <?php echo intval( $wrapper_width ); ?>%;"
127+
data-width="<?php echo intval( $wrapper_width ); ?>"
127128
data-subfield-name="<?php echo esc_attr( $sub_field->name ); ?>"
129+
<?php if ( ! empty( $sub_field->id ) ) : ?>
130+
data-field-id="<?php echo esc_attr( $sub_field->id ); ?>"
131+
<?php endif; ?>
128132
<?php if ( $wrapper_id ) : ?>
129133
id="<?php echo esc_attr( $wrapper_id ); ?>"
130134
<?php endif; ?>

plugin/includes/admin/field-renderers/repeater.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,12 @@ function openfields_render_repeater_subfield( $sub_field, $index, $base_name, $o
257257

258258
// Build style with CSS custom property - no inline PHP in attributes
259259
$width_val = intval( $width );
260-
echo '<div class="' . implode( ' ', array_map( 'esc_attr', $classes ) ) . '" style="--of-field-width: ' . $width_val . '%;" data-width="' . $width_val . '"' . $id_attr . '>';
260+
$field_id_attr = '';
261+
if ( ! empty( $sub_field->id ) ) {
262+
$field_id_attr = ' data-field-id="' . esc_attr( $sub_field->id ) . '"';
263+
}
264+
265+
echo '<div class="' . implode( ' ', array_map( 'esc_attr', $classes ) ) . '" style="--of-field-width: ' . $width_val . '%;" data-width="' . $width_val . '"' . $id_attr . $field_id_attr . '>';
261266

262267
if ( ! empty( $sub_field->label ) ) {
263268
echo '<label class="openfields-repeater-subfield-label" for="' . esc_attr( $field_id ) . '">';

plugin/includes/fields/class-openfields-field-wrapper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public function render() {
104104
$html .= ' style="--of-field-width: ' . intval( $width ) . '%;"';
105105
$html .= ' data-width="' . intval( $width ) . '"';
106106

107+
// Add unique field ID for conditional logic and form tracking.
108+
if ( ! empty( $this->field->id ) ) {
109+
$html .= ' data-field-id="' . esc_attr( $this->field->id ) . '"';
110+
}
111+
107112
// Add data attributes for conditional logic if present.
108113
$conditions = isset( $config['conditional_logic'] ) ? $config['conditional_logic'] : array();
109114
if ( ! empty( $conditions ) ) {

0 commit comments

Comments
 (0)