From 33be4606ccf1561c1ab70a1581682b9925aea736 Mon Sep 17 00:00:00 2001 From: Jim Schmid Date: Tue, 24 Jan 2017 22:42:01 +0100 Subject: [PATCH] Support custom backend routes (see #512). --- composer.json | 2 +- src/Resources/config/services.yml | 9 ++ src/Resources/contao/classes/BackendUser.php | 9 +- .../contao/controllers/BackendCustom.php | 64 ++++++++++ .../contao/templates/backend/be_main.html5 | 2 +- src/Resources/public/core.js | 12 +- src/Resources/public/core.min.js | 4 +- src/Resources/public/mootao.js | 2 +- src/Resources/views/Backend/be_page.html.twig | 5 + .../Extension/ContaoTemplateExtension.php | 83 +++++++++++++ .../Extension/ContaoTemplateExtensionTest.php | 115 ++++++++++++++++++ 11 files changed, 295 insertions(+), 12 deletions(-) create mode 100644 src/Resources/contao/controllers/BackendCustom.php create mode 100644 src/Resources/views/Backend/be_page.html.twig create mode 100644 src/Twig/Extension/ContaoTemplateExtension.php create mode 100644 tests/Twig/Extension/ContaoTemplateExtensionTest.php diff --git a/composer.json b/composer.json index a573daafa4..e6ad21b97f 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "symfony/yaml": "^3.2", "sensio/framework-extra-bundle": "^3.0.2", "psr/log": "^1.0", - "twig/twig": "^1.20", + "twig/twig": "^1.26", "doctrine/dbal": "^2.5", "doctrine/doctrine-bundle": "^1.6", "doctrine/doctrine-cache-bundle": "^1.3", diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 6647c026af..53dc64dcb5 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -219,3 +219,12 @@ services: - "_contao_fe_attributes" calls: - ["setName", ["contao_frontend"]] + + contao.twig.template_extension: + class: Contao\CoreBundle\Twig\Extension\ContaoTemplateExtension + public: false + arguments: + - "@request_stack" + - "@contao.framework" + tags: + - { name: twig.extension } diff --git a/src/Resources/contao/classes/BackendUser.php b/src/Resources/contao/classes/BackendUser.php index 7ff5cae167..380b2d17b7 100644 --- a/src/Resources/contao/classes/BackendUser.php +++ b/src/Resources/contao/classes/BackendUser.php @@ -13,6 +13,7 @@ use Contao\CoreBundle\Exception\RedirectResponseException; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Routing\RouterInterface; /** @@ -459,6 +460,9 @@ public function navigation($blnShowAll=false) /** @var AttributeBagInterface $objSessionBag */ $objSessionBag = \System::getContainer()->get('session')->getBag('contao_backend'); + /** @var RouterInterface $router */ + $router = \System::getContainer()->get('router'); + $arrModules = array(); $session = $objSessionBag->all(); @@ -477,7 +481,8 @@ public function navigation($blnShowAll=false) $arrModules[$strGroupName]['class'] = ' node-expanded'; $arrModules[$strGroupName]['title'] = \StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['collapseNode']); $arrModules[$strGroupName]['label'] = (($label = is_array($GLOBALS['TL_LANG']['MOD'][$strGroupName]) ? $GLOBALS['TL_LANG']['MOD'][$strGroupName][0] : $GLOBALS['TL_LANG']['MOD'][$strGroupName]) != false) ? $label : $strGroupName; - $arrModules[$strGroupName]['href'] = \Controller::addToUrl('mtg=' . $strGroupName); + $arrModules[$strGroupName]['href'] = $router->generate('contao_backend', array('do'=>\Input::get('do'), 'mtg'=>$strGroupName, 'ref'=>TL_REFERER_ID)); + $arrModules[$strGroupName]['ajaxUrl'] = $router->generate('contao_backend'); // Do not show the modules if the group is closed if (!$blnShowAll && isset($session['backend_modules'][$strGroupName]) && $session['backend_modules'][$strGroupName] < 1) @@ -497,7 +502,7 @@ public function navigation($blnShowAll=false) $arrModules[$strGroupName]['modules'][$strModuleName]['title'] = \StringUtil::specialchars($GLOBALS['TL_LANG']['MOD'][$strModuleName][1]); $arrModules[$strGroupName]['modules'][$strModuleName]['label'] = (($label = is_array($GLOBALS['TL_LANG']['MOD'][$strModuleName]) ? $GLOBALS['TL_LANG']['MOD'][$strModuleName][0] : $GLOBALS['TL_LANG']['MOD'][$strModuleName]) != false) ? $label : $strModuleName; $arrModules[$strGroupName]['modules'][$strModuleName]['class'] = 'navigation ' . $strModuleName; - $arrModules[$strGroupName]['modules'][$strModuleName]['href'] = TL_SCRIPT . '?do=' . $strModuleName . '&ref=' . TL_REFERER_ID; + $arrModules[$strGroupName]['modules'][$strModuleName]['href'] = $router->generate('contao_backend', array('do'=>$strModuleName, 'ref'=>TL_REFERER_ID)); // Mark the active module and its group if (\Input::get('do') == $strModuleName) diff --git a/src/Resources/contao/controllers/BackendCustom.php b/src/Resources/contao/controllers/BackendCustom.php new file mode 100644 index 0000000000..6c9eebb834 --- /dev/null +++ b/src/Resources/contao/controllers/BackendCustom.php @@ -0,0 +1,64 @@ + + */ +class BackendCustom extends BackendMain +{ + + /** + * Constructor. + */ + public function __construct() + { + parent::__construct(); + + // Initialize the template in the constructor so it is available in the getTemplateObject() method + $this->Template = new \BackendTemplate('be_main'); + } + + + /** + * Return the template object + * + * @return BackendTemplate|object + */ + public function getTemplateObject() + { + return $this->Template; + } + + + /** + * Run the controller and parse the template + * + * @return Response + */ + public function run() + { + $packages = $this->getContainer()->getParameter('kernel.packages'); + + $this->Template->version = $packages['contao/core-bundle']; + + // Ajax request + if ($_POST && \Environment::get('isAjaxRequest')) + { + $this->objAjax = new \Ajax(\Input::post('action')); + $this->objAjax->executePreActions(); + } + + return $this->output(); + } +} diff --git a/src/Resources/contao/templates/backend/be_main.html5 b/src/Resources/contao/templates/backend/be_main.html5 index 96e2d69e5c..e34f3dec3c 100644 --- a/src/Resources/contao/templates/backend/be_main.html5 +++ b/src/Resources/contao/templates/backend/be_main.html5 @@ -57,7 +57,7 @@