Nicolas Pastorino recently released the eZKeyword Autocomplete extension for eZ Publish 4 and I decided to give it a whirl.
Installation of the extension is very straightforward:
It doesn’t get much easier than that, but unfortunately it didn’t work. A quick review of the module.php showed that /ajaxbackend/autocomplete_keywords was being queried for keyword results. On loading up this url I encountered a PHP Fatal Error.
[Mon Jul 07 23:51:17 2008] [error] [client 202.0.51.250] PHP Fatal error: Call to undefined function json_encode() in /home/andrewdu/public_html/extension/ezkeyword_autocomplete/modules/ajaxbackend/autocomplete_ezkeywords.php on line 66
It appears that the script was unable to find the json_encode function. Doh! json_encode was only added to PHP 5.2, which leaves a few of us still on PHP 5.1 with an extension that doesn’t work. Of course we can soon rectify this issue by providing a fallback function incase the json_encode function is not available on your system. I patched ezkeyword_autocomplete/modules/ajaxbackend/autocomplete_ezkeywords.php with the following function.
if (!function_exists('json_encode'))
{
function json_encode($a=false)
{
if (is_null($a)) return 'null';
if ($a === false) return 'false';
if ($a === true) return 'true';
if (is_scalar($a))
{
if (is_float($a))
{
// Always use "." for floats.
return floatval(str_replace(",", ".", strval($a)));
}
if (is_string($a))
{
static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
}
else
return $a;
}
$isList = true;
for ($i = 0, reset($a); $i < count($a); $i++, next($a))
{
if (key($a) !== $i)
{
$isList = false;
break;
}
}
$result = array();
if ($isList)
{
foreach ($a as $v) $result[] = json_encode($v);
return '[' . join(',', $result) . ']';
}
else
{
foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
return '{' . join(',', $result) . '}';
}
}
}
The above takes care of our json_encode issue and the script should now work. Autocompletion of the keywords ensures that you pick appropriate existing keywords each time rather than cluttering up your keywords with many similar keywords.
In summary the extension works well and thanks should go out to Nicolas for such a great contribution.