Jump to content

Adding text titles from Page Reference fields to a Cache field?


johnstephens
 Share

Recommended Posts

Hello, ProcessWire friends!

I have a template that includes a Cache field to combine other fields for full-text search. I wonder if it's possible to add the titles from a Page Reference field on the same template. If so, how would one do that?

To clarify, suppose my template includes fields like this:

  • Title: "#DungeonWorldDay 2020: Saturday, 22 August"
  • Body: "The Dungeon World Discord server is hosting Dungeon World Day on Saturday, 22 August. GMs will be running pick-up games, intelligentsia will be offering how-to sessions and Q&A, and fledgling players are welcome to storm dungeons galore alongside hardened veterans. ¶ Please spread the word, invite your friends, invite your enemies if they are fun to play with, and invite your ambivalent associates."
  • Tags (Page Reference)
    • "Role-playing"
    • "Events for Hipsters"
  • Cache

Is there a way to configure the Cache field to include "Role-playing" and "Events for Hipsters" (referenced page titles) in addition to the Title and Body?

Thanks in advance!

Link to comment
Share on other sites

I think you're better off not using FieldtypeCache. See this issue for some discussion: https://github.com/ryancramerdesign/ProcessWire/issues/1577
And I remember Ryan commenting somewhere that he's thinking of removing it from the core.

The simplest thing if you want to merge multiple fields into a single field for search purposes is to use the SearchEngine module. Or if you want to go more hands-on it's not difficult to populate a hidden "index" textarea field from other field values using a Pages::saveReady hook.

$pages->addHookAfter('saveReady', function(HookEvent $event) {
	$page = $event->arguments(0);
	if($page->template == 'your_template') {
		$index = $page->title;
		if($page->body) $index .= ' ' . strip_tags($page->body);
		foreach($page->tags as $tag) $index .= ' ' . $tag->title;
		$page->index = $index;
	}
});

 

Edited by Robin S
Added hook code
  • Like 4
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...