Skip to content

Fix Datatables error in Media Manager#5296

Open
gmarcon wants to merge 1 commit intofisharebest:mainfrom
gmarcon:fix/datatables-undefined-array-key-2
Open

Fix Datatables error in Media Manager#5296
gmarcon wants to merge 1 commit intofisharebest:mainfrom
gmarcon:fix/datatables-undefined-array-key-2

Conversation

@gmarcon
Copy link
Contributor

@gmarcon gmarcon commented Jan 23, 2026

Fix Datatables error "Undefined array key 2" when using the "Manage media" utility.

To reproduce the error:

  • open "Manage media" for Local or External files
  • sort by the third column "Media object"
  • switch to "Unused files"
  • user sees a pop-up "DataTables warning: table id=wt-datatables-admin-media - Invalid JSON response. For more information about this error, please see https://datatables.net/tn/1"
  • instead of the JSON the backend returns "Undefined array key 2 …/app/Services/DatatablesService.php:79"

…edia" utility, triggered when setting the ordering for media object in "Local files" or "External files", and then switching to "Unused files"
@codecov
Copy link

codecov bot commented Jan 23, 2026

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 35.20%. Comparing base (f09ae71) to head (8ec6eb4).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
app/Http/RequestHandlers/ManageMediaData.php 0.00% 5 Missing ⚠️
app/Services/DatatablesService.php 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #5296      +/-   ##
============================================
- Coverage     35.20%   35.20%   -0.01%     
- Complexity    11236    11237       +1     
============================================
  Files          1159     1159              
  Lines         48108    48114       +6     
============================================
  Hits          16937    16937              
- Misses        31171    31177       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@BertKoor
Copy link
Contributor

BertKoor commented Jan 23, 2026

Is this issue #5283 or #5198 ? Sorry, those are already fixed...

@BertKoor
Copy link
Contributor

To answer my own question: perhaps related, but certainly not the very same issue!

@gmarcon
Copy link
Contributor Author

gmarcon commented Jan 24, 2026

I confirm that this is directly related to #5198.

It appears to me that the previous fix (intended to provide unique IDs for tables to prevent state sharing) does not fully resolve the issue in this context. The ID for the media admin table remains wt-datatables-admin-media regardless of whether one is browsing local, external, or unused files. Because the state is shared, DataTables attempts to apply a sort index from a previous view (e.g., Column 2) to the "Unused" view, which lacks that key.

This PR provides a more robust solution by:

  • Explicitly mapping the columns in the ManageMediaData handler so that any sorting attempt defaults to the first column (filename), which is the most logical sorting behavior for this view in my opinion.
  • Adding a backend safeguard in the DatatablesService to gracefully skip missing sort keys rather than triggering a 500 error.

@kiwi3685
Copy link
Contributor

This appears to be a good solution.

But is 'state sharing' really of any value on this table? Perhaps more effective to simply not apply it here.

@gmarcon
Copy link
Contributor Author

gmarcon commented Jan 26, 2026

I agree that state sharing between these specific views is problematic (that is why I run into the error!), but I find state saving quite valuable. Disabling it would cause the Media Manager to behave differently than other tables in webtrees. For example, users would lose their current page, search filters, and display length whenever the page refreshes - which isn't ideal for UX.

While we could use unique IDs for each view to prevent state "bleeding" between them, that adds more complexity to the frontend/handler logic. The proposed solution is a much simpler way to make the backend robust enough to handle the shared state gracefully without sacrificing the benefits of stateSave.

If useful, this is the documentation on state saving for datatables: https://datatables.net/examples/basic_init/state_save.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants