Skip to content
Open
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
19 changes: 14 additions & 5 deletions src/Fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
use Statamic\Facades\Blink;
use Statamic\Facades\Fieldset as FieldsetRepository;
use Statamic\Support\Arr;
use Statamic\Support\Traits\Hookable;

/**
* @phpstan-consistent-constructor
*/
class Fields
{
use Hookable;

protected $items;
protected $fields;
protected $parent;
Expand Down Expand Up @@ -216,15 +219,21 @@ public function preProcessValidatables()

public function augment()
{
return $this->newInstance()->setFields(
$this->fields->map->augment()
);
return $this->augmentFields(false);
}

public function shallowAugment()
{
return $this->newInstance()->setFields(
$this->fields->map->shallowAugment()
return $this->augmentFields(true);
}

private function augmentFields(bool $shallow)
{
$fields = $this->runHooksWith('augmenting', ['fields' => $this, 'shallow' => $shallow])->fields;
$method = $shallow ? 'shallowAugment' : 'augment';

return $fields->newInstance()->setFields(
$fields->all()->map->{$method}()
);
}

Expand Down
46 changes: 46 additions & 0 deletions tests/Fields/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,52 @@ public function preProcess($data)
], $preProcessed->values()->all());
}

#[Test]
public function it_runs_the_augmenting_hook_before_augmenting_fields()
{
FieldtypeRepository::shouldReceive('find')->with('fieldtype')->andReturn(new class extends Fieldtype
{
public function augment($data)
{
return $data.' augmented';
}

public function shallowAugment($data)
{
return $data.' shallow augmented';
}
});

FieldRepository::shouldReceive('find')->with('one')->andReturn(new Field('one', ['type' => 'fieldtype']));
FieldRepository::shouldReceive('find')->with('two')->andReturn(new Field('two', ['type' => 'fieldtype']));

$shallow = [];

Fields::hook('augmenting', function ($payload, $next) use (&$shallow) {
$shallow[] = $payload->shallow;

// Fill an empty field from the value of another one.
$payload->fields = $payload->fields->newInstance()->setFields(
$payload->fields->all()->map(fn ($field) => $field->handle() === 'two'
? $field->newInstance()->setValue($payload->fields->all()->get('one')->value().' again')
: $field
)
);

return $next($payload);
});

$fields = (new Fields([
['handle' => 'one', 'field' => 'one'],
['handle' => 'two', 'field' => 'two'],
]))->addValues(['one' => 'foo']);

$this->assertEquals(['one' => 'foo augmented', 'two' => 'foo again augmented'], $fields->augment()->values()->all());
$this->assertEquals(['one' => 'foo shallow augmented', 'two' => 'foo again shallow augmented'], $fields->shallowAugment()->values()->all());
$this->assertEquals([false, true], $shallow);
$this->assertEquals(['one' => 'foo', 'two' => null], $fields->values()->all());
}

#[Test]
public function it_augments_each_fields_values_by_its_fieldtype()
{
Expand Down
Loading