Skip to content

Commit

Permalink
Use AssetCollector to add underlyingQuery script tag
Browse files Browse the repository at this point in the history
  • Loading branch information
mh-goe committed Jul 8, 2024
1 parent 20fc67b commit 1e7fd7e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
25 changes: 14 additions & 11 deletions Classes/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Subugoe\Find\Utility\ArrayUtility;
use Subugoe\Find\Utility\FrontendUtility;
use TYPO3\CMS\Core\Log\LogManagerInterface;
use TYPO3\CMS\Core\Page\AssetCollector;
use TYPO3\CMS\Core\Utility\ArrayUtility as CoreArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Http\ForwardResponse;
Expand Down Expand Up @@ -62,15 +63,17 @@ public function detailAction(string $id): ResponseInterface
{
$arguments = $this->searchProvider->getRequestArguments();
$detail = $this->searchProvider->getDocumentById($id);
$response = $this->responseFactory->createResponse();
if ($this->request->hasArgument('underlyingQuery')) {
$underlyingQueryInfo = $this->request->getArgument('underlyingQuery');
$response->withAddedHeader('detail', FrontendUtility::addQueryInformationAsJavaScript(
$underlyingQueryScriptTagContent = FrontendUtility::addQueryInformationAsJavaScript(
$underlyingQueryInfo['q'],
$this->settings,
(int) $underlyingQueryInfo['position'],
$arguments
));
);

GeneralUtility::makeInstance(AssetCollector::class)
->addInlineJavaScript('underlyingQueryVar', $underlyingQueryScriptTagContent, ['type' => 'text/javascript'], ['priority' => true]);
}

$this->addStandardAssignments();
Expand All @@ -94,17 +97,17 @@ public function indexAction(): ResponseInterface
}

$this->searchProvider->setCounter();
$response = $this->responseFactory->createResponse();

$response->withAddedHeader('index',
FrontendUtility::addQueryInformationAsJavaScript(
$this->searchProvider->getRequestArguments()['q'],
$this->settings,
null,
$this->searchProvider->getRequestArguments()
)
$underlyingQueryScriptTagContent = FrontendUtility::addQueryInformationAsJavaScript(
$this->searchProvider->getRequestArguments()['q'] ?? [],
$this->settings,
null,
$this->searchProvider->getRequestArguments()
);

GeneralUtility::makeInstance(AssetCollector::class)
->addInlineJavaScript('underlyingQueryVar', $underlyingQueryScriptTagContent, ['type' => 'text/javascript'], ['priority' => true]);

$this->addStandardAssignments();
$defaultQuery = $this->searchProvider->getDefaultQuery();

Expand Down
23 changes: 10 additions & 13 deletions Classes/Utility/FrontendUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;

/**
* Utility for JavaScripts, Views, ...
*/
Expand All @@ -38,16 +35,14 @@ class FrontendUtility
/**
* Stores information about the active query in the »underlyingQuery« JavaScript variable.
*
* @param array $query
* @param int|null $position of the record in the result list
* @param array $arguments overrides $this->requestArguments if set
*
* @throws \JsonException
*/
public static function addQueryInformationAsJavaScript($query, array $settings, ?int $position = null, $arguments = []): string
public static function addQueryInformationAsJavaScript(array $query, array $settings, ?int $position = null, array $arguments = []): string
{
if ($settings['paging']['detailPagePaging']) {
$scriptTag = GeneralUtility::makeInstance(TagBuilder::class, 'script');
$scriptTag->addAttribute('type', 'text/javascript');

if (array_key_exists('underlyingQuery', $arguments)) {
$arguments = $arguments['underlyingQuery'];
}
Expand All @@ -61,17 +56,19 @@ public static function addQueryInformationAsJavaScript($query, array $settings,
$underlyingQuery['position'] = $position;
}

if ($arguments['count']) {
if (array_key_exists('count', $arguments)) {
$underlyingQuery['count'] = $arguments['count'];
} elseif (array_key_exists('count', $settings)) {
$underlyingQuery['count'] = $settings['count'];
}

if ($arguments['sort']) {
if (array_key_exists('sort', $arguments)) {
$underlyingQuery['sort'] = $arguments['sort'];
} elseif (array_key_exists('sort', $settings)) {
$underlyingQuery['sort'] = $settings['sort'];
}

$scriptTag->setContent('var underlyingQuery = '.json_encode($underlyingQuery).';');

return $scriptTag->render();
return 'var underlyingQuery = '.json_encode($underlyingQuery, JSON_THROW_ON_ERROR).';';
}

return '';
Expand Down

0 comments on commit 1e7fd7e

Please sign in to comment.