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

[4.x] Add PHP fieldPathPrefix method #9080

Merged
merged 6 commits into from
Feb 13, 2024

Conversation

jacksleight
Copy link
Contributor

@jacksleight jacksleight commented Nov 28, 2023

Vue fieldtype components have a fieldPathPrefix prop which contains a dot notation version of the field handles/set indexes. This tells fields exactly where they are in the structure and is needed to access the values of sibling set fields in the store.

PHP fieldtype classes do not have an equivalent, which means it’s not currently possible to get the values of sibling set fields in process/preload etc. Currently they can only access:

  • $this->field->parent() which returns the root entry
  • $this->field->parentField() which returns the replicator/bard field
  • $this->field->handlePath() which returns an array of the field handles but excludes the set indexes

This PR adds a Field::fieldPathPrefix() method that returns a dot notation path just like Vue. It also adds a fieldPathKeys() method that returns the same data as an array. And for completeness a fieldPathKeys method has also been added to the Vue fieldtype components.

To make it work I’ve had to update a few other classes in order to ensure the index is always passed down. If people are extending replicator/grid and overriding the processRow() method this could be breaking as there's now a new index argument, so it might need to target v5.

There are two situations where an index doesn't exist and therefore the fieldPathPrefix won’t be complete/real:

  1. During the NestedFields::preProcessConfig() method the field has no knowledge of it's parents so the path is just a single field handle
  2. During the $defaults and $new lookups in bard/replicator preload() methods the field has knowledge of its parents but it’s not being used for a real set so the index will be -1

@jasonvarga jasonvarga merged commit d6fc17f into statamic:4.x Feb 13, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants