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

[5.x] Allow asset container order to be specified #10177

Closed
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
1 change: 1 addition & 0 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'asset_container_handle_instructions' => 'Used to reference this container on the frontend. It\'s non-trivial to change later.',
'asset_container_intro' => 'Media and document files live in folders on the server or other file storage services. Each of these locations is called a container.',
'asset_container_move_instructions' => 'When enabled will allow users to move files around inside this container.',
'asset_container_order_instructions' => 'Containers are sorted ascending by this value in the sidebar and asset tabs.',
'asset_container_quick_download_instructions' => 'When enabled will add a quick download button in the Asset Manager.',
'asset_container_rename_instructions' => 'When enabled will allow users to rename the files in this container.',
'asset_container_source_preset_instructions' => 'Uploaded images will be permanently processed using this preset.',
Expand Down
12 changes: 12 additions & 0 deletions src/Assets/AssetContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AssetContainer implements Arrayable, ArrayAccess, AssetContainerContract,
protected $withEvents = true;
protected $sortField;
protected $sortDirection;
protected $order;
protected $validation;

public function id($id = null)
Expand Down Expand Up @@ -640,6 +641,7 @@ public function fileData()
'create_folders' => $this->createFolders,
'source_preset' => $this->sourcePreset,
'warm_presets' => $this->warmPresets,
'order' => $this->order,
'validate' => $this->validation,
];

Expand Down Expand Up @@ -671,6 +673,16 @@ public function searchIndex($index = null)
->args(func_get_args());
}

public function order($order = null)
{
return $this
->fluentlyGetOrSet('order')
->getter(function ($order) {
return $order ?? 1;
})
->args(func_get_args());
}

public static function __callStatic($method, $parameters)
{
return Facades\AssetContainer::{$method}(...$parameters);
Expand Down
2 changes: 1 addition & 1 deletion src/CP/Navigation/CoreNav.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function makeContentSection()
->icon('/assets')
->can('index', AssetContainer::class)
->children(function () {
return AssetContainerAPI::all()->sortBy->title()->map(function ($assetContainer) {
return AssetContainerAPI::all()->map(function ($assetContainer) {
return Nav::item($assetContainer->title())
->url($assetContainer->showUrl())
->can('view', $assetContainer);
Expand Down
21 changes: 19 additions & 2 deletions src/Http/Controllers/CP/Assets/AssetContainersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function show($container)

public function index(Request $request)
{
$containers = AssetContainer::all()->sortBy->title()->filter(function ($container) {
$containers = AssetContainer::all()->filter(function ($container) {
return User::current()->can('view', $container);
})->map(function ($container) {
return [
Expand Down Expand Up @@ -65,6 +65,7 @@ public function edit($container)
'source_preset' => $container->sourcePreset(),
'warm_intelligent' => $intelligent = $container->warmsPresetsIntelligently(),
'warm_presets' => $intelligent ? [] : $container->warmPresets(),
'order' => $container->order() == 1 ? '' : $container->order(),
'validation' => $container->validationRules(),
];

Expand Down Expand Up @@ -101,6 +102,7 @@ public function update(Request $request, $container)
->createFolders($values['create_folders'])
->sourcePreset($values['source_preset'])
->warmPresets($values['warm_intelligent'] ? null : $values['warm_presets'])
->order($values['order'])
->validationRules($values['validation'] ?? null);

$container->save();
Expand Down Expand Up @@ -149,7 +151,8 @@ public function store(Request $request)
->allowUploads($values['allow_uploads'])
->createFolders($values['create_folders'])
->sourcePreset($values['source_preset'])
->warmPresets($values['warm_intelligent'] ? null : $values['warm_presets']);
->warmPresets($values['warm_intelligent'] ? null : $values['warm_presets'])
->order($values['order']);

$container->save();

Expand Down Expand Up @@ -309,6 +312,20 @@ protected function formBlueprint($container = null)
],
]);

$fields = array_merge($fields, [
'display' => [
'display' => __('Display'),
'fields' => [
'order' => [
'type' => 'text',
'display' => __('Order'),
'instructions' => __('statamic::messages.asset_container_order_instructions'),
'validate' => 'numeric',
],
],
],
]);

return Blueprint::makeFromTabs($fields);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait RedirectsToFirstAssetContainer
{
public function redirectToFirstContainer()
{
$containers = AssetContainer::all()->sortBy->title()->filter(function ($container) {
$containers = AssetContainer::all()->filter(function ($container) {
return User::current()->can('view', $container);
});

Expand Down
2 changes: 1 addition & 1 deletion src/Stache/Repositories/AssetContainerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function all(): Collection
{
$keys = $this->store->paths()->keys();

return $this->store->getItems($keys);
return $this->store->getItems($keys)->sortBy(fn ($container) => [$container->order(), $container->title()]);
}

public function find($id): ?AssetContainer
Expand Down
1 change: 1 addition & 0 deletions src/Stache/Stores/AssetContainersStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function makeItemFromFile($path, $contents)
->searchIndex(Arr::get($data, 'search_index'))
->sortField(Arr::get($data, 'sort_by'))
->sortDirection(Arr::get($data, 'sort_dir'))
->order(Arr::get($data, 'order'))
->validationRules(Arr::get($data, 'validate'));
}
}
12 changes: 12 additions & 0 deletions tests/Assets/AssetContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1076,4 +1076,16 @@ private function containerWithDisk($fixture = 'container')

return $container;
}

/** @test */
public function it_gets_and_sets_the_order()
{
$container = new AssetContainer;
$this->assertSame(1, $container->order());

$return = $container->order(3);

$this->assertEquals($container, $return);
$this->assertSame(3, $container->order());
}
}
59 changes: 59 additions & 0 deletions tests/Feature/AssetContainers/ListAssetContainersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,65 @@ public function it_loads_a_view_when_requested_normally()
->assertViewHas('containers', $this->containerArray());
}

/** @test */
public function it_lists_container_by_order_specified()
{
$this->setTestRoles(['test' => ['access cp', 'view one assets', 'view two assets', 'view three assets']]);
$user = User::make()->assignRole('test')->save();
AssetContainer::make('one')->order(3)->save();
AssetContainer::make('two')->order(2)->save();
AssetContainer::make('three')->order(1)->save();

$this
->actingAs($user)
->getJson(cp_route('asset-containers.index'))
->assertSuccessful()
->assertJson([
[
'id' => 'three',
'title' => 'Three',
'allow_downloading' => true,
'allow_moving' => true,
'allow_renaming' => true,
'allow_uploads' => true,
'create_folders' => true,
'edit_url' => 'http://localhost/cp/asset-containers/three/edit',
'delete_url' => 'http://localhost/cp/asset-containers/three',
'blueprint_url' => 'http://localhost/cp/asset-containers/three/blueprint',
'can_edit' => false,
'can_delete' => false,
],
[
'id' => 'two',
'title' => 'Two',
'allow_downloading' => true,
'allow_moving' => true,
'allow_renaming' => true,
'allow_uploads' => true,
'create_folders' => true,
'edit_url' => 'http://localhost/cp/asset-containers/two/edit',
'delete_url' => 'http://localhost/cp/asset-containers/two',
'blueprint_url' => 'http://localhost/cp/asset-containers/two/blueprint',
'can_edit' => false,
'can_delete' => false,
],
[
'id' => 'one',
'title' => 'One',
'allow_downloading' => true,
'allow_moving' => true,
'allow_renaming' => true,
'allow_uploads' => true,
'create_folders' => true,
'edit_url' => 'http://localhost/cp/asset-containers/one/edit',
'delete_url' => 'http://localhost/cp/asset-containers/one',
'blueprint_url' => 'http://localhost/cp/asset-containers/one/blueprint',
'can_edit' => false,
'can_delete' => false,
],
]);
}

public function containerArray()
{
return [
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/GraphQL/AssetContainersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function it_queries_asset_containers()
->post('/graphql', ['query' => $query])
->assertGqlOk()
->assertExactJson(['data' => ['assetContainers' => [
['handle' => 'public', 'title' => 'Public'],
['handle' => 'private', 'title' => 'Private'],
['handle' => 'public', 'title' => 'Public'],
]]]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Search/Searchables/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function it_gets_assets($locale, $config, $expected)
$provider = $this->makeProvider($locale, $config);

// Check if it provides the expected assets.
$this->assertEquals($expected, $provider->provide()->map->filename()->all());
$this->assertEquals($expected, $provider->provide()->map->filename()->sort()->values()->all());

// Check if the assets are contained by the provider or not.
foreach (Asset::all() as $asset) {
Expand Down
Loading