Skip to content

Commit

Permalink
Merge pull request #17 from MatthiasvanPijkeren/main
Browse files Browse the repository at this point in the history
Added option for localized
  • Loading branch information
tdwesten authored May 2, 2024
2 parents 630a1cc + 70ec46e commit 5083ffe
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PageBlueprint extends Blueprint
Text::make('title')
->displayName('Title')
->instructions('The title of the page')
->localizable()
->required()
]),
]),
Expand Down
19 changes: 14 additions & 5 deletions src/FieldTypes/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Field implements Renderable

protected $fields;

protected $localizable = false;

/**
* @var Collection
*/
Expand Down Expand Up @@ -83,8 +85,8 @@ public function toArray()
// Remove empty values
$content['field'] = collect($content['field'])->filter(function ($item) {
return is_array($item)
? ! empty($item)
: ! in_array($item, [null, ''], true);
? !empty($item)
: !in_array($item, [null, ''], true);
})->all();

// Sort keys
Expand All @@ -111,6 +113,7 @@ public function fieldDefaults(): Collection
'visibility' => $this->visibility->value,
'validate' => $this->validate->toArray(),
'width' => $this->width,
'localizable' => $this->localizable,
]);
}

Expand All @@ -129,13 +132,12 @@ public function withAttributes(array $attributes)
$this->customAttributes = $attributes;

return $this;

}

public function getHandle()
{
if ($this->prefix) {
return $this->prefix.'.'.$this->handle;
return $this->prefix . '.' . $this->handle;
}

return $this->handle;
Expand Down Expand Up @@ -286,6 +288,13 @@ public function default($default)
return $this;
}

public function localizable($localizable = true)
{
$this->localizable = $localizable;

return $this;
}

public function fieldsToArray()
{
$this->fields = FieldParser::parseMixedFieldsToFlatCollection($this->fields);
Expand All @@ -298,7 +307,7 @@ public function fieldsToArray()
public function validateField()
{
if ($this->getHandle() === null || empty($this->getHandle())) {
throw new BlueprintRenderException('[ '.get_called_class().'] Field handle cannot be empty');
throw new BlueprintRenderException('[ ' . get_called_class() . '] Field handle cannot be empty');
}
}
}
1 change: 1 addition & 0 deletions tests/Unit/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'input_type' => 'text',
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'replicator_preview' => true,
'type' => 'text',
'visibility' => 'visible',
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@
expect($field->toArray()['field']['hide_display'])->toBe(true);
});

test('Can set localizable', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field->localizable();

expect($field->toArray()['field']['localizable'])->toBe(true);
});

test('Can set validation to sometimes', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Fields/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'input_type' => 'text',
'instructions_position' => 'above',
'listable' => true,
'localizable' => false,
'replicator_preview' => true,
'type' => 'text',
'visibility' => 'visible',
Expand All @@ -43,6 +44,7 @@
'hide_display' => false,
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'replicator_preview' => true,
'type' => 'group',
'visibility' => 'visible',
Expand Down Expand Up @@ -89,6 +91,7 @@
'input_type' => 'text',
'instructions_position' => 'above',
'listable' => true,
'localizable' => false,
'replicator_preview' => true,
'type' => 'text',
'visibility' => 'visible',
Expand All @@ -100,6 +103,7 @@
'hide_display' => false,
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'replicator_preview' => true,
'type' => 'group',
'visibility' => 'visible',
Expand All @@ -110,6 +114,7 @@
'hide_display' => false,
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'replicator_preview' => true,
'type' => 'group',
'visibility' => 'visible',
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Fields/ReplicatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'hide_display' => false,
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'previews' => true,
'replicator_preview' => true,
'sets' => [
Expand All @@ -80,6 +81,7 @@
'input_type' => 'text',
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'replicator_preview' => true,
'type' => 'text',
'visibility' => 'visible',
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Fields/TermsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'mode' => 'default',
'replicator_preview' => true,
'required' => true,
Expand Down Expand Up @@ -56,6 +57,7 @@
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'max_items' => 5,
'mode' => 'default',
'replicator_preview' => true,
Expand Down Expand Up @@ -92,6 +94,7 @@
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'mode' => 'default',
'replicator_preview' => true,
'required' => true,
Expand Down Expand Up @@ -129,6 +132,7 @@
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'mode' => 'select',
'replicator_preview' => true,
'required' => true,
Expand Down Expand Up @@ -167,6 +171,7 @@
'instructions' => 'The counties of the school',
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'mode' => 'select',
'replicator_preview' => true,
'required' => true,
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Fields/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'instructions' => 'The name of the county',
'instructions_position' => 'above',
'listable' => 'visible',
'localizable' => false,
'replicator_preview' => true,
'required' => true,
'type' => 'text',
Expand Down

0 comments on commit 5083ffe

Please sign in to comment.