Jump to content

Issue with hooking ProcessPageSearch::executeFor and the new Search Feature in PW > 3.0.108


gingebaker
 Share

Recommended Posts

Hello

I am having an issue with hooking the SearchBox in the Admin Interface in PW >= 3.0.108 where the new Search Features are added.

I hooked:

$this->addHookAfter('ProcessPageSearch::executeFor',$this,'executeFor');

to change the title format of specific pages in the SearchBox of the Admin. This looked something like this:

public function executeFor($event) {
        $return = json_decode($event->return);
        if($this->input->get->admin_search) {
            $return = json_decode($event->return);
            if(isset($return->matches)) {
                $didChange = false;
                foreach($return->matches as $key => $match) {
                    if($match->template == "templateX" || $match->template == "templateY") {
                        // add vorname and nachname to title
                        $matchPage = $this->pages->get($match->id);
                        $return->matches[$key]->title = $match->title.' - '.$matchPage->get('nachname').' '.$matchPage->get('vorname');
                        $didChange = true;
                    }
                }
                if($didChange) {
                    $event->return = json_encode($return);
                }
            }
        }
    }

The reason I am doing this is that on some special templates I don´t want to show only the title field of this template in the Search Autocomplete but show some other fields to get a better usability.

Now with the new ProcessPageSearchLive.php Module it seems that this is not possible anymore?
There are the two hookable functions ___renderList and ___renderItem,  but the are only called by viewAll Output.

So the code where I should hook into is somewhere in the findPages() function of ProcessPageSearchLive.php, but I could not find a way to Hook into and modify it?

https://github.com/processwire/processwire/blob/dev/wire/modules/Process/ProcessPageSearch/ProcessPageSearchLive.php#L736

Is there a way to get this done as it was with the old search? Perhaps i am overseeing something?

Thanks for any help!

  • Like 1
Link to comment
Share on other sites

Yes, it seems we have sadly lost the ability to manipulate the search results like that. I've used that approach a few times myself.

Maybe you could make a GitHub request that Ryan make ProcessPageSearchLive::find() hookable? It looks like manipulating the $items array that the method returns could be a good solution.

On 8/14/2018 at 1:17 AM, gingebaker said:

The reason I am doing this is that on some special templates I don´t want to show only the title field of this template in the Search Autocomplete but show some other fields to get a better usability.

And maybe add some thumbs-up support to this existing request too: https://github.com/processwire/processwire-requests/issues/214 ?

  • Like 2
Link to comment
Share on other sites

Hello gingebaker,

in that situation, until the issue will be resolved, you can copy core module to site/modules and there change it to get working temporarily solution.

If you want to do that, here are steps:

1) copy core ProcessPageSearch module from wire/modules/Process/ProcessPageSearch to site/modules/
2
) after it configure PW to use that variant (administration, Modules->Configure->PageSearch...).

3) inside ProcessPageSearchLive.php, execute() do small changes:

public function execute($getJSON = true) {

		/** @var WireInput $input */
		$input = $this->wire('input');

		$liveSearch = $this->init();

		if((int) $input->get('version') > 1) {
			// version 2+ keep results in native format, for future use
			$items = $this->find($liveSearch);
		} else {
			// version 1 is currently used by PW admin themes
			$items = $this->convertItemsFormat($this->find($liveSearch));
		}
		
		$result = array(
			'matches' => &$items
		);

       // --- START
       if(count($result['matches'])){
		 foreach($result['matches'] as $key => $match) {			
			if($match['template_label'] == "templateX" || $match['template_label'] == "templateY") {
				// add vorname and nachname to title
				$matchPage = $this->pages->get($match['id']);
				$result['matches'][$key]['title'] = $match['title'] .' - '.$matchPage->get('nachname').' '.$matchPage->get('vorname');                        
			}
          }
        }
        //--- END
		return $getJSON ? json_encode($result) : $items;
	}

Regards.

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...