Skip to content

[4.x] Add PHP fieldPathPrefix method - #9080

Merged
jasonvarga merged 6 commits into
statamic:4.xfrom
jacksleight:field-path-prefix
Feb 13, 2024
Merged

[4.x] Add PHP fieldPathPrefix method#9080
jasonvarga merged 6 commits into
statamic:4.xfrom
jacksleight:field-path-prefix

Conversation

@jacksleight

@jacksleight jacksleight commented Nov 28, 2023

Copy link
Copy Markdown
Contributor

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

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.

2 participants