Skip to content

Instantly share code, notes, and snippets.

@calien666
Created February 22, 2022 11:39
Show Gist options
  • Save calien666/e6d07510cfbb452e288bc2f0b899aed0 to your computer and use it in GitHub Desktop.
Save calien666/e6d07510cfbb452e288bc2f0b899aed0 to your computer and use it in GitHub Desktop.
Adding tx_solr[q] as speaking URL part to TYPO3
Search:
type: Plugin
namespace: tx_solr
routePath: '/{q}'
limitToPages: [1571]
defaults:
q: ''
requirements:
q: '^.*$'
const triggerNewUrl = function (form) {
var searchField = form.querySelector('.input--search');
var currentSearchWord = searchField.value;
searchField.removeAttribute('name');
var action = form.action.split('?')[0];
form.addEventListener('keyup', function (event) {
if (event.target === searchField) {
var actionParams = action.split('/');
var lastParam = actionParams.pop();
var searchParams = actionParams.pop();
if (searchParams !== lastParam) {
actionParams.push(searchParams);
}
if (currentSearchWord.length > 0) {
var encodedSearchWord = encodeURI(currentSearchWord);
actionParams.forEach(function (elem, index) {
if (elem === encodedSearchWord) {
actionParams[index] = encodeURI(searchField.value);
}
});
} else {
actionParams.push(encodeURI(searchField.value));
}
actionParams.push(lastParam);
action = actionParams.join('/');
currentSearchWord = searchField.value;
form.action = action;
}
});
form.addEventListener('submit', function (event) {
event.preventDefault();
event.stopPropagation();
if (searchField.value !== currentSearchWord) {
if (currentSearchWord) {
action = action.replace(encodeURI(currentSearchWord), encodeURI(searchField.value));
} else {
action += searchField.value + "/";
}
form.action = action;
}
window.location.href = form.action;
return false;
});
};
const triggerHeaderSearchUrl = function () {
var form = document.querySelector('.navigation__bar .tx_solr form');
triggerNewUrl(form);
};
$(document).ready(function () {
var mainForm = document.querySelector('.wrap--content-element .tx_solr form');
if (mainForm) {
triggerNewUrl(mainForm);
}
var searchForm = document.querySelector('.tx-euwid-search form');
if (searchForm) {
triggerNewUrl(searchForm);
}
triggerHeaderSearchUrl();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment