From e1ef27e0b48af71c7e674b16f07502a0bb5fca9e Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Fri, 15 Jul 2022 11:06:19 +0200 Subject: [PATCH] Allow to switch language in edit and paste mode --- .../BackendView/ArticleViewListener.php | 18 ++++++++++++++--- .../BackendView/PageViewListener.php | 2 +- .../BackendView/ParentChildViewListener.php | 20 +++++++++++++++++-- src/EventListener/CallbackSetupListener.php | 1 + 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/EventListener/BackendView/ArticleViewListener.php b/src/EventListener/BackendView/ArticleViewListener.php index 00b7e5d..8598480 100644 --- a/src/EventListener/BackendView/ArticleViewListener.php +++ b/src/EventListener/BackendView/ArticleViewListener.php @@ -7,6 +7,7 @@ use Contao\ArticleModel; use Contao\Controller; use Contao\Input; +use Contao\Model; use Contao\Model\Collection; use Contao\PageModel; use Contao\Session; @@ -24,7 +25,10 @@ class ArticleViewListener extends AbstractViewListener */ protected function isSupported() { - return 'article' === (string) Input::get('do'); + return 'article' === (string) Input::get('do') + && (('edit' === Input::get('act') && empty(Input::get('table'))) + || ($this->getTable() === Input::get('table')) + ); } /** @@ -33,7 +37,15 @@ protected function isSupported() protected function getCurrentPage() { if (false === $this->currentArticle) { - $this->currentArticle = ArticleModel::findByPk($this->dataContainer->id); + if (Input::get('table') === $this->getTable() && !empty(Input::get('act'))) { + if ('paste' !== Input::get('act')) { + return null; + } + + $this->currentArticle = ArticleModel::findOneBy(['tl_article.id=(SELECT pid FROM tl_content WHERE id=?)'], [$this->dataContainer->id]); + } else { + $this->currentArticle = ArticleModel::findByPk($this->dataContainer->id); + } } if (null === $this->currentArticle) { @@ -96,7 +108,7 @@ protected function getAvailableLanguages(PageModel $page) */ protected function doSwitchView($id): void { - $url = Url::removeQueryString(['switchLanguage']); + $url = Url::removeQueryString(['switchLanguage', 'act', 'mode']); $url = Url::addQueryString('id='.$id, $url); Controller::redirect($url); diff --git a/src/EventListener/BackendView/PageViewListener.php b/src/EventListener/BackendView/PageViewListener.php index cc422e7..3b9bd82 100644 --- a/src/EventListener/BackendView/PageViewListener.php +++ b/src/EventListener/BackendView/PageViewListener.php @@ -17,7 +17,7 @@ class PageViewListener extends AbstractViewListener */ protected function isSupported() { - return 'page' === Input::get('do') || 'article' === Input::get('do'); + return 'page' === Input::get('do') || ('article' === Input::get('do') && 'edit' !== Input::get('act')); } /** diff --git a/src/EventListener/BackendView/ParentChildViewListener.php b/src/EventListener/BackendView/ParentChildViewListener.php index 6b027f2..6ca9bec 100644 --- a/src/EventListener/BackendView/ParentChildViewListener.php +++ b/src/EventListener/BackendView/ParentChildViewListener.php @@ -32,7 +32,15 @@ protected function getCurrentPage() /** @var string|Model $class */ $class = $this->getModelClass(); - $this->current = class_exists($class) ? $class::findByPk($this->dataContainer->id) : null; + if (!class_exists($class)) { + return null; + } +dump($class, $this->getTable()); + if ('paste' === Input::get('act') || ('edit' === Input::get('act') && 'tl_content' === $this->getTable())) { + $this->current = $class::findOneBy(['id=(SELECT pid FROM '.$this->getTable().' WHERE id=?)'], [$this->dataContainer->id]); + } else { + $this->current = $class::findByPk($this->dataContainer->id); + } } if (null === $this->current) { @@ -72,7 +80,11 @@ protected function getAvailableLanguages(PageModel $page) */ protected function doSwitchView($id): void { - $url = Url::removeQueryString(['switchLanguage']); + if ('edit' === Input::get('act') && 'tl_content' !== $this->getTable()) { + $url = Url::removeQueryString(['switchLanguage']); + } else { + $url = Url::removeQueryString(['switchLanguage', 'act', 'mode']); + } $url = Url::addQueryString('id='.$id, $url); Controller::redirect($url); @@ -121,6 +133,10 @@ private function getModelClass() { Controller::loadDataContainer($this->getTable()); + if ('edit' === Input::get('act') && 'tl_content' !== $this->getTable()) { + return Model::getClassFromTable($this->getTable()); + } + return Model::getClassFromTable($GLOBALS['TL_DCA'][$this->getTable()]['config']['ptable']); } diff --git a/src/EventListener/CallbackSetupListener.php b/src/EventListener/CallbackSetupListener.php index 8511b92..432ae70 100644 --- a/src/EventListener/CallbackSetupListener.php +++ b/src/EventListener/CallbackSetupListener.php @@ -17,6 +17,7 @@ class CallbackSetupListener 'tl_article' => [ 'Terminal42\ChangeLanguage\EventListener\DataContainer\ArticleListener', 'Terminal42\ChangeLanguage\EventListener\BackendView\PageViewListener', + 'Terminal42\ChangeLanguage\EventListener\BackendView\ArticleViewListener', ], 'tl_content' => [ 'Terminal42\ChangeLanguage\EventListener\BackendView\ArticleViewListener',