Skip to content

Commit

Permalink
fix 'could not find template' error (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Sep 20, 2019
1 parent 6e08af9 commit 2e1d42a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
2 changes: 0 additions & 2 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ This file is part of the InheritArticle Bundle.
EOF;

$finder = PhpCsFixer\Finder::create()
->exclude('Resources')
->exclude('Fixtures')
->in([
__DIR__.'/src',
])
Expand Down
46 changes: 39 additions & 7 deletions src/EventListener/InheritArticleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,42 @@

namespace InheritArticleBundle\EventListener;

use Contao\CoreBundle\Framework\FrameworkAwareInterface;
use Contao\CoreBundle\Framework\FrameworkAwareTrait;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Image\PictureFactory;
use Contao\LayoutModel;
use Contao\PageModel;
use Contao\PageRegular;
use Doctrine\DBAL\Connection;

class InheritArticleListener implements FrameworkAwareInterface
class InheritArticleListener
{
use FrameworkAwareTrait;

/** @var array */
protected $columns;

/** @var array */
protected $sections;

/** @var string */
protected $modules;

/** @var Connection */
protected $db;

public function __construct(Connection $db)
/** @var ContaoFramework */
protected $framework;

/** @var PictureFactory */
protected $pictureFactory;

public function __construct(Connection $db, ContaoFramework $framework, PictureFactory $pictureFactory)
{
$this->db = $db;
$this->framework = $framework;
$this->pictureFactory = $pictureFactory;
}

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

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

// Set theme and layout related information in the page object (see #8)
$this->applyThemeAndLayout($pageModel, $layoutModel);

// Create a mapper array in case a module is included more than once (see #4849)
if (null !== $objModules) {
while ($objModules->next()) {
Expand Down Expand Up @@ -220,4 +235,21 @@ protected function getInheritedArticles($pid, string $column, int $level): array

return $renderedArticles;
}

/**
* Sets theme and layout related information in the page object,
* which is usually done by Contao after the getPageLayout hook.
*/
protected function applyThemeAndLayout(PageModel $page, LayoutModel $layout): void
{
/** @var \Contao\ThemeModel $theme */
$theme = $layout->getRelated('pid');
$this->pictureFactory->setDefaultDensities($theme->defaultImageDensities);
$page->layoutId = $layout->id;
$page->template = $layout->template ?: 'fe_page';
$page->templateGroup = $theme->templates;
[$strFormat, $strVariant] = explode('_', $layout->doctype);
$page->outputFormat = $strFormat;
$page->outputVariant = $strVariant;
}
}
13 changes: 5 additions & 8 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
services:
_instanceof:
Contao\CoreBundle\Framework\FrameworkAwareInterface:
calls:
- ["setFramework", ["@contao.framework"]]

contao_inherit_article.listener:
class: InheritArticleBundle\EventListener\InheritArticleListener
arguments: ['@doctrine.dbal.default_connection']
InheritArticleBundle\EventListener\InheritArticleListener:
arguments:
- '@doctrine.dbal.default_connection'
- '@contao.framework'
- '@contao.image.picture_factory'
public: true
6 changes: 4 additions & 2 deletions src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
* @license LGPL-3.0-or-later
*/

$GLOBALS['TL_HOOKS']['getPageLayout'][] = ['contao_inherit_article.listener', 'onGetPageLayout'];
$GLOBALS['TL_HOOKS']['generatePage'][] = ['contao_inherit_article.listener', 'onGeneratePage'];
use InheritArticleBundle\EventListener\InheritArticleListener;

$GLOBALS['TL_HOOKS']['getPageLayout'][] = [InheritArticleListener::class, 'onGetPageLayout'];
$GLOBALS['TL_HOOKS']['generatePage'][] = [InheritArticleListener::class, 'onGeneratePage'];

0 comments on commit 2e1d42a

Please sign in to comment.