Skip to content

Commit

Permalink
Merge pull request #58 from oveleon/develop
Browse files Browse the repository at this point in the history
Fix page / layout template vars (#57)
  • Loading branch information
doishub authored Jan 5, 2022
2 parents 7475cf0 + 034fcf1 commit 2aaec7b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
31 changes: 14 additions & 17 deletions src/Resources/contao/classes/StyleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,30 +376,27 @@ public static function deserializeValues($arrValue){
return $arrValue;
}

/**
* Adding template variables defined in pages and layouts
*
* @param $objPage
* @param $objLayout
* @param $page
*/
public function onGeneratePage($objPage, $objLayout, &$page)
{
$arrStyles = array_filter(array_merge_recursive(
StringUtil::deserialize($objPage->styleManager, true),
StringUtil::deserialize($objLayout->styleManager, true)
));

$page->Template->styleManager = serialize($arrStyles);
}

/**
* Parse Template and set Variables
*
* @param $template
*/
public function onParseTemplate($template)
{
// Check page and template variables to pass them to the template
if(strpos($template->getName(), 'fe_page') === 0)
{
global $objPage;

$arrStyles = array_filter(array_merge_recursive(
StringUtil::deserialize($objPage->styleManager, true),
StringUtil::deserialize($template->layout->styleManager, true)
));

$template->styleManager = serialize($arrStyles);
}

// Build Styles object and assign it to the template
if(!($template->styleManager instanceof Styles))
{
$arrStyles = StringUtil::deserialize($template->styleManager);
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/contao/classes/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct($arrStyles=null)
*/
public function get($identifier, $arrGroups=null)
{
if($this->styles === null || !is_array($this->styles[ $identifier ]))
if($this->styles === null || !is_array(($this->styles[ $identifier ] ?? null)))
{
return '';
}
Expand Down
1 change: 0 additions & 1 deletion src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

// Hooks
$GLOBALS['TL_HOOKS']['parseTemplate'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'onParseTemplate');
$GLOBALS['TL_HOOKS']['generatePage'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'onGeneratePage');
$GLOBALS['TL_HOOKS']['loadFormField'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'onLoadFormField');
$GLOBALS['TL_HOOKS']['addCustomRegexp'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'addVariableRegexp');
$GLOBALS['TL_HOOKS']['executePostActions'][] = array('\\Oveleon\\ContaoComponentStyleManager\\Ajax', 'executePostActions');
Expand Down

0 comments on commit 2aaec7b

Please sign in to comment.