Skip to content

Commit

Permalink
Default vs. custom simplesearch.md (#186)
Browse files Browse the repository at this point in the history
Made code modifications to create a custom page without having to include a simplesearch section in the header. If the custom page contains a template variable in the header, it takes precedence over the setting in the config file. When using the default page, the opposite behavior occurs, where the settings in the config file are preferred, which overrides the settings in the page header, assuming that the default page now contains the template variable directly from the plugin authors.

This fixes some of the issues described in #173 and #43 (maybe others too).

I will send other separate improvements in the next Pull request.
  • Loading branch information
petira authored May 13, 2020
1 parent 70e4cf2 commit 2c939b9
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions simplesearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function onPagesInitialized()
}

// If a page exists merge the configs
if ($page) {
if (isset($page)) {
$this->config->set('plugins.simplesearch', $this->mergeConfig($page));
}

Expand All @@ -110,6 +110,12 @@ public function onPagesInitialized()
return;
}

// set the template is not set in the page header (the page header setting takes precedence over the plugin config setting)
if (!isset($page->header()->template)) {
$template_override = $this->config->get('plugins.simplesearch.template', 'simplesearch_results');
$page->template($template_override);
}

// Explode query into multiple strings. Drop empty values
$this->query = array_filter(array_filter(explode(',', $query), 'trim'), 'strlen');

Expand Down Expand Up @@ -225,15 +231,17 @@ public function onPagesInitialized()
);
}

// if page doesn't have settings set, create a page
if (!isset($page->header()->simplesearch)) {
// Display simplesearch page if no page was found for the current route
$pages = $this->grav['pages'];
$page = $pages->dispatch($this->config->get('plugins.simplesearch.route', '/search'), true);
if (!isset($page)) {
// create the search page
$page = new Page;
$page->init(new \SplFileInfo(__DIR__ . '/pages/simplesearch.md'));

// override the template is set in the config
// override the template is set in the plugin config (the plugin config setting takes precedence over the page header setting)
$template_override = $this->config->get('plugins.simplesearch.template');
if ($template_override) {
if (isset($template_override)) {
$page->template($template_override);
}

Expand Down

0 comments on commit 2c939b9

Please sign in to comment.