Jump to content

404 search module


cmscritic
 Share

Recommended Posts

Just committed an update to this which means that it now works out of the box without needing to edit the search.php template file.

You can still choose to add the output of the 404 page's body field, but this is not a requirement, and the functionality for loading search results on the 404 page is now completely automatic.

It should be backwards compatible with your site, if you had it previously installed and were using the old required:

if(isset($options['q'])) $input->get->q = $options['q'];

line in your search.php, but please let me know if you have any problems.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

@adrian @Wanze How can I make this work with TemplateEngineFactory and Smarty?

Your search function tries to render a normal template file but it does not output my search page (which I selected in module settings).

So I tried to modify the search function to use a partial that would be rendered, but it didn´t work either:

public function search($event) {

        if($this->searchPage) {
            $request = parse_url($_SERVER['REQUEST_URI']);
            $path = $request["path"];
            $result = trim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');
            $term = str_replace(array('-','_','/','%20'),' ',$result);

            // New way of setting the value of get->q directly.
            // This way, this module can work out of the box with no need to modify the search.php template file
            $this->input->get->q = $term;

            // Setting the value for $options['q'] in the render is for backwards compatibility
            // and as a way to identify that this module is rendering the search template so the
            // dev can choose to output conditional content such as if(isset($options['q'])) $content .= $pages->get(27)->body;
            $templateFilename = $this->pages->get($this->searchPage)->template->filename;
            $factory = wire("modules")->get('TemplateEngineFactory');
            $partial = $factory->load('search.tpl');
            $event->return = $partial->render();

        }

Any suggestions how I can get this to work?

EDIT: It seems that the Smarty File is beeing loaded but I get much undefinded index errors. The errors appear because it seems that my _init.php isn´t prepended.

Link to comment
Share on other sites

Hi both,

I don't know the module myself and I'm not sure why your prepended file is not respected when rendering via a filename. Can you try if this works? This should trigger the Page::render() of the search page and also output the markup from your corresponding smarty template.

$event->return = $this->pages->get($this->searchPage)->render();

 

  • Like 1
Link to comment
Share on other sites

21 minutes ago, Wanze said:

Hi both,

I don't know the module myself and I'm not sure why your prepended file is not respected when rendering via a filename. Can you try if this works? This should trigger the Page::render() of the search page and also output the markup from your corresponding smarty template.


$event->return = $this->pages->get($this->searchPage)->render();

 

Thanks Wanze - very good point!

When I wrote that module I initially had things set up quite differently such that it required rendering the template file directly. More recent changes mean that it no longer needs to do that. Rendering the page directly (like you noted) works fine here, so I will make that change to the module anyway and hopefully it will solve the problem with TEF.

PS New version with this change just committed.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...