The best way is to create a new method in Nette\Application\UI\Form
use Contributte\Imagist\Bridge\Nette\Form\ImageUploadControl;
use Contributte\Imagist\Scope\Scope;
class Form extends \Nette\Application\UI\Form
{
public function addImageUpload(string $name, ?string $label = null, ?string $scope = null): ImageUploadControl
{
$control = $this[$name] = new ImageUploadControl($label);
if ($scope) {
$control->setScope(new Scope($scope));
}
return $control;
}
}
// usage
$form = new Form();
$form->addImageUpload('image');
if you do not want to register a new method, create new instance
$form = new Form();
$form['image'] = new ImageUploadControl($label);
use Contributte\Imagist\Entity\PersistentImage;
$form = new Form();
$form->addImageUpload('image')
->setDefaultValue(new PersistentImage('image.png'));
// or
$form->setDefaults([
'image' => new PersistentImage('image.png'),
]);
use Contributte\Imagist\Entity\PersistentImage;
assert($imagePreviewFactory instanceof Contributte\Imagist\Bridge\Nette\Form\Preview\ImagePreviewFactoryInterface); // inject
$form = new Form();
$form->addImageUpload('image')
->setPreview($imagePreviewFactory->create())
->setDefaultValue(new PersistentImage('image.png'));
use Contributte\Imagist\Bridge\Nette\Form\Remove\ImageRemove;
use Contributte\Imagist\Entity\PersistentImage;
$form = new Form();
$form->addImageUpload('image')
->setRemove(new ImageRemove('Check to delete image'))
->setDefaultValue(new PersistentImage('image.png'));