Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the best way to perform site search in PageBuilder? #65

Open
pmfx opened this issue Mar 8, 2018 · 8 comments
Open

What is the best way to perform site search in PageBuilder? #65

pmfx opened this issue Mar 8, 2018 · 8 comments

Comments

@pmfx
Copy link
Contributor

pmfx commented Mar 8, 2018

I was wondering what is the best way to make site search when website content heavily depends on PageBuilder?

How to configure AjaxSearch or other snippet to search in PageBuilder and show the search results?

@mnoskov
Copy link
Collaborator

mnoskov commented Mar 9, 2018

Good question

@mnoskov
Copy link
Collaborator

mnoskov commented Mar 9, 2018

I found that AjaxSearch can perform search in custom tables this way:

function pagebuilder_ajaxsearch(&$main, &$joined, $bsf, $fields) {
    global $modx;

    $joined = [
        'tb_name' => $modx->getFullTablename('pagebuilder'),
        'tb_alias' => 'pb',
        'id' => 'id',
        'main' => 'id',
        'join' => 'document_id',
        'searchable' => ['values'],
        'displayed' => ['config', 'values'],
        'concat_separator' => ', ',
        'filters' => [
            ['field' => 'visible', 'oper' => '=', 'value' => '1'],
        ],
    ];
}
[!AjaxSearch? &whereSearch=`content|pagebuilder_ajaxsearch` &ajaxSearch=`0`!]

But results filtered by built in jscropper (because data stored in json format)

@pmfx
Copy link
Contributor Author

pmfx commented Mar 11, 2018

Thanks.
That was a good clue and I got it working!
At least it looks like it is working ;)

Here is what I did:

I've copied AjaxSearch default config file:
assets/snippets/ajaxSearch/configs/default.config.php
as a
assets/snippets/ajaxSearch/configs/pagebuilder.config.php

At the end of this new config file I've added your code and a copy of AjaxSearch strip code (without JS strip):

function pagebuilder_ajaxsearch(&$main, &$joined, $bsf, $fields) {
  global $modx;

  $joined = [
    'tb_name' => $modx->getFullTablename('pagebuilder'),
    'tb_alias' => 'pb',
    'id' => 'id',
    'main' => 'id',
    'join' => 'document_id',
    'searchable' => ['values'],
    'displayed' => ['config', 'values'],
    'concat_separator' => ', ',
    'filters' => [
      ['field' => 'visible', 'oper' => '=', 'value' => '1']
    ],
  ];
}

function pagebuilderStripOutput($text) {
  global $modx;

  if ($text !== '') {
    // stripLineBreaking : replace line breaking tags with whitespace
    $text = preg_replace("'<(br[^/>]*?/|hr[^/>]*?/|/(div|h[1-6]|li|p|td))>'si", ' ', $text);

    // stripTags : Remove MODX sensitive tags
    $modRegExArray[] = '~\[\[(.*?)\]\]~s';
    $modRegExArray[] = '~\[\!(.*?)\!\]~s';
    $modRegExArray[] = '#\[\~(.*?)\~\]#s';
    $modRegExArray[] = '~\[\((.*?)\)\]~s';
    $modRegExArray[] = '~{{(.*?)}}~s';
    $modRegExArray[] = '~\[\*(.*?)\*\]~s';
    $modRegExArray[] = '~\[\+(.*?)\+\]~s';

    foreach ($modRegExArray as $mReg) $text = preg_replace($mReg, '', $text);

    // stripHtml : Remove HTML sensitive tags
    $text = strip_tags($text);
  }
  return $text;
}

The last thing was a snippet call:

[!AjaxSearch? 
&config=`pagebuilder` 
&ajaxSearch=`0` 
&whereSearch=`content|tv|pagebuilder_ajaxsearch` 
&stripOutput=`pagebuilderStripOutput`
!]

@pmfx
Copy link
Contributor Author

pmfx commented Mar 11, 2018

To show extract from PB in AS results you can use this in snippet parameter:

&extract=`1:content,pb_values`

But this may result in showing code like {"richtext":"Lorem ipsum....."}

As a temporary workaround I added this at the end of a "pagebuilderStripOutput" function:

// strip pagebuilder wrappers
$text = str_replace('{"richtext":"', '', $text);
$text = str_replace('"}', '', $text);

But it is not perfect in case other field names are used. Probably can be solved with preg_replace/regex but I'm not that familiar with it.

@pmfx
Copy link
Contributor Author

pmfx commented Mar 20, 2018

While the configuring AjaxSearch is pretty difficult, maybe easiest solution would be to make a copy of PageBuilder values (without JS code) inside "content" field of "site_content" table on every document save event?

With this, there would be no issues with searching or configuring AjaxSearch.

@pmfx
Copy link
Contributor Author

pmfx commented Mar 20, 2018

... and it could be added as a new plugin setting (turned off by default).

@mnoskov
Copy link
Collaborator

mnoskov commented Mar 21, 2018

I don't like this solution, we can just end AjaxSearch config to crop all mustaches with regex

@fourroses666
Copy link

Cool, have it working. My mistake was that I didn't add it to the result (landing) page snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants