1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trying to add a Google CSE custom search to the forum

Discussion in 'XenForo Development Discussions' started by Stuart Wright, Jan 2, 2014.

  1. Stuart Wright

    Stuart Wright Well-Known Member

    Hi folks,
    I'm using the two page solution for Google CSE.
    A query parameter, q, is passed to the results page.

    I found a code snippet for a callback on a page which Jake posted for user id:

    class Callback_PageNode
    public static function 
    getUser(XenForo_ControllerPublic_Abstract $controllerXenForo_ControllerResponse_Abstract &$response)
    $userId $controller->getInput()->filterSingle('u'XenForo_Input::UINT);

    $userModel XenForo_Model::create('XenForo_Model_User');
    $user $userModel->getFullUserById($userId);
    $user $userModel->prepareUser($user);

    $response->params['user'] = $user;
    Obviously q is going to be a string, though.
    I'm assuming I have to change XenForo_Input::UINT to a string.
    and then just return $response->params['q'] = $q;
    Which XF function is it I would have to use instead of
    $q = $controller->getInput()->filterSingle('q', XenForo_Input::UINT);
  2. Stuart Wright

    Stuart Wright Well-Known Member

    Ok so I dug around and I should have guessed it's as simple as XenForo_Input::STRING.
    So I created the controller php script and uploaded it and saved the page with the controller stuff in there ok.
    But the template for the page needs to have the variable $q available for the Google CSE search results.
    The search results page template looks like this.
    <div class="baseHtml ugc">
    <xen:set var="$q">{xen:raw $q}</xen:set>
      (function() {
        var cx = 'partner-pub-2615987788629632:lme0jj5mawk';
        var gcse = document.createElement('script');
        gcse.type = 'text/javascript';
        gcse.async = true;
        gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
            '//www.google.com/cse/cse.js?cx=' + cx;
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(gcse, s);
    No search results are displayed, I'm guessing because the value of q is not available to the script.

Share This Page