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

Incorrect media sorting when multiple() is used #15636

Open
Muffinman opened this issue Feb 19, 2025 · 1 comment
Open

Incorrect media sorting when multiple() is used #15636

Muffinman opened this issue Feb 19, 2025 · 1 comment
Labels

Comments

@Muffinman
Copy link

Package

filament/spatie-laravel-media-library-plugin

Package Version

v3.2.134

Laravel Version

v11.39.1

Livewire Version

No response

PHP Version

8.3.15

Problem description

Currently if using multiple() and reorderable() media, the sortable order seems to be stored inverted.

For example:

In form:

SpatieMediaLibraryFileUpload::make('photos')
    ->label('Photos')
    ->visibility('public')
    ->collection('photos')
    ->multiple()
    ->reorderable(),

In table:

SpatieMediaLibraryImageColumn::make('photo')
    ->collection('photos')
    ->conversion('thumb')
    ->limit(1)
    ->width(50)
    ->height(50),

Images are reordered so that 01JMF06G5WXKAVQGZC9NP8YXAT.jpg is first.

Image

In DB this is stored with 01JMF06G5WXKAVQGZC9NP8YXAT.jpg with the larger sort order in order_column (which seems potentially incorrect).

Image

When viewing field, the last one is shown rather than the first.

Image

Expected behavior

You would expect the table column to correctly return the first sorted media item in the same order as the field sorts them, however it currently returns the last.

Steps to reproduce

  1. Create a multiple() and reorderable() Media Field
  2. Re-order via the UI and save
  3. View the table field

Reproduction repository (issue will be closed if this is not valid)

https://github.com/Muffinman/filament-issue

Relevant log output

@Muffinman
Copy link
Author

To follow up on this, I tracked down the issue to the shouldAppendFiles property inside the Alpine file-upload.js component. If shouldAppendFiles is false then it reverses the sort order of the returned keys:

this.pond.on('reorderfiles', async (files) => {
    const orderedFileKeys = files
        .map((file) =>
            file.source instanceof File
                ? file.serverId
                : (this.uploadedFileIndex[file.source] ?? null),
        ) // file.serverId is null for a file that is not yet uploaded
        .filter((fileKey) => fileKey)

    await reorderUploadedFilesUsing(
        shouldAppendFiles
            ? orderedFileKeys
            : orderedFileKeys.reverse(),
    )
})

However this reversed order is not taken into account in the Livewire component:

$this->reorderUploadedFilesUsing(static function (SpatieMediaLibraryFileUpload $component, ?Model $record, array $state): array {
    $uuids = array_filter(array_values($state));

    $mediaClass = ($record && method_exists($record, 'getMediaModel')) ? $record->getMediaModel() : null;
    $mediaClass ??= config('media-library.media_model', Media::class);

    $mappedIds = $mediaClass::query()->whereIn('uuid', $uuids)->pluck(app($mediaClass)->getKeyName(), 'uuid')->toArray();

    $mediaClass::setNewOrder([
        ...array_flip($uuids),
        ...$mappedIds,
    ]);

    return $state;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo
Development

No branches or pull requests

1 participant