Jump to content

Add Repeater Matrix Fields to Search Selector


MateThemes
 Share

Recommended Posts

Hello everyone!

I am trying to add my repeater matrix fields to the search selector, but unfortunately nothing seems to work for me.

I have following search code in my search.php:

<?php namespace ProcessWire;

// look for a GET variable named 'q' and sanitize it
$q = input()->get('q');

// sanitize to text, which removes markup, newlines, too long, etc.
$q = sanitizer()->text($q);

// did $q have anything in it after sanitizing to text?
if($q) {

	// Make the search query appear in the top-right search box.
	// Always entity encode any user input that also gets output
	echo '<input id="search-query" value="' . sanitizer()->entities($q) . '">';

	// Sanitize for placement within a selector string. This is important for any 
	// values that you plan to bundle in a selector string like we are doing here.
	// It quotes them when necessary, and removes characters that might cause issues.
	$q = sanitizer()->selectorValue($q);

	// Search the title and body fields for our query text.
	// Limit the results to 50 pages. The has_parent!=2 excludes irrelevant admin
	// pages from the search, for when an admin user performs a search. 
	$selector = "title|body~=$q, limit=50, has_parent!=2";

	// Find pages that match the selector
	$matches = pages()->find($selector);
	
} else {
	$matches = array();
}

// unset the variable that we no longer need, since it can contain user input
unset($q);

?>
<main pw-replace='main'>
	<?php include('./includes/_pageheadersearch.php'); ?>

	<div id='content-body' class='uk-section uk-section-large uk-section-large'>
		<div class='uk-container uk-container-small'>
			<?php
			// did we find any matches?
			if(count($matches)) {
				// yes we did, render them
				echo ukAlert(sprintf(_n('Found %d page', 'Found %d pages', $matches->count), $matches->count), "default", "check");
				echo ukDescriptionListPages($matches);
			} else {
				// we didn't find any
				echo ukAlert(__('Sorry, no results were found'), "danger", "warning");
			}
			?>
		</div>
	</div>
</main>

I have tried to add my fields to the selector code (repeater_matrix.aboutsblock_repeaters.mytextfield) . But I didn't get any results.

What I am doing wrong?

Thanks for your help!

Link to comment
Share on other sites

You could check if one of the newer fieldtype selectors work for you:

https://processwire.com/blog/posts/processwire-3.0.91-core-updates/

I just quickly did a test: It seems if you omit the matrix-fieldname, it works, e.g.

If I have a matrix field called matrix, and inside it is a body field with a matrix-name "richtext":

matrix.richtext.body%=keyword // doesn't work

matrix.body%=keyword // works

$res = $pages->find("matrix.body%=fräsen");
if($res->count) {
    echo '<ul>';
    foreach($res as $match) {
        echo "<li><a href='{$match->url}'>{$match->title}</a></li>";
    }
echo '</ul>';
} else {
    echo "<p>No results found.</p>";
}

 

matrix-gotcha.PNG

  • Like 1
Link to comment
Share on other sites

I'm going to jump on this train as well. I've got a matrix with repeater inside and then a body field, just like MateThemes example above.

This works

title|headline|lead|body%=$q

Also this works

matrix.repeater_field.body%=$q

But if I combine both like so, it wont return anything

$selector .= ", title|headline|lead|body%=$q";
$selector .= ", matrix.repeater_field.body%=$q;

If I try to enclose each line in parenthesis it returns every single page on the website, even if the search word is something that doesn't exist on the site

$selector .= ", (title|headline|lead|body%=$q)";
$selector .= ", (matrix.repeaters.body%=$q)";

So I'm kinda almost there, but not quite.

Link to comment
Share on other sites

Hmm yes, I just tried it out myself and saw this msg: Exception: Multi-dot 'a.b.c' type selectors may not be used with OR '|' fields on line: 896 in /wire/core/PageFinder.php ?

Guess you'd have to do two separate queries then, one for regular fields, and one for Matrix fields (?). I guess you could file an issue @ Github, if it isn't mentioned already.

edit: Just found this related thread: 

 

Edited by dragan
added link
Link to comment
Share on other sites

On 4/25/2019 at 9:14 PM, Hurme said:

If I try to enclose each line in parenthesis it returns every single page on the website, even if the search word is something that doesn't exist on the site

Maybe you made a typo somewhere in your selector - the OR-group syntax is working for me:

2019-04-29_232222.png.022de0e098bd5488d3cece209e98b84e.png

 

Link to comment
Share on other sites

9 hours ago, Hurme said:

What happens if you have multiple pages, where some of the pages have the matrix and some don't? Does it still work for you?

There are multiple pages existing in the example above, so yes it does work.

I saw your post in the other thread - the problem is that you are using a single unnamed OR-group for all your selectors. Only one parenthesised selector in the OR-group has to match. So when you do...

$selector = "(template=home|basic-page|product-category, status!=hidden), (template=news-item|product-page)";

...this means that any unhidden page with the home, basic-page or product-category template and any page with the news-item or product-page template will match regardless of any other selectors you later add to the same OR-group. Have another look at the documentation for OR-groups and try giving names to the different OR-groups for template and field values.

  • Like 1
Link to comment
Share on other sites

2 hours ago, Hurme said:

What does naming the groups actually achieve?

It tells PW that there is more than one OR-group in the selector string. It's all about specifying what has to match in order for the whole selector string to match - remember that for every OR-group only one selector in the group has to match.

So previously you were doing this...

// Templates
$selector = "(template=home|basic-page|product-category, status!=hidden), (template=news-item|product-page)";
// Fields
$selector .= ", (title|headline|lead|body%=$q), (content_matrix.columns.body%=$q, content_matrix.columns.count>0)";

...which we can represent as this...

// Templates
$selector = "(template selector 1), (template selector 2)";
// Fields
$selector .= ", (field selector 1), (field selector 2)";

...and it is the same thing as...

// Templates
$selector = "foo=(template selector 1), foo=(template selector 2)";
// Fields
$selector .= ", foo=(field selector 1), foo=(field selector 2)";

...which is saying "find any pages that match template selector 1 OR template selector 2 OR field selector 1 OR field selector 2". So this will match a lot of pages that you don't actually want to match.

Whereas this...

// Templates
$selector = "foo=(template selector 1), foo=(template selector 2)";
// Fields
$selector .= ", bar=(field selector 1), bar=(field selector 2)";

...is saying "find any pages that match (template selector 1 OR template selector 2) AND (field selector 1 OR field selector 2)".

  • Like 2
Link to comment
Share on other sites

@Robin S It works fine now, thanks for your help. Ryan basically gave the same advice in another thread so it should be doubly fixed now. ?
 
@MateThemes It works just fine.

You'll want to write it like this:

$pages->find("matrix_field.repeater_field.regular_field%=$searchquery, matrix_field.repeater_field.count>0");

Note that you'll want to leave out matrix type. The count part at the end is to make sure it only returns pages that actually have content in them (seems to be a bug and I haven't checked if it's been fixed yet or not).

Link to comment
Share on other sites

  • 1 year later...

Sorry to bring this old thread up but felt it was better than posting a new one? Maybe not. Anyway... I'm having a similar issue when it comes to searching a textarea field in a repeaterMatrix.

$fieldsToSearch = 'name|title|text|textA|textarea|projectsCategories.title|projectsClients.title|aboutPressCategories.title|aboutPressClients.title';
$templatesToSearch = 'projectsSingle|shopSingle|typeSingle|typeCustomSingle|typeCustomSingleB|aboutPressSingle|repeater_modulesA';

$results = $pages->find("$fieldsToSearch%=$searchQuery, template=$templatesToSearch, sort=sort, check_access=0");

This is all working but it doesn't seem to include the repeaterMatrix field `modulesA` in the search as when I search for a word that is within the `repeater_modulesA` template within the field `textarea` it doesn't return the page. If it did I would then need to check the returned item's template and return the `getForPage()` to link to the page it's on.

Any thoughts on why it's not returning anything?

Link to comment
Share on other sites

I've always included a "path" to the field when including fields inside a repeaters or matrix fields. I'm not actually sure if it's really needed or if I've just been too meticulous about it.

matrix_field.regular_field|matrix_field.repeater_field.regular_field

Perhaps repeaters and matrix fields having their own template has something to do with it.

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