From df3e66246f0a33148354a6cce92db01ed71da96e Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 3 Jan 2015 15:15:57 +1100 Subject: [PATCH] Fixes #839 - When rendering partials from component PHP code, be firm about the context --- modules/cms/classes/ComponentBase.php | 10 ++++++++++ modules/cms/classes/Controller.php | 28 +++++++++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/modules/cms/classes/ComponentBase.php b/modules/cms/classes/ComponentBase.php index e62d38260..1c6dcd60b 100644 --- a/modules/cms/classes/ComponentBase.php +++ b/modules/cms/classes/ComponentBase.php @@ -168,6 +168,16 @@ abstract class ComponentBase extends Extendable return $this->alias; } + /** + * Renders a requested partial in context of this component, + * see Cms\Classes\Controller@renderPartial for usage. + */ + public function renderPartial() + { + $this->controller->setComponentContext($this); + return call_user_func_array([$this->controller, 'renderPartial'], func_get_args()); + } + /** * Executes the event cycle when running an AJAX handler. * @return boolean Returns true if the handler was found. Returns false otherwise. diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index 0ff69bdc3..50c18ebcf 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -84,11 +84,6 @@ class Controller extends BaseController */ protected $pageContents; - /** - * @var string Alias name of an executing component. - */ - protected $componentContext; - /** * @var array A list of variables to pass to the page. */ @@ -99,8 +94,19 @@ class Controller extends BaseController */ protected $statusCode = 200; + /** + * @var self Cache of self + */ protected static $instance = null; + /** + * @var Cms\Classes\ComponentBase Object of the active component, used internally. + */ + protected $componentContext; + + /** + * @var array Component partial stack, used internally. + */ protected $partialComponentStack = []; /** @@ -701,7 +707,7 @@ class Controller extends BaseController } elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) { if ($throwException) { - throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name])); + throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$partialName])); } else { return false; @@ -1095,6 +1101,16 @@ class Controller extends BaseController return null; } + /** + * Set the component context manually, used by Components when calling renderPartial. + * @param ComponentBase $component + * @return void + */ + public function setComponentContext(ComponentBase $component) + { + $this->componentContext = $component; + } + /** * Sets component property values from partial parameters. * The property values should be defined as {{ param }}.