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

If you only have one asset container, use that as a default in form submissions #4974

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Forms/Uploaders/AssetsUploader.php
Expand Up @@ -2,8 +2,11 @@

namespace Statamic\Forms\Uploaders;

use Statamic\Exceptions\AssetContainerNotFoundException;
use Statamic\Facades\Asset;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Path;
use Statamic\Fieldtypes\Assets\UndefinedContainerException;
use Statamic\Support\Arr;

class AssetsUploader
Expand Down Expand Up @@ -76,14 +79,36 @@ protected function createAsset($file)
$path = Path::assemble($this->config->get('folder'), $file->getClientOriginalName());

$asset = Asset::make()
->container($this->config->get('container'))
->container($this->assetContainer())
->path(ltrim($path, '/'));

$asset->upload($file)->save();

return $asset;
}

/**
* Find the asset container.
*
* @return \Statamic\Contracts\Assets\AssetContainer
*/
protected function assetContainer()
{
if ($configured = $this->config->get('container')) {
if ($container = AssetContainer::find($configured)) {
return $container;
}

throw new AssetContainerNotFoundException($configured);
}

if (($containers = AssetContainer::all())->count() === 1) {
return $containers->first();
}

throw new UndefinedContainerException;
}

/**
* Determine if uploader should only upload a single file.
*
Expand Down