Jump to content

Filter page lister where field exists?


cst989
 Share

Recommended Posts

I'd like a bookmark in the admin to a page list of pages that have a "meta title" field, so that you can quickly see which pages need updating. 

Without just limiting all the templates that have the field, I want something a bit more future-proof.

Is this possible? If so how?

I don't want to check if its empty or not equal to anything, as it would also serve to review what is there.

I have ListerPro as well but can't see how to do it with either.

Link to comment
Share on other sites

Well, there's no selector "if template / page has field_x", so a workaround could be two custom selectors:

custom (field=value) 1 = meta%=
custom (field=value) 2 = meta!=

Make sure to check the checkbox on the right (make it OR rather than AND)

  • Thanks 1
Link to comment
Share on other sites

Hi @dragan thanks for the response, I think I'm misunderstanding or doing it wrong, empty fields are now being excluded. To clarify my field is meta_title - this is what I've done

As you can see it matches 14 pages but there should be 16, also the number doesn't change whether using OR or AND

image.png.0e50261dc9869d25db47244e66f01088.png

Link to comment
Share on other sites

grumble... sorry, but my first example doesn't indeed work.

It seems that this (quite surprisingly) does seem to work:

page.fieldname=

At least in my test-environment, it lists all pages (my field only exists in only one template)

Link to comment
Share on other sites

Yeah, well in that case it really seems impossible. Can't you narrow it down with some other condition? has_parent, template, or similar? I know you said you want something future-proof, but certainly you are not adding new templates every day?

Field dependencies let you write your own logic in plain PHP (one of many options), I guess Ryan has a reason he didn't allow such freedom when it comes to Lister (Pro or not) - probably security concerns.

You might want to take a look at @bernhard's RockGrid module instead. A bit more work initially than just creating a new Lister Pro instance, but you're totally free to list / select / query whatever you like.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Yeah I absolutely can narrow it down in other ways, its just that if this worked I would have something I could use on any site without modifying the templates checked each time, or having this extra step to check when finishing a site. I build quite a lot of ProcessWire sites so I'm making templates more often than most!

I really appreciate the input though, I will make do with what I have for now. Maybe @ryan could weigh in on this one day ?

Link to comment
Share on other sites

@cst989, here is one way you could do it.

First create a new Lister bookmark with the columns and sorting set the way you want. It doesn't matter what you set in the filters because we are going to override that in a hook.

Take note of the bookmark ID in the URL when you view the bookmark in Lister.

Now add the following hook to /site/ready.php, updating the bookmark ID to suit:

$wire->addHookBefore('ProcessPageLister::execute', function(HookEvent $event) {
	/* @var ProcessPageLister $lister */
	$lister = $event->object;
	$bookmark = $event->wire('input')->get('bookmark');
	// If executing your specific bookmark
	if($bookmark === '_1574293851') {
		// Find the templates that contain the meta_title field
		$tpls = new TemplatesArray();
		foreach($event->wire('templates') as $template) {
			if($template->flags) continue; // Skip system templates (e.g. repeater templates)
			if($template->fieldgroup->has('meta_title')) $tpls->add($template);
		}
		// Set the defaultSelector for the Lister
		$lister->defaultSelector = "template=$tpls, meta_title=''";
	}
});

 

  • Like 1
Link to comment
Share on other sites

9 hours ago, cst989 said:

Would this work with ListerPro as well?

Yes. If you want the hook to apply to a specific Lister Pro instance instead of a bookmark you could do this:

$wire->addHookBefore('ProcessPageLister::execute', function(HookEvent $event) {
	/* @var ProcessPageLister $lister */
	$lister = $event->object;
	// If executing a specific Lister Pro instance
	if($event->wire('page')->name === 'your-lister-page-name') {
		// Find the templates that contain the meta_title field
		$tpls = new TemplatesArray();
		foreach($event->wire('templates') as $template) {
			if($template->flags) continue; // Skip system templates (e.g. repeater templates)
			if($template->fieldgroup->has('meta_title')) $tpls->add($template);
		}
		// Set the defaultSelector for the Lister
		$lister->defaultSelector = "template=$tpls, meta_title=''";
	}
});

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

Thank you, I finally got around to doing this. I was able to combine in

|| $template->fieldgroup->has('meta_description')

 as well, and left out the

, meta_title=''

 so you can see them all, empty or otherwise. Only minor oddity remaining is that all the filters generated by this script load in an open position, and you get an odd js glitch when you try to close them the first time - but beggars can't be choosers!

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...