diff --git a/src/Reference/ArticleReference.php b/src/Reference/ArticleReference.php index 62d98d6..6cbe692 100644 --- a/src/Reference/ArticleReference.php +++ b/src/Reference/ArticleReference.php @@ -4,19 +4,15 @@ namespace Hofff\Contao\Content\Reference; -use Contao\ArticleModel; use Contao\CoreBundle\Security\Authentication\Token\TokenChecker; -use Contao\Model\Registry; use Doctrine\DBAL\Connection; -use Hofff\Contao\Content\Renderer\ArticleRenderer; -use Hofff\Contao\Content\Renderer\Renderer; use Hofff\Contao\Content\Renderer\Select; use Hofff\Contao\Content\Util\ContaoUtil; use Symfony\Contracts\Translation\TranslatorInterface; final class ArticleReference extends RelatedReference implements CreatesRenderer, CreatesSelect { - use ConfigureRenderer; + use CreateArticleRender; public function __construct( Connection $connection, @@ -43,24 +39,6 @@ public function backendIcon(array $row): string return $this->contaoUtil->isPublished((object) $reference) ? 'articles.svg' : 'articles_.svg'; } - /** {@inheritDoc} */ - public function createRenderer(array $reference, array $config): Renderer - { - $model = Registry::getInstance()->fetch('tl_article', $reference['id']); - if (! $model instanceof ArticleModel) { - $model = new ArticleModel(); - $model->setRow($reference); - } - - $renderer = new ArticleRenderer($this->tokenChecker); - $renderer->setArticle($model); - $renderer->setRenderContainer($config['render_container']); - - $this->configureRenderer($renderer, $config); - - return $renderer; - } - /** {@inheritDoc} */ public function createSelect(array $config, int $index, string $column): Select { diff --git a/src/Reference/CreateArticleRender.php b/src/Reference/CreateArticleRender.php new file mode 100644 index 0000000..4116124 --- /dev/null +++ b/src/Reference/CreateArticleRender.php @@ -0,0 +1,36 @@ +fetch('tl_article', $reference['id']); + if (! $model instanceof ArticleModel) { + $model = new ArticleModel(); + $model->setRow($reference); + } + + $renderer = new ArticleRenderer($this->tokenChecker); + $renderer->setArticle($model); + $renderer->setRenderContainer($config['render_container']); + + $this->configureRenderer($renderer, $config); + + return $renderer; + } +} diff --git a/src/Reference/PageReference.php b/src/Reference/PageReference.php index 77c099e..555ba8a 100644 --- a/src/Reference/PageReference.php +++ b/src/Reference/PageReference.php @@ -5,6 +5,7 @@ namespace Hofff\Contao\Content\Reference; use Contao\Controller; +use Contao\CoreBundle\Security\Authentication\Token\TokenChecker; use Contao\StringUtil; use Doctrine\DBAL\Connection; use Hofff\Contao\Content\Renderer\Select; @@ -15,11 +16,14 @@ use function array_merge; use function array_values; -final class PageReference extends RelatedReference implements CreatesSelect +final class PageReference extends RelatedReference implements CreatesSelect, CreatesRenderer, ProvidesOrderClause { + use CreateArticleRender; + public function __construct( Connection $connection, private readonly LanguageRelationDetector $langRelationDetector, + private readonly TokenChecker $tokenChecker, ) { parent::__construct($connection); } @@ -80,11 +84,14 @@ public function createSelect(array $config, int $index, string $column): Select article.pid = ? $targetCondition $sourceCondition -ORDER BY - article.sorting SQL; - return new Select('article', $sql, $params); + return new Select('page', $sql, $params); + } + + public function orderClause(): string + { + return 'hofff_content_index, sorting'; } protected function labelColumn(): string diff --git a/src/Reference/ProvidesOrderClause.php b/src/Reference/ProvidesOrderClause.php new file mode 100644 index 0000000..4cc39b7 --- /dev/null +++ b/src/Reference/ProvidesOrderClause.php @@ -0,0 +1,10 @@ + $select->sql, $selects); $params = array_merge(...array_map(static fn (Select $select) => $select->params, $selects)); $sql = '(' . implode(') UNION ALL (', $queries) . ')'; - $result = $this->connection->executeQuery($sql, $params); + + if ($reference instanceof ProvidesOrderClause) { + $sql .= ' ORDER BY ' . $reference->orderClause(); + } + + $result = $this->connection->executeQuery($sql, $params); while ($row = $result->fetchAssociative()) { $i = $row['hofff_content_index']; diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 7e2aeb1..0cc8331 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -75,6 +75,7 @@ +