Skip to content

Commit

Permalink
Allow to switch language in edit and paste mode
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 15, 2022
1 parent 62c43f2 commit e1ef27e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
18 changes: 15 additions & 3 deletions src/EventListener/BackendView/ArticleViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'))
);
}

/**
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/BackendView/PageViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down
20 changes: 18 additions & 2 deletions src/EventListener/BackendView/ParentChildViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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']);
}

Expand Down
1 change: 1 addition & 0 deletions src/EventListener/CallbackSetupListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit e1ef27e

Please sign in to comment.