Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cater for duplicate field names with different keys #16

Open
bobbingwide opened this issue Aug 21, 2023 · 5 comments
Open

Cater for duplicate field names with different keys #16

bobbingwide opened this issue Aug 21, 2023 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@bobbingwide
Copy link
Owner

While trying to develop PHPUnit tests for each field type I encountered a problem where the field name Text was being displayed for the wrong post type.

Explanation

  • The field's key was different from the expected value.
  • This was due to the fact that in a field group called Layout I'd used the clone field type with a clone of the Basic group.
  • I'd changed the Basic group to apply to the test-acf-fields CPT, but I'd not yet changed the Layout group.
  • The Text field being displayed was from the cloned group. Its key was field_64abd97b8ff80_field_64aa869f48df7

Proposed solution

TBC

@bobbingwide bobbingwide added the bug Something isn't working label Aug 21, 2023
@bobbingwide bobbingwide self-assigned this Aug 21, 2023
@bobbingwide
Copy link
Owner Author

In s.b/oikcom I must have created the ACF field blocks for the fields in the Basic group before I created the Layout group.
However I note that the attributes don't show the expected value for the field key ( _acf_field_name ).

<!-- wp:acf-field/acf-field {"name":"acf-field/acf-field",
"data":{"acf-field-name":"text","_acf-field-name":"field_645f589a88304"},
"mode":"edit","backgroundColor":"very-light-gray","fontSize":"normal"} /-->

@bobbingwide
Copy link
Owner Author

However I note that the attributes don't show the expected value for the field key ( _acf_field_name ).

What we see in the data attribute are two values for acf-field-name and _acf-field-name
The value for acf-field-name is the user friendly field name.
The value for _acf-field-name is the ACF internal name for the acf-field-name attribute.
It's NOT the ACF internal name for the text field.

I believe I need to change the code such that the field name is actually the field's key.

@bobbingwide
Copy link
Owner Author

Having done this the HTML for the block is simplified
For the original text field it becomes

<!-- wp:acf-field/acf-field {"name":"acf-field/acf-field",
"data":{"field_645f589a88304":"field_64aa869f48df7"},
"mode":"preview"} /-->

For the cloned field in the Layout group it becomes.

<!-- wp:acf-field/acf-field {"name":"acf-field/acf-field",
"data":{"field_645f589a88304":"field_64abd97b8ff80_field_64aa869f48df7"},
"mode":"edit"} /-->

@bobbingwide
Copy link
Owner Author

bobbingwide commented Sep 2, 2023

It would appear that the simplified version is only temporary; it's what's displayed in the Code Editor when in edit mode.

When saved the data attribute is more complicated.

<!-- wp:acf-field/acf-field {"name":"acf-field/acf-field",
"data":{"acf-field-name":"field_64aa869f48df7","_acf-field-name":"field_645f589a88304"},
"mode":"edit"} /-->

@bobbingwide
Copy link
Owner Author

The value for the acf-field-name attribute in data is now the field's key. This is currently used to generate the classNames for the div.

<div class="acf-field-field_64aa869f48df7 acf-type-text has-text-align- wp-block-acf-field-acf-field">rendered text field</div>

It would be nice to be able to map the key back to the name given by the user.
This would improve the message shown when the field is not set.

<p>Field field_64aa869f48df7 not set for post ID 0</p>

Rather than field_64aa869f48df7 we'd see text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant