Skip to content

Commit

Permalink
Merge 4.x into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI authored Apr 11, 2024
2 parents ed57f00 + 8fd5658 commit 9b7a118
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 44 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [4.30.1](https://github.com/sonata-project/SonataAdminBundle/compare/4.30.0...4.30.1) - 2024-04-10
### Fixed
- [[#8175](https://github.com/sonata-project/SonataAdminBundle/pull/8175)] `Filter::getLabelTranslationParameters` implementation ([@VincentLanglet](https://github.com/VincentLanglet))

## [4.30.0](https://github.com/sonata-project/SonataAdminBundle/compare/4.29.3...4.30.0) - 2024-04-10
### Added
- [[#8170](https://github.com/sonata-project/SonataAdminBundle/pull/8170)] SearchHandlerInterface ([@VincentLanglet](https://github.com/VincentLanglet))

### Deprecated
- [[#8172](https://github.com/sonata-project/SonataAdminBundle/pull/8172)] Not implementing `FilterInterface::showFilter` ([@VincentLanglet](https://github.com/VincentLanglet))
- [[#8172](https://github.com/sonata-project/SonataAdminBundle/pull/8172)] Not implementing `FilterInterface::getLabelTranslationParameters` ([@VincentLanglet](https://github.com/VincentLanglet))
- [[#8172](https://github.com/sonata-project/SonataAdminBundle/pull/8172)] Not implementing `FilterInterface::withAdvancedFilter` ([@VincentLanglet](https://github.com/VincentLanglet))

### Fixed
- [[#8173](https://github.com/sonata-project/SonataAdminBundle/pull/8173)] Active state in side menu for nested child admins ([@VincentLanglet](https://github.com/VincentLanglet))
- [[#8172](https://github.com/sonata-project/SonataAdminBundle/pull/8172)] `label_translation_parameters` option usage ([@VincentLanglet](https://github.com/VincentLanglet))

## [4.29.3](https://github.com/sonata-project/SonataAdminBundle/compare/4.29.2...4.29.3) - 2024-01-22
### Fixed
- [[#8149](https://github.com/sonata-project/SonataAdminBundle/pull/8149)] Fix possible null error in search ([@core23](https://github.com/core23))
Expand Down
4 changes: 2 additions & 2 deletions src/Block/AdminSearchBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Filter\FilterInterface;
use Sonata\AdminBundle\Search\SearchableFilterInterface;
use Sonata\AdminBundle\Search\SearchHandler;
use Sonata\AdminBundle\Search\SearchHandlerInterface;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
Expand All @@ -37,7 +37,7 @@ final class AdminSearchBlockService extends AbstractBlockService
public function __construct(
Environment $twig,
private Pool $pool,
private SearchHandler $searchHandler,
private SearchHandlerInterface $searchHandler,
private TemplateRegistryInterface $templateRegistry,
private string $emptyBoxesOption,
private string $adminRoute
Expand Down
23 changes: 22 additions & 1 deletion src/Datagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,28 @@ private function buildForm(): FormInterface
// NEXT_MAJOR: Keep the if part.
if (method_exists($filter, 'getFormOptions')) {
$type = FilterDataType::class;
$options = $filter->getFormOptions();

// NEXT_MAJOR: Keep the if part.
if (method_exists($filter, 'getLabelTranslationParameters')) {
$labelTranslationParameters = $filter->getLabelTranslationParameters();
} else {
@trigger_error(
'Not implementing "getLabelTranslationParameters()" is deprecated since sonata-project/admin-bundle 4.30'
.' and will throw an error in 5.0.',
\E_USER_DEPRECATED
);

$labelTranslationParameters = $filter->getOption('label_translation_parameters');
}

$defaultFormOptions = [
'label' => $filter->getLabel(),
'label_translation_parameters' => $labelTranslationParameters,
'translation_domain' => $filter->getTranslationDomain(),
'field_type' => $filter->getFieldType(),
'field_options' => $filter->getFieldOptions(),
];
$options = array_merge($defaultFormOptions, $filter->getFormOptions());
} else {
@trigger_error(
'Not implementing "getFormOptions()" is deprecated since sonata-project/admin-bundle 4.15'
Expand Down
18 changes: 18 additions & 0 deletions src/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ public function getRenderSettings(): array
];
}

final public function showFilter(): ?bool
{
return $this->getOption('show_filter');
}

/**
* @return array<string, mixed>
*/
final public function getLabelTranslationParameters(): array
{
return $this->getOption('label_translation_parameters', []);
}

final public function withAdvancedFilter(): bool
{
return $this->getOption('advanced_filter');
}

final protected function setActive(bool $active): void
{
$this->active = $active;
Expand Down
3 changes: 3 additions & 0 deletions src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* @author Thomas Rabaix <[email protected]>
*
* @method array getFormOptions();
* @method bool|null showFilter();
* @method array getLabelTranslationParameters();
* @method bool withAdvancedFilter();
*/
interface FilterInterface
{
Expand Down
43 changes: 27 additions & 16 deletions src/Menu/Matcher/Voter/AdminVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,43 @@ public function __construct(

public function matchItem(ItemInterface $item): ?bool
{
$admin = $item->getExtra('admin');

$request = $this->requestStack->getMainRequest();
if (null === $request) {
return null;
}

if ($admin instanceof AdminInterface
$admin = $item->getExtra('admin');
if (
$admin instanceof AdminInterface
&& $admin->hasRoute('list') && $admin->hasAccess('list')
&& null !== $request
&& $this->match($admin, $request->get('_sonata_admin'))
) {
$requestCode = $request->get('_sonata_admin');

if ($admin->getCode() === $requestCode) {
return true;
}

foreach ($admin->getChildren() as $child) {
if ($child->getBaseCodeRoute() === $requestCode) {
return true;
}
}
return true;
}

$route = $item->getExtra('route');
if (null !== $route && null !== $request && $route === $request->get('_route')) {
if (null !== $route && $route === $request->get('_route')) {
return true;
}

return null;
}

/**
* @param AdminInterface<object> $admin
*/
private function match(AdminInterface $admin, mixed $requestCode): bool
{
if ($admin->getBaseCodeRoute() === $requestCode) {
return true;
}

foreach ($admin->getChildren() as $child) {
if ($this->match($child, $requestCode)) {
return true;
}
}

return false;
}
}
3 changes: 3 additions & 0 deletions src/Resources/config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Sonata\AdminBundle\Request\AdminFetcherInterface;
use Sonata\AdminBundle\Route\AdminPoolLoader;
use Sonata\AdminBundle\Search\SearchHandler;
use Sonata\AdminBundle\Search\SearchHandlerInterface;
use Sonata\AdminBundle\SonataConfiguration;
use Sonata\AdminBundle\Templating\TemplateRegistry;
use Sonata\AdminBundle\Translator\BCLabelTranslatorStrategy;
Expand Down Expand Up @@ -124,6 +125,8 @@

->set('sonata.admin.search.handler', SearchHandler::class)

->alias(SearchHandlerInterface::class, 'sonata.admin.search.handler')

->set('sonata.admin.controller.crud', CRUDController::class)
->public()
->tag('container.service_subscriber')
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/views/Block/block_search_result.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ file that was distributed with this source code.
<div class="matches">
{% for name, filter in filters %}
<a class="label label-primary" href="{{ admin.generateUrl('list', {'filter': {(filter.formName): {'value': term}}}) }}">
{% if filter.option('translation_domain') is same as(false) %}
{{ filter.option('label') }}
{% if filter.translationDomain is same as(false) %}
{{ filter.label }}
{% else %}
{{ filter.option('label')|trans({}, filter.option('translation_domain', admin.translationDomain)) }}
{{ filter.label|trans({}, filter.translationDomain ?? admin.translationDomain) }}
{% endif %}
</a>
{% endfor %}
Expand Down
16 changes: 11 additions & 5 deletions src/Resources/views/CRUD/base_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ file that was distributed with this source code.
{% endblock %}

{% block list_filters_actions %}
{% set displayableFilters = admin.datagrid.filters|filter(filter => filter.option('show_filter') is not same as (false)) %}
{# NEXT_MAJOR: Remove |default(filter.option('show_filter')) #}
{% set displayableFilters = admin.datagrid.filters|filter(filter => filter.showFilter|default(filter.option('show_filter')) is not same as (false)) %}
{%- if displayableFilters|length %}
<ul class="nav navbar-nav navbar-right">

Expand All @@ -270,14 +271,17 @@ file that was distributed with this source code.

<ul class="dropdown-menu dropdown-menu-scrollable" role="menu">
{% for filter in displayableFilters %}
{% set filterDisplayed = filter.isActive() or filter.option('show_filter') is same as (true) %}
{# NEXT_MAJOR: Remove |default(filter.option('show_filter')) #}
{% set filterDisplayed = filter.isActive() or filter.showFilter|default(filter.option('show_filter')) is same as (true) %}
<li>
<a href="#" class="sonata-toggle-filter sonata-ba-action" filter-target="filter-{{ admin.uniqid }}-{{ filter.name }}" filter-container="filter-container-{{ admin.uniqid() }}">
<i class="far {{ filterDisplayed ? 'fa-check-square' : 'fa-square' }}"></i>
{% if filter.label is not same as(false) %}
{% if filter.translationDomain is same as(false) %}
{{ filter.label }}
{% else %}
{% elseif filter.labelTranslationParameters is defined %}
{{ filter.label|trans(filter.labelTranslationParameters, filter.translationDomain ?? admin.translationDomain) }}
{% else %} {# NEXT_MAJOR: remove the else part and change elseif to else #}
{{ filter.label|trans(filter.option('label_translation_parameters', {}), filter.translationDomain ?? admin.translationDomain) }}
{% endif %}
{% endif %}
Expand Down Expand Up @@ -310,7 +314,8 @@ file that was distributed with this source code.
<div class="col-sm-9">
{% set withAdvancedFilter = false %}
{% for filter in admin.datagrid.filters %}
{% set filterDisplayed = filter.isActive() or filter.option('show_filter') is same as (true) %}
{# NEXT_MAJOR: Remove |default(filter.option('show_filter')) #}
{% set filterDisplayed = filter.isActive() or filter.showFilter|default(filter.option('show_filter')) is same as (true) %}
{% set filterCanBeDisplayed = filter.option('show_filter') is not same as(false) %}
<div class="form-group {% block sonata_list_filter_group_class %}{% endblock %}" id="filter-{{ admin.uniqid }}-{{ filter.name }}" sonata-filter="{{ filterCanBeDisplayed ? 'true' : 'false' }}" style="display: {% if filterDisplayed %}block{% else %}none{% endif %}">
{% if filter.label is not same as(false) %}
Expand Down Expand Up @@ -343,7 +348,8 @@ file that was distributed with this source code.
{% endif %}
</div>

{% if filter.option('advanced_filter') %}
{# NEXT_MAJOR: Remove |default(filter.option('advanced_filter')) #}
{% if filter.withAdvancedFilter|default(filter.option('advanced_filter')) %}
{% set withAdvancedFilter = true %}
{% endif %}
{% endfor %}
Expand Down
10 changes: 1 addition & 9 deletions src/Search/SearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,18 @@

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\PagerInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Filter\FilterInterface;

/**
* @author Thomas Rabaix <[email protected]>
*/
final class SearchHandler
final class SearchHandler implements SearchHandlerInterface
{
/**
* @var array<string, bool>
*/
private array $adminsSearchConfig = [];

/**
* @throws \RuntimeException
*
* @phpstan-template T of object
* @phpstan-param AdminInterface<T> $admin
* @phpstan-return PagerInterface<ProxyQueryInterface<T>>|null
*/
public function search(AdminInterface $admin, string $term, int $page = 0, int $offset = 20): ?PagerInterface
{
// If the search is disabled for the whole admin, skip any further processing.
Expand Down
30 changes: 30 additions & 0 deletions src/Search/SearchHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Search;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\PagerInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;

interface SearchHandlerInterface
{
/**
* @throws \RuntimeException
*
* @phpstan-template T of object
* @phpstan-param AdminInterface<T> $admin
* @phpstan-return PagerInterface<ProxyQueryInterface<T>>|null
*/
public function search(AdminInterface $admin, string $term, int $page = 0, int $offset = 20): ?PagerInterface;
}
Loading

0 comments on commit 9b7a118

Please sign in to comment.