Skip to content

Commit

Permalink
also restore the modules within the layout object
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Nov 26, 2018
1 parent 71d62a7 commit 9af2a64
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/EventListener/InheritArticleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class InheritArticleListener implements FrameworkAwareInterface

protected $columns;
protected $sections;
protected $modules;
protected $db;

public function __construct(Connection $db)
Expand All @@ -38,9 +39,10 @@ public function onGetPageLayout(PageModel $pageModel, LayoutModel $layoutModel,
$stringUtil = $this->framework->getAdapter(\Contao\StringUtil::class);
$moduleModel = $this->framework->getAdapter(\Contao\ModuleModel::class);

// Reset the columns
// Reset the cached data
$this->columns = [];
$this->sections = [];
$this->modules = serialize([]);

// Initialize modules and sections
$arrSections = ['header', 'left', 'right', 'main', 'footer'];
Expand Down Expand Up @@ -102,6 +104,7 @@ public function onGetPageLayout(PageModel $pageModel, LayoutModel $layoutModel,
}

// Empty the modules in the layout
$this->modules = $layoutModel->modules;
$layoutModel->modules = serialize([]);
}

Expand All @@ -112,6 +115,8 @@ public function onGeneratePage(PageModel $pageModel, LayoutModel $layoutModel, P
}

$pageRegular->Template->sections = $this->sections;

$layoutModel->modules = $this->modules;
}

protected function getFrontendModule(PageModel $page, $module, string $column): string
Expand All @@ -122,34 +127,34 @@ protected function getFrontendModule(PageModel $page, $module, string $column):
return $generatedModule;
}

// initialize pid
// Initialize pid
$pid = $page->id;

// get all the parents
// Get all the parents
$parents = [];

// search for next parent ids while parent id > 0
// Search for next parent ids while parent id > 0
do {
// get the next pid
// Get the next pid
$parent = $this->db->executeQuery('SELECT pid FROM tl_page WHERE id=?', [$pid])->fetch();

// if there are no parents anymore, break the loop
// If there are no parents anymore, break the loop
if (!$parent) {
break;
}

// get the parent id
// Get the parent id
$pid = $parent['pid'];

// store id
// Store id
$parents[] = $pid;
} while ($pid);

// initialize rendered article modules
// Initialize rendered article modules
$renderedArticles = [];
$renderedArticles[0] = $generatedModule;

// go through each parent
// Go through each parent
$level = 1;
foreach ($parents as $pid) {
$inheritArticles = $this->getInheritedArticles($pid, $column, $level);
Expand All @@ -160,19 +165,20 @@ protected function getFrontendModule(PageModel $page, $module, string $column):
}
}

// increase level
// iIncrease level
++$level;
}

// sort by key
// Sort by key
krsort($renderedArticles);

// return combined articles
// Return combined articles
return implode('', $renderedArticles);
}

protected function getInheritedArticles($pid, string $column, int $level): array
{
// Get adapters
$controller = $this->framework->getAdapter(\Contao\Controller::class);
$articleModel = $this->framework->getAdapter(\Contao\ArticleModel::class);
$pageModel = $this->framework->getAdapter(\Contao\PageModel::class);
Expand All @@ -196,7 +202,7 @@ protected function getInheritedArticles($pid, string $column, int $level): array
$renderedArticles = [];

if (null !== ($articles = $articleModel->findBy($columns, $values, $options))) {
// override global page object
// Override global page object
global $objPage;
$currentPage = $objPage;
$objPage = $pageModel->findById($pid);
Expand All @@ -205,7 +211,7 @@ protected function getInheritedArticles($pid, string $column, int $level): array
$renderedArticles[$article->inheritPriority] = $controller->getArticle($article->id, false, false, $column);
}

// reset global page object
// Reset global page object
$objPage = $currentPage;
}

Expand Down

0 comments on commit 9af2a64

Please sign in to comment.