-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #656 from getformwork/feature/files-and-images-input
Improve image input, add new type file and multiple value variants (images, files)
- Loading branch information
Showing
21 changed files
with
424 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
use Formwork\Cms\App; | ||
use Formwork\Data\Exceptions\InvalidValueException; | ||
use Formwork\Fields\Exceptions\ValidationException; | ||
use Formwork\Fields\Field; | ||
use Formwork\Files\File; | ||
use Formwork\Files\FileCollection; | ||
use Formwork\Images\Image; | ||
use Formwork\Utils\Constraint; | ||
|
||
return function (App $app) { | ||
return [ | ||
'return' => function (Field $field): ?File { | ||
return $field->value() !== null | ||
? $field->getFiles()->get($field->value()) | ||
: null; | ||
}, | ||
|
||
'getFiles' => function (Field $field): FileCollection { | ||
if (!$field->has('options')) { | ||
$model = $field->parent()?->model(); | ||
|
||
if ($model === null || !method_exists($model, 'files')) { | ||
throw new InvalidValueException(sprintf('Field "%s" of type "%s" must have a model with files', $field->name(), $field->type())); | ||
} | ||
|
||
return $model->files(); | ||
} | ||
|
||
return $field->get('options'); | ||
}, | ||
|
||
'validate' => function (Field $field, $value): ?string { | ||
if (Constraint::isEmpty($value)) { | ||
return null; | ||
} | ||
|
||
if (!is_string($value)) { | ||
throw new ValidationException(sprintf('Invalid value for field "%s" of type "%s"', $field->name(), $field->type())); | ||
} | ||
|
||
return $value; | ||
}, | ||
|
||
'options' => function (Field $field): array { | ||
$collection = $field->getFiles(); | ||
|
||
if ($field->has('fileType')) { | ||
$collection = $collection->filter(static fn(File $file) => in_array($file->type(), (array) $field->get('fileType'), true)); | ||
} | ||
|
||
return $collection | ||
->map(static fn(File $file) => [ | ||
'value' => $file->name(), | ||
'icon' => 'file-' . $file->type(), | ||
'thumb' => $file instanceof Image ? $file->square(300, 'contain')->uri() : null, | ||
])->toArray(); | ||
}, | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
use Formwork\Cms\App; | ||
use Formwork\Data\Exceptions\InvalidValueException; | ||
use Formwork\Fields\Exceptions\ValidationException; | ||
use Formwork\Fields\Field; | ||
use Formwork\Files\File; | ||
use Formwork\Files\FileCollection; | ||
use Formwork\Images\Image; | ||
use Formwork\Utils\Constraint; | ||
|
||
return function (App $app) { | ||
return [ | ||
'getFiles' => function (Field $field): FileCollection { | ||
if (!$field->has('options')) { | ||
$model = $field->parent()?->model(); | ||
|
||
if ($model === null || !method_exists($model, 'files')) { | ||
throw new InvalidValueException(sprintf('Field "%s" of type "%s" must have a model with files', $field->name(), $field->type())); | ||
} | ||
|
||
return $model->files(); | ||
} | ||
|
||
return $field->get('options'); | ||
}, | ||
|
||
'toString' => function ($field) { | ||
return implode(', ', $field->value() ?? []); | ||
}, | ||
|
||
'return' => function (Field $field): FileCollection { | ||
return $field->getFiles()->filter(static fn(File $file) => in_array($file->name(), $field->value(), true)); | ||
}, | ||
|
||
'validate' => function (Field $field, $value): array { | ||
if (Constraint::isEmpty($value)) { | ||
return []; | ||
} | ||
|
||
if (is_string($value)) { | ||
$value = array_map(trim(...), explode(',', $value)); | ||
} | ||
|
||
if (!is_array($value)) { | ||
throw new ValidationException(sprintf('Invalid value for field "%s" of type "%s"', $field->name(), $field->type())); | ||
} | ||
|
||
if ($field->has('pattern')) { | ||
$value = array_filter($value, static fn($item): bool => Constraint::matchesRegex($item, $field->get('pattern'))); | ||
} | ||
|
||
if ($field->limit() !== null && count($value) > $field->limit()) { | ||
throw new ValidationException(sprintf('Field "%s" of type "%s" has a limit of %d items', $field->name(), $field->type(), $field->get('limit'))); | ||
} | ||
|
||
return array_values(array_filter($value)); | ||
}, | ||
|
||
'options' => function (Field $field): array { | ||
$collection = $field->getFiles(); | ||
|
||
if ($field->has('fileType')) { | ||
$collection = $collection->filter(static fn(File $file) => in_array($file->type(), (array) $field->get('fileType'), true)); | ||
} | ||
|
||
return $collection | ||
->map(static fn(File $file) => [ | ||
'value' => $file->name(), | ||
'icon' => 'file-' . $file->type(), | ||
'thumb' => $file instanceof Image ? $file->square(300, 'contain')->uri() : null, | ||
])->toArray(); | ||
}, | ||
|
||
'limit' => function (Field $field): ?int { | ||
return $field->get('limit', null); | ||
}, | ||
|
||
'isOrderable' => function ($field): bool { | ||
return $field->is('orderable', true); | ||
}, | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
use Formwork\Cms\App; | ||
use Formwork\Data\Exceptions\InvalidValueException; | ||
use Formwork\Fields\Exceptions\ValidationException; | ||
use Formwork\Fields\Field; | ||
use Formwork\Files\File; | ||
use Formwork\Files\FileCollection; | ||
use Formwork\Images\Image; | ||
use Formwork\Utils\Constraint; | ||
|
||
return function (App $app) { | ||
return [ | ||
'getImages' => function (Field $field): FileCollection { | ||
if (!$field->has('options')) { | ||
$model = $field->parent()?->model(); | ||
|
||
if ($model === null || !method_exists($model, 'files')) { | ||
throw new InvalidValueException(sprintf('Field "%s" of type "%s" must have a model with files', $field->name(), $field->type())); | ||
} | ||
|
||
$files = $model->files(); | ||
} else { | ||
$files = $field->get('options'); | ||
} | ||
|
||
return $files->filter(static fn(File $file) => $file instanceof Image); | ||
}, | ||
|
||
'toString' => function ($field) { | ||
return implode(', ', $field->value() ?? []); | ||
}, | ||
|
||
'return' => function (Field $field): FileCollection { | ||
return $field->getImages()->filter(static fn(File $file) => in_array($file->name(), $field->value(), true)); | ||
}, | ||
|
||
'validate' => function (Field $field, $value): array { | ||
if (Constraint::isEmpty($value)) { | ||
return []; | ||
} | ||
|
||
if (is_string($value)) { | ||
$value = array_map(trim(...), explode(',', $value)); | ||
} | ||
|
||
if (!is_array($value)) { | ||
throw new ValidationException(sprintf('Invalid value for field "%s" of type "%s"', $field->name(), $field->type())); | ||
} | ||
|
||
if ($field->has('pattern')) { | ||
$value = array_filter($value, static fn($item): bool => Constraint::matchesRegex($item, $field->get('pattern'))); | ||
} | ||
|
||
if ($field->limit() !== null && count($value) > $field->limit()) { | ||
throw new ValidationException(sprintf('Field "%s" of type "%s" has a limit of %d items', $field->name(), $field->type(), $field->get('limit'))); | ||
} | ||
|
||
return array_values(array_filter($value)); | ||
}, | ||
|
||
'options' => function (Field $field): array { | ||
return $field->getImages() | ||
->map(static fn(Image $image) => [ | ||
'value' => $image->name(), | ||
'icon' => 'image', | ||
'thumb' => $image->square(300, 'contain')->uri(), | ||
])->toArray(); | ||
}, | ||
|
||
'limit' => function (Field $field): ?int { | ||
return $field->get('limit', null); | ||
}, | ||
|
||
'isOrderable' => function ($field): bool { | ||
return $field->is('orderable', true); | ||
}, | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.