Skip to content

Commit 2e1d42a

Browse files
committed
fix 'could not find template' error (#8)
1 parent 6e08af9 commit 2e1d42a

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

.php_cs.dist

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ This file is part of the InheritArticle Bundle.
99
EOF;
1010

1111
$finder = PhpCsFixer\Finder::create()
12-
->exclude('Resources')
13-
->exclude('Fixtures')
1412
->in([
1513
__DIR__.'/src',
1614
])

src/EventListener/InheritArticleListener.php

+39-7
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,42 @@
1212

1313
namespace InheritArticleBundle\EventListener;
1414

15-
use Contao\CoreBundle\Framework\FrameworkAwareInterface;
16-
use Contao\CoreBundle\Framework\FrameworkAwareTrait;
15+
use Contao\CoreBundle\Framework\ContaoFramework;
16+
use Contao\CoreBundle\Image\PictureFactory;
1717
use Contao\LayoutModel;
1818
use Contao\PageModel;
1919
use Contao\PageRegular;
2020
use Doctrine\DBAL\Connection;
2121

22-
class InheritArticleListener implements FrameworkAwareInterface
22+
class InheritArticleListener
2323
{
24-
use FrameworkAwareTrait;
25-
24+
/** @var array */
2625
protected $columns;
26+
27+
/** @var array */
2728
protected $sections;
29+
30+
/** @var string */
2831
protected $modules;
32+
33+
/** @var Connection */
2934
protected $db;
3035

31-
public function __construct(Connection $db)
36+
/** @var ContaoFramework */
37+
protected $framework;
38+
39+
/** @var PictureFactory */
40+
protected $pictureFactory;
41+
42+
public function __construct(Connection $db, ContaoFramework $framework, PictureFactory $pictureFactory)
3243
{
3344
$this->db = $db;
45+
$this->framework = $framework;
46+
$this->pictureFactory = $pictureFactory;
3447
}
3548

3649
public function onGetPageLayout(PageModel $pageModel, LayoutModel $layoutModel, PageRegular $pageRegular): void
3750
{
38-
$this->framework->initialize();
3951
$stringUtil = $this->framework->getAdapter(\Contao\StringUtil::class);
4052
$moduleModel = $this->framework->getAdapter(\Contao\ModuleModel::class);
4153

@@ -62,6 +74,9 @@ public function onGetPageLayout(PageModel $pageModel, LayoutModel $layoutModel,
6274
if (null !== $objModules || 0 === $arrModules[0]['mod'] || '0' === $arrModules[0]['mod']) { // see #4137
6375
$arrMapper = [];
6476

77+
// Set theme and layout related information in the page object (see #8)
78+
$this->applyThemeAndLayout($pageModel, $layoutModel);
79+
6580
// Create a mapper array in case a module is included more than once (see #4849)
6681
if (null !== $objModules) {
6782
while ($objModules->next()) {
@@ -220,4 +235,21 @@ protected function getInheritedArticles($pid, string $column, int $level): array
220235

221236
return $renderedArticles;
222237
}
238+
239+
/**
240+
* Sets theme and layout related information in the page object,
241+
* which is usually done by Contao after the getPageLayout hook.
242+
*/
243+
protected function applyThemeAndLayout(PageModel $page, LayoutModel $layout): void
244+
{
245+
/** @var \Contao\ThemeModel $theme */
246+
$theme = $layout->getRelated('pid');
247+
$this->pictureFactory->setDefaultDensities($theme->defaultImageDensities);
248+
$page->layoutId = $layout->id;
249+
$page->template = $layout->template ?: 'fe_page';
250+
$page->templateGroup = $theme->templates;
251+
[$strFormat, $strVariant] = explode('_', $layout->doctype);
252+
$page->outputFormat = $strFormat;
253+
$page->outputVariant = $strVariant;
254+
}
223255
}

src/Resources/config/services.yml

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
services:
2-
_instanceof:
3-
Contao\CoreBundle\Framework\FrameworkAwareInterface:
4-
calls:
5-
- ["setFramework", ["@contao.framework"]]
6-
7-
contao_inherit_article.listener:
8-
class: InheritArticleBundle\EventListener\InheritArticleListener
9-
arguments: ['@doctrine.dbal.default_connection']
2+
InheritArticleBundle\EventListener\InheritArticleListener:
3+
arguments:
4+
- '@doctrine.dbal.default_connection'
5+
- '@contao.framework'
6+
- '@contao.image.picture_factory'
107
public: true

src/Resources/contao/config/config.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010
* @license LGPL-3.0-or-later
1111
*/
1212

13-
$GLOBALS['TL_HOOKS']['getPageLayout'][] = ['contao_inherit_article.listener', 'onGetPageLayout'];
14-
$GLOBALS['TL_HOOKS']['generatePage'][] = ['contao_inherit_article.listener', 'onGeneratePage'];
13+
use InheritArticleBundle\EventListener\InheritArticleListener;
14+
15+
$GLOBALS['TL_HOOKS']['getPageLayout'][] = [InheritArticleListener::class, 'onGetPageLayout'];
16+
$GLOBALS['TL_HOOKS']['generatePage'][] = [InheritArticleListener::class, 'onGeneratePage'];

0 commit comments

Comments
 (0)