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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions resources/js/components/fieldtypes/Fieldtype.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export default {
return this.value;
},

fieldPathKeys() {
const prefix = this.fieldPathPrefix || this.handle;

return prefix.split('.');
},

fieldId() {
let prefix = this.fieldPathPrefix ? this.fieldPathPrefix+'.' : '';

Expand Down
29 changes: 27 additions & 2 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Field implements Arrayable
protected $value;
protected $parent;
protected $parentField;
protected $parentIndex;
protected $validationContext;
protected ?Form $form = null;

Expand All @@ -33,7 +34,7 @@ public function newInstance()
{
return (new static($this->handle, $this->config))
->setParent($this->parent)
->setParentField($this->parentField)
->setParentField($this->parentField, $this->parentIndex)
->setValue($this->value);
}

Expand All @@ -58,6 +59,24 @@ public function handlePath()
return $path;
}

public function fieldPathKeys()
{
$path = $this->parentField ? $this->parentField->fieldPathKeys() : [];

if (isset($this->parentIndex)) {
$path[] = $this->parentIndex;
}

$path[] = $this->handle();

return $path;
}

public function fieldPathPrefix()
{
return implode('.', $this->fieldPathKeys());
}

public function setPrefix($prefix)
{
$this->prefix = $prefix;
Expand All @@ -70,6 +89,11 @@ public function prefix()
return $this->prefix;
}

public function parentIndex()
{
return $this->parentIndex;
}

public function type()
{
return array_get($this->config, 'type', 'text');
Expand Down Expand Up @@ -277,9 +301,10 @@ public function parent()
return $this->parent;
}

public function setParentField($field)
public function setParentField($field, $index = null)
{
$this->parentField = $field;
$this->parentIndex = $index;

return $this;
}
Expand Down
14 changes: 8 additions & 6 deletions src/Fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ class Fields
protected $fields;
protected $parent;
protected $parentField;
protected $parentIndex;
protected $filled = [];
protected $withValidatableValues = false;
protected $withComputedValues = false;

public function __construct($items = [], $parent = null, $parentField = null)
public function __construct($items = [], $parent = null, $parentField = null, $parentIndex = null)
{
$this
->setParent($parent)
->setParentField($parentField)
->setParentField($parentField, $parentIndex)
->setItems($items);
}

Expand Down Expand Up @@ -59,12 +60,13 @@ public function setParent($parent)
return $this;
}

public function setParentField($field)
public function setParentField($field, $index = null)
{
$this->parentField = $field;
$this->parentIndex = $index;

if ($this->fields) {
$this->fields->each(fn ($f) => $f->setParentField($field));
$this->fields->each(fn ($f) => $f->setParentField($field, $index));
}

return $this;
Expand Down Expand Up @@ -115,7 +117,7 @@ public function newInstance()
{
return (new static)
->setParent($this->parent)
->setParentField($this->parentField)
->setParentField($this->parentField, $this->parentIndex)
->setItems($this->items)
->setFields($this->fields)
->setFilled($this->filled);
Expand Down Expand Up @@ -245,7 +247,7 @@ protected function newField($handle, $config)
{
return (new Field($handle, $config))
->setParent($this->parent)
->setParentField($this->parentField);
->setParentField($this->parentField, $this->parentIndex);
}

private function getReferencedField(array $config): Field
Expand Down
24 changes: 11 additions & 13 deletions src/Fieldtypes/Bard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Statamic\Facades\Entry;
use Statamic\Facades\GraphQL;
use Statamic\Facades\Site;
use Statamic\Fields\Fields;
use Statamic\Fieldtypes\Bard\Augmentor;
use Statamic\GraphQL\Types\BardSetsType;
use Statamic\GraphQL\Types\BardTextType;
Expand Down Expand Up @@ -265,12 +264,12 @@ public function process($value)
$value = $this->unwrapInlineValue($value);
}

$structure = collect($value)->map(function ($row) {
$structure = collect($value)->map(function ($row, $index) {
if ($row['type'] !== 'set') {
return $row;
}

return $this->processRow($row);
return $this->processRow($row, $index);
})->all();

if ($this->shouldSaveHtml()) {
Expand Down Expand Up @@ -334,9 +333,9 @@ protected function shouldSaveHtml()
return $this->config('save_html');
}

protected function processRow($row)
protected function processRow($row, $index)
{
$row['attrs']['values'] = parent::processRow($row['attrs']['values']);
$row['attrs']['values'] = parent::processRow($row['attrs']['values'], $index);

if (array_get($row, 'attrs.enabled', true) === true) {
unset($row['attrs']['enabled']);
Expand Down Expand Up @@ -532,21 +531,20 @@ public function preload()

$existing = collect($value)->filter(function ($item) {
return $item['type'] === 'set';
})->mapWithKeys(function ($set) {
})->mapWithKeys(function ($set, $index) {
$values = $set['attrs']['values'];
$config = Arr::get($this->flattenedSetsConfig(), "{$values['type']}.fields", []);

return [$set['attrs']['id'] => (new Fields($config))->addValues($values)->meta()->put('_', '_')];
return [$set['attrs']['id'] => $this->fields($values['type'], $index)->addValues($values)->meta()->put('_', '_')];
})->toArray();

$defaults = collect($this->flattenedSetsConfig())->map(function ($set) {
return (new Fields($set['fields']))->all()->map(function ($field) {
$defaults = collect($this->flattenedSetsConfig())->map(function ($set, $handle) {
return $this->fields($handle)->all()->map(function ($field) {
return $field->fieldtype()->preProcess($field->defaultValue());
})->all();
})->all();

$new = collect($this->flattenedSetsConfig())->map(function ($set, $handle) use ($defaults) {
return (new Fields($set['fields']))->addValues($defaults[$handle])->meta()->put('_', '_');
return $this->fields($handle)->addValues($defaults[$handle])->meta()->put('_', '_');
})->toArray();

$previews = collect($existing)->map(function ($fields) {
Expand Down Expand Up @@ -587,14 +585,14 @@ public function preProcessValidatable($value)

$value = json_decode($value ?? '[]', true);

return collect($value)->map(function ($item) {
return collect($value)->map(function ($item, $index) {
if ($item['type'] !== 'set') {
return $item;
}

$values = $item['attrs']['values'];

$processed = $this->fields($values['type'])
$processed = $this->fields($values['type'], $index)
->addValues($values)
->preProcessValidatables()
->values()
Expand Down
4 changes: 2 additions & 2 deletions src/Fieldtypes/Bard/Augmentor.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ protected function augmentSets($value, $shallow)
{
$augmentMethod = $shallow ? 'shallowAugment' : 'augment';

return $value->map(function ($set) use ($augmentMethod) {
return $value->map(function ($set, $index) use ($augmentMethod) {
if (! Arr::get($this->fieldtype->flattenedSetsConfig(), "{$set['type']}.fields")) {
return $set;
}

$values = $this->fieldtype->fields($set['type'])->addValues($set)->{$augmentMethod}()->values()->all();
$values = $this->fieldtype->fields($set['type'], $index)->addValues($set)->{$augmentMethod}()->values()->all();

return array_merge($values, [RowId::handle() => $set[RowId::handle()] ?? null, 'type' => $set['type']]);
})->all();
Expand Down
32 changes: 16 additions & 16 deletions src/Fieldtypes/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public function filter()

public function process($data)
{
return collect($data)->map(function ($row) {
return $this->processRow($row);
return collect($data)->map(function ($row, $index) {
return $this->processRow($row, $index);
})->all();
}

private function processRow($row)
private function processRow($row, $index)
{
$fields = $this->fields()->addValues($row)->process()->values()->all();
$fields = $this->fields($index)->addValues($row)->process()->values()->all();

$row = array_merge([RowId::handle() => Arr::pull($row, '_id')], $row, $fields);

Expand All @@ -113,7 +113,7 @@ public function preProcess($data)

private function preProcessRow($row, $index)
{
$fields = $this->fields()->addValues($row)->preProcess()->values()->all();
$fields = $this->fields($index)->addValues($row)->preProcess()->values()->all();

$id = Arr::pull($row, RowId::handle()) ?? RowId::generate();

Expand All @@ -122,9 +122,9 @@ private function preProcessRow($row, $index)
]);
}

public function fields()
public function fields($index = -1)
{
return new Fields($this->config('fields'), $this->field()->parent(), $this->field());
return new Fields($this->config('fields'), $this->field()->parent(), $this->field(), $index);
}

public function rules(): array
Expand Down Expand Up @@ -154,7 +154,7 @@ public function extraRules(): array
protected function rowRules($data, $index)
{
$rules = $this
->fields()
->fields($index)
->addValues($data)
->validator()
->withContext([
Expand All @@ -174,9 +174,9 @@ protected function rowRuleFieldPrefix($index)

public function extraValidationAttributes(): array
{
$attributes = $this->fields()->validator()->attributes();
return collect($this->field->value())->map(function ($row, $index) {
$attributes = $this->fields($index)->validator()->attributes();

return collect($this->field->value())->map(function ($row, $index) use ($attributes) {
return collect($attributes)->except('_id')->mapWithKeys(function ($attribute, $handle) use ($index) {
return [$this->rowRuleFieldPrefix($index).'.'.$handle => $attribute];
});
Expand All @@ -190,8 +190,8 @@ public function preload()
return [
'defaults' => $this->defaultRowData()->all(),
'new' => $this->fields()->meta()->all(),
'existing' => collect($this->field->value())->mapWithKeys(function ($row) {
return [$row['_id'] => $this->fields()->addValues($row)->meta()];
'existing' => collect($this->field->value())->mapWithKeys(function ($row, $index) {
return [$row['_id'] => $this->fields($index)->addValues($row)->meta()];
})->toArray(),
];
}
Expand All @@ -217,8 +217,8 @@ private function performAugmentation($value, $shallow)
{
$method = $shallow ? 'shallowAugment' : 'augment';

return collect($value)->map(function ($row) use ($method) {
$values = $this->fields()->addValues($row)->{$method}()->values();
return collect($value)->map(function ($row, $index) use ($method) {
$values = $this->fields($index)->addValues($row)->{$method}()->values();

return new Values($values->merge([RowId::handle() => $row[RowId::handle()] ?? null])->all());
})->all();
Expand Down Expand Up @@ -247,8 +247,8 @@ private function gqlItemTypeName()

public function preProcessValidatable($value)
{
return collect($value)->map(function ($values) {
$processed = $this->fields()
return collect($value)->map(function ($values, $index) {
$processed = $this->fields($index)
->addValues($values)
->preProcessValidatables()
->values()
Expand Down
Loading
Loading