Skip to content

Commit

Permalink
Strip preview script from backend routes
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Jun 27, 2023
1 parent b0aa6c8 commit 5b5b0eb
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/FrontendHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ public function outputFrontendTemplateHook($content, $template)
'REQUEST_TOKEN' => System::getContainer()->get('contao.csrf.token_manager')->getDefaultTokenValue(),
'pageId' => $GLOBALS['objPage']->id ?? null,
'routes' => [
'elements' => Controller::getContainer()->get('router')->generate('rocksolid_frontend_helper_elements'),
'insert' => Controller::getContainer()->get('router')->generate('rocksolid_frontend_helper_insert'),
'delete' => Controller::getContainer()->get('router')->generate('rocksolid_frontend_helper_delete'),
'render' => Controller::getContainer()->get('router')->generate('rocksolid_frontend_helper_render'),
'elements' => self::route('rocksolid_frontend_helper_elements'),
'insert' => self::route('rocksolid_frontend_helper_insert'),
'delete' => self::route('rocksolid_frontend_helper_delete'),
'render' => self::route('rocksolid_frontend_helper_render'),
],
);

Expand All @@ -339,7 +339,7 @@ public function outputFrontendTemplateHook($content, $template)
$previewEnabled = System::getContainer()->get('contao.security.token_checker')->isPreviewMode();
$data['config']['beSwitch'] = array(
'label' => $GLOBALS['TL_LANG']['MSC']['hiddenElements'] . ': ' . $GLOBALS['TL_LANG']['MSC'][$previewEnabled ? 'hiddenHide' : 'hiddenShow'],
'url' => System::getContainer()->get('router')->generate('contao_backend_switch'),
'url' => self::route('contao_backend_switch'),
'data' => array(
'FORM_SUBMIT' => 'tl_switch',
'REQUEST_TOKEN' => System::getContainer()->get('contao.csrf.token_manager')->getDefaultTokenValue(),
Expand Down Expand Up @@ -901,7 +901,7 @@ protected static function getBackendURL($do, $table, $id, $act = 'edit', array $
$params['rt'] = System::getContainer()->get('contao.csrf.token_manager')->getDefaultTokenValue();
$params['rsfhr'] = 1;

$url = System::getContainer()->get('router')->generate('contao_backend');
$url = self::route('contao_backend');

// Third parameter is required because of arg_separator.output
$url .= '?' . http_build_query($params, null, '&');
Expand Down Expand Up @@ -963,4 +963,17 @@ private function getElementTypeSettings($table, $type)

return $this->elementTypeSettings[$table][$type] ?? [];
}

private static function route(string $routeName): string
{
$container = System::getContainer();
$url = $container->get('router')->generate($routeName);
$previewScript = $container->getParameter('contao.preview_script');

if ($previewScript && substr($url, 0, \strlen($previewScript) + 1) === "$previewScript/") {
$url = substr($url, \strlen($previewScript));
}

return $url;
}
}

0 comments on commit 5b5b0eb

Please sign in to comment.