Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions docs/basic-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
```

14 changes: 13 additions & 1 deletion src/Controller/Component/SearchComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
Expand All @@ -52,6 +54,7 @@ class SearchComponent extends Component
'emptyValues' => [],
'modelClass' => null,
'formClass' => null,
'autoloadHelper' => true,
'events' => [
'Controller.startup' => 'startup',
'Controller.beforeRender' => 'beforeRender',
Expand Down Expand Up @@ -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.
*
Expand All @@ -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());
Expand Down
87 changes: 87 additions & 0 deletions tests/TestCase/Controller/Component/SearchComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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']);
}
}