Jump to content

Change Default User Filter


joer80
 Share

Recommended Posts

There might be a better approach, but this snippet in site/ready.php works for me:

<?php namespace ProcessWire;

$wire->addHookBefore('ProcessPageLister::execute', function(HookEvent $event) {
	
	if($event->page->process != "ProcessUser")
		return;

	# Put all fields that should be searchable by default here:
	$userSearchFields = ['email'];
	# We create a comma separated field selector string from the array above for use by ProcessPageLister
	$searchFieldsString = implode('', array_map(function($f) { return ", $f="; }, $userSearchFields));

	$lister = $event->object;
	# ProcessUser appends the ", roles=" selector before our hook happens, so we inject our fields
	# before that part:
	$lister->defaultSelector = preg_replace(
		'/(?=, roles=)/',
		$searchFieldsString,
		$lister->defaultSelector
	);
	
});

ProcessPageLister builds its field list from the selector style string its $defaultSelector property. So you just need to put the names of your fields into the $userSearchFields array and ProcessPageLister will take care of the rest.

If you want to completely replace the search field list, it's even simpler.

<?php namespace ProcessWire;

	if($event->page->process != "ProcessUser")
		return;

	# Put all fields that should be searchable by default here:
	$userSearchFields = ['lastname', 'firstname'];
	# We create a comma separated field selector string from the array above for use by ProcessPageLister
	$searchFieldsString = implode(', ', array_map(function($f) { return "$f="; }, $userSearchFields));

	$lister = $event->object;

	$lister->defaultSelector = $searchFieldsString;
}

 

  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...
On 3/2/2021 at 12:45 PM, BitPoet said:

There might be a better approach, but this snippet in site/ready.php works for me:


<?php namespace ProcessWire;

$wire->addHookBefore('ProcessPageLister::execute', function(HookEvent $event) {
	
	if($event->page->process != "ProcessUser")
		return;

	# Put all fields that should be searchable by default here:
	$userSearchFields = ['email'];
	# We create a comma separated field selector string from the array above for use by ProcessPageLister
	$searchFieldsString = implode('', array_map(function($f) { return ", $f="; }, $userSearchFields));

	$lister = $event->object;
	# ProcessUser appends the ", roles=" selector before our hook happens, so we inject our fields
	# before that part:
	$lister->defaultSelector = preg_replace(
		'/(?=, roles=)/',
		$searchFieldsString,
		$lister->defaultSelector
	);
	
});

 

This got me where I needed to go!  This is what I did:

$wire->addHookBefore('ProcessPageLister::execute', function(HookEvent $event) {
    
    if($event->page->process != "ProcessUser")
		return;

	$lister = $event->object;
    
    $lister->defaultSelector = 'name%=, firstname%=, lastname%=, email%=';
    
});

 

  • Like 1
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

  • Recently Browsing   0 members

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