Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate search index for saved pages in ProcessWire
<?php
/**
* Generate search index for saved pages
*
*/
$this->addHook('Pages::saveReady', function(HookEvent $event) {
$page = $event->arguments[0];
if ($page->id && $page->hasField('search_index')) {
$page->search_index = indexPage($page, array(
'title',
'headline',
'summary',
'body',
'footer',
'publish_from',
'meta_keywords',
'meta_description',
));
}
});
/**
* Store page data in search index and return generated index as a string
*
* @param Page $page
* @param array $index_fields
* @return string
*/
function indexPage(Page $page, $index_fields = array()) {
$search_index = "";
if ($page->id && !empty($index_fields)) {
foreach ($page->fields as $field) {
if (in_array($field->name, $index_fields)) {
$formatted_data = $page->getFormatted($field->name);
$search_index .= strip_tags($formatted_data);
$search_index .= getIndexUrls($formatted_data);
} else if ($field->type == "FieldtypePageTable") {
foreach ($page->get($field->name) as $child) {
$search_index .= indexPage($child, $index_fields);
}
}
}
}
return $search_index;
}
/**
* Grab URLs from field data, mash them together into a space separated string,
* and return the resulting string
*
* @param string $data
* @return string
*/
function getIndexUrls($data) {
$out = "";
if ($data && preg_match_all('/href=([\"\'])(.*?)\1/i', $data, $matches)) {
$out = " link:" . implode(" link:", $matches[2]) . " ";
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.