From 478498a8271cbd2774603062da8ca3d57a972f24 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 15 Nov 2025 19:33:45 +0530 Subject: [PATCH] Add option to autoload the search helper. --- docs/basic-configuration.md | 13 ++- src/Controller/Component/SearchComponent.php | 14 ++- .../Component/SearchComponentTest.php | 87 +++++++++++++++++++ 3 files changed, 105 insertions(+), 9 deletions(-) diff --git a/docs/basic-configuration.md b/docs/basic-configuration.md index d18cea5e..e826f513 100644 --- a/docs/basic-configuration.md +++ b/docs/basic-configuration.md @@ -132,18 +132,15 @@ query params. ### Adding a reset button dynamically The Search component will pass down the information on whether the query was modified by your search query string by setting `$_isSearch` view variable to -true here in this case. It also passes down a `$_searchParams` array of all query string params -that currently are part of the search. -You can use those to display certain elements on the page or use the following helper as -convenience wrapper. +true here in this case. It also passes down a `$_searchParams` array of all query +string params that currently are part of the search. -```php -// in AppView.php -$this->loadHelper('Search.Search'); +You can use the Search helper (which is autoloaded by default for the search actions +by the Search component). +```php // in your form template if ($this->Search->isSearch()) { echo $this->Search->resetLink(__('Reset'), ['class' => 'button']); } ``` - diff --git a/src/Controller/Component/SearchComponent.php b/src/Controller/Component/SearchComponent.php index 6bc2c54a..717ff63b 100644 --- a/src/Controller/Component/SearchComponent.php +++ b/src/Controller/Component/SearchComponent.php @@ -41,6 +41,8 @@ class SearchComponent extends Component * - `formClass` : The form class to use for the search form. Default `null`. * - `events`: List of events this component listens to. You can disable an * event by setting it to false. + * - `autoloadHelper` : Whether to autoload the SearchHelper for search actions, default `true`. + * Use `false` to disable automatic loading or set it to an array to configure the helper. * E.g. `'events' => ['Controller.beforeRender' => false]` * * @var array @@ -52,6 +54,7 @@ class SearchComponent extends Component 'emptyValues' => [], 'modelClass' => null, 'formClass' => null, + 'autoloadHelper' => true, 'events' => [ 'Controller.startup' => 'startup', 'Controller.beforeRender' => 'beforeRender', @@ -143,8 +146,9 @@ protected function getSearchForm(): ?Form } /** - * Populates the $_isSearch view variable based on the current request. + * Populates the $_isSearch & $_searchParams view variable based on the current request. * + * Also autoloads the SearchHelper if configured to do so (default). * You need to configure the modelClass config if you are not using the controller's * default modelClass property. * @@ -170,6 +174,14 @@ public function beforeRender(): void return; } + $helperConfig = $this->getConfig('autoloadHelper'); + if ($helperConfig) { + $controller->viewBuilder()->addHelper( + 'Search.Search', + is_array($helperConfig) ? $helperConfig : [], + ); + } + /** @var \Search\Model\Behavior\SearchBehavior $searchBehavior */ $searchBehavior = $model->getBehavior('Search'); $controller->set('_isSearch', $searchBehavior->isSearch()); diff --git a/tests/TestCase/Controller/Component/SearchComponentTest.php b/tests/TestCase/Controller/Component/SearchComponentTest.php index 68dffb93..91382c70 100644 --- a/tests/TestCase/Controller/Component/SearchComponentTest.php +++ b/tests/TestCase/Controller/Component/SearchComponentTest.php @@ -13,6 +13,7 @@ use ReflectionProperty; use Search\Controller\Component\SearchComponent; use Search\Test\TestApp\Form\SearchForm; +use Search\Test\TestApp\Model\Table\ArticlesTable; class SearchComponentTest extends TestCase { @@ -462,4 +463,90 @@ public function testEventsConfig() $result = $this->Search->implementedEvents(); $this->assertSame(['Controller.startup' => 'startup'], $result); } + + /** + * Test that autoloadHelper is true by default and loads the Search helper + * + * @return void + */ + public function testAutoloadHelperDefault() + { + $this->Controller->setRequest( + $this->Controller->getRequest()->withAttribute('params', [ + 'controller' => 'Articles', + 'action' => 'index', + ]), + ); + + $articles = $this->getTableLocator()->get('Articles', [ + 'className' => ArticlesTable::class, + ]); + $articles->addBehavior('Search.Search'); + + $this->Controller->getTableLocator()->set('Articles', $articles); + + $this->Search->beforeRender(); + + $helpers = $this->Controller->viewBuilder()->getHelpers(); + $this->assertArrayHasKey('Search', $helpers); + } + + /** + * Test that autoloadHelper can be disabled with false + * + * @return void + */ + public function testAutoloadHelperDisabled() + { + $this->Controller->setRequest( + $this->Controller->getRequest()->withAttribute('params', [ + 'controller' => 'Articles', + 'action' => 'index', + ]), + ); + + $articles = $this->getTableLocator()->get('Articles', [ + 'className' => ArticlesTable::class, + ]); + $articles->addBehavior('Search.Search'); + + $this->Controller->getTableLocator()->set('Articles', $articles); + + $this->Search->setConfig('autoloadHelper', false); + $this->Search->beforeRender(); + + $helpers = $this->Controller->viewBuilder()->getHelpers(); + $this->assertArrayNotHasKey('Search', $helpers); + } + + /** + * Test that autoloadHelper accepts array configuration for the helper + * + * @return void + */ + public function testAutoloadHelperWithConfig() + { + $this->Controller->setRequest( + $this->Controller->getRequest()->withAttribute('params', [ + 'controller' => 'Articles', + 'action' => 'index', + ]), + ); + + $articles = $this->getTableLocator()->get('Articles', [ + 'className' => ArticlesTable::class, + ]); + $articles->addBehavior('Search.Search'); + + $this->Controller->getTableLocator()->set('Articles', $articles); + + $helperConfig = ['additionalBlacklist' => ['foo']]; + $this->Search->setConfig('autoloadHelper', $helperConfig); + $this->Search->beforeRender(); + + $helpers = $this->Controller->viewBuilder()->getHelpers(); + $this->assertArrayHasKey('Search', $helpers); + $this->assertSame('Search.Search', $helpers['Search']['className']); + $this->assertSame(['foo'], $helpers['Search']['additionalBlacklist']); + } }