Skip to content

Commit

Permalink
Merge pull request #47 from wernerkrauss/fix-php72
Browse files Browse the repository at this point in the history
SS4 / PHP7.2: Object is a reserved word
  • Loading branch information
g4b0 committed Apr 10, 2020
2 parents bf1c984 + da2ca52 commit 1305d68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ getContentFields()):
*Note: `getSearchFilterByCallback()` is an optional filter. If you don't plan on calculating any value to determine a returned `true` or `false` value it is suggested you don't add this function to your `DataObject` or `Page` type.*

```php
use g4b0\SearchableDataObjects\Searchable;

class DoNews extends DataObject implements Searchable {

private static $db = array(
Expand Down Expand Up @@ -158,10 +160,10 @@ Extend Page and the desired DataObjects through the following yaml:
```YAML
Page:
extensions:
- SearchableDataObject
- g4b0\SearchableDataObjects\SearchableDataObject
DoNews:
extensions:
- SearchableDataObject
- g4b0\SearchableDataObjects\SearchableDataObject
```

Run a `dev/build` and then populate the search table running PopulateSearch task:
Expand All @@ -175,17 +177,17 @@ Enjoy the news into the search results :)

#### Set the number of search results per page

Setting the `CustomSearch.items_per_page` config setting you can define, how many search results per page are shown. Default is 10
Setting the `g4b0\SearchableDataObjects\CustomSearch.items_per_page` config setting you can define, how many search results per page are shown. Default is 10

By default the search result is shown at the same page, so if you're searching e.g. on the */about-us/*, the results are
shown on */about-us/SearchForm/?s=foo*. If you don't like that, you can define any Page or Controller class in the
`CustomSearch.search_controller` setting. If you set this setting to `this`, the current page will be used. Defaults to `SearchPage`
`g4b0\SearchableDataObjects\CustomSearch.search_controller` setting. If you set this setting to `this`, the current page will be used. Defaults to `SearchPage`
and falls back to the current page if no SearchPage is found.

```YAML
CustomSearch:
g4b0\SearchableDataObjects\CustomSearch:
items_per_page: 15
search_controller: SearchPage #page type to show the search
search_controller: g4b0\SearchableDataObjects\SearchPage #page type to show the search
```

### Note
Expand Down
9 changes: 5 additions & 4 deletions src/CustomSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField;
Expand Down Expand Up @@ -37,7 +38,7 @@ class CustomSearch extends Extension
* either 'this' for the current page (owner) or a page / controller, e.g. 'SearchPage'
* @var string
*/
private static $search_controller = 'SearchPage';
private static $search_controller = SearchPage::class;

private static $allowed_actions = array(
'SearchForm',
Expand Down Expand Up @@ -96,14 +97,14 @@ public function getSearchActions()
*/
public function getControllerForSearchForm()
{
$controllerName = Config::inst()->get('CustomSearch', 'search_controller');
$controllerName = Config::inst()->get(CustomSearch::class, 'search_controller');

if ($controllerName == 'this') {
return $this->owner;
}

if (class_exists($controllerName)) {
$obj = Object::create($controllerName);
$obj = Injector::inst()->create($controllerName);

if ($obj instanceof SiteTree && $page = $controllerName::get()->first()) {
return ModelAsController::controller_for($page);
Expand Down Expand Up @@ -172,7 +173,7 @@ public function getSearchResults($request, $data = [], $form = null)
}
}

$pageLength = Config::inst()->get('g4b0\SearchableDataObjects\CustomSearch', 'items_per_page');
$pageLength = Config::inst()->get(CustomSearch::class, 'items_per_page');
$ret = new PaginatedList($list, $request);
$ret->setPageLength($pageLength);

Expand Down

0 comments on commit 1305d68

Please sign in to comment.