Skip to content

feat(SonataAdmin): Add search bar in media library#132

Open
loic425 wants to merge 2 commits into
jolicode:mainfrom
loic425:feat-sonata-admin/search-bar-in-media-library
Open

feat(SonataAdmin): Add search bar in media library#132
loic425 wants to merge 2 commits into
jolicode:mainfrom
loic425:feat-sonata-admin/search-bar-in-media-library

Conversation

@loic425
Copy link
Copy Markdown
Contributor

@loic425 loic425 commented May 22, 2026

Based on #129

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 search handling (recursive listing + filter callbacks) to MediaAdminController::list() and exposes the value to the template.
  • Adds a search form in list.html.twig and supporting modal AJAX/search logic in mediaSelector.js (with new getSearchUrl/setupSearch helpers and a guard against re-POSTing the search form).
  • Adds action.search / media.search_label translations (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);
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.

2 participants