feat(SonataAdmin): Add search bar in media library#132
Open
loic425 wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a search bar to the SonataAdmin media library. In explore mode the form submits a search GET parameter; in the choose-file modal the search is wired via AJAX in mediaSelector.js. The controller now performs recursive listing of directories and media when search is set, filtering on a case-insensitive path substring match (mirroring the existing Sylius bridge pattern).
Changes:
- Adds
searchhandling (recursive listing + filter callbacks) toMediaAdminController::list()and exposes the value to the template. - Adds a search form in
list.html.twigand supporting modal AJAX/search logic inmediaSelector.js(with newgetSearchUrl/setupSearchhelpers and a guard against re-POSTing the search form). - Adds
action.search/media.search_labeltranslations (FR + EN), small CSS rule for.joli-media-search, and rebuilds Sonata Encore assets (manifest/entrypoints + hashed bundles).
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Bridge/SonataAdmin/src/Controller/MediaAdminController.php | Reads search query and switches to recursive filtered listing for directories and paginated media. |
| src/Bridge/SonataAdmin/templates/list.html.twig | Renders the search form for explore and choose templates, with explore submitting a GET form and choose using a data-component="media-search" AJAX form. |
| src/Bridge/SonataAdmin/assets/js/components/mediaSelector.js | Tracks currentSearchValue/currentFolder, wires modal search submission, and short-circuits the generic submit handler for the search form. |
| src/Bridge/SonataAdmin/assets/styles/media-choice.css | Adds simple padding for .joli-media-search. |
| src/Bridge/SonataAdmin/translations/JoliMediaSonataAdminBundle.{en,fr}.yaml | Adds action.search and media.search_label; FR also straightens curly quotes in deleted_success. |
| src/Bridge/SonataAdmin/public/{manifest.json,entrypoints.json,.css,.js} | Rebuilt Encore assets reflecting the new JS/CSS. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+90
to
+101
| {% if 'choose' in base_template or 'explore' in base_template %} | ||
| <div class="joli-media-search"> | ||
| <form class="joli-media-search-form" {% if 'explore' in base_template %}method="GET" action="{{ path(app.request.attributes.get('_route'), { key: current_key }) }}"{% else %}data-component="media-search"{% endif %}> | ||
| <div class="input-group"> | ||
| <input type="search" class="form-control joli-media-search-input" {% if 'explore' in base_template %}name="search"{% endif %} placeholder="{{ 'media.search_label'|trans }}" value="{{ search }}" autocomplete="off"> | ||
| <span class="input-group-btn"> | ||
| <button class="btn btn-default joli-media-search-btn" type="submit"><i class="fas fa-search"></i> {{ 'action.search'|trans }}</button> | ||
| </span> | ||
| </div> | ||
| </form> | ||
| </div> | ||
| {% endif %} |
|
|
||
| {% block content %} | ||
| {{ block('content_header') }} | ||
| {% if 'choose' in base_template or 'explore' in base_template %} |
Comment on lines
+251
to
+266
| $searchValue = $request->query->getString('search', ''); | ||
| $hasSearch = '' !== $searchValue; | ||
|
|
||
| try { | ||
| $trashPath = $this->getOriginalStorage()->getTrashPath(); | ||
|
|
||
| if ($trashPath === $currentKey || str_starts_with($currentKey, $trashPath . '/')) { | ||
| throw new ForbiddenPathException($trashPath); | ||
| } | ||
|
|
||
| $directories = $this->getOriginalStorage()->listDirectories($currentKey, recursive: false); | ||
| natcasesort($directories); | ||
| $dirFilter = null; | ||
| if ($hasSearch) { | ||
| $dirFilter = static fn (string $a): bool => str_contains(strtolower($a), strtolower($searchValue)); | ||
| } | ||
|
|
||
| $directories = $this->getOriginalStorage()->listDirectories($currentKey, recursive: $hasSearch, filter: $dirFilter); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Based on #129