Skip to content

@philBrown /ModuleLayoutLoader.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Module layout loader action helper
pluginPaths.My_Application_Resource = "My/Application/Resource"
resources.modulelayout.module.layout = "layout-script-name"
resources.modulelayout.module.layoutPath = "path/to/module/layout"
<?php
class My_Application_Resource_Modulelayout extends Zend_Application_Resource_ResourceAbstract
{
/**
* Initialise controller action helper and register with the
* helper broker
*
* @return My_Controller_Action_Helper_ModuleLayoutLoader
*/
public function init()
{
$this->getBootstrap()->bootstrap('layout');
$helper = new My_Controller_Action_Helper_ModuleLayoutLoader($this->getOptions());
Zend_Controller_Action_HelperBroker::addHelper($helper);
return $helper;
}
}
<?php
class My_Controller_Action_Helper_ModuleLayoutLoader extends Zend_Controller_Action_Helper_Abstract
{
/**
* @var array
*/
protected $_moduleLayouts;
/**
* Helper constructor
*
* Set options for module layouts
*
* @param array|Zend_Config $options
* @return void
*/
public function __construct($options = null)
{
if (is_array($options)) {
$this->setOptions($options);
} else if ($options instanceof Zend_Config) {
$this->setOptions($options->toArray());
}
// Add current layout options for default module if not configured
if (!$this->hasModuleLayout('default') && null !== ($layout = Zend_Layout::getMvcInstance())) {
$this->addModuleLayout('default', array(
'layout' => $layout->getLayout(),
'layoutPath' => $layout->getLayoutPath()
));
}
}
/**
* Set module layout options
*
* @param array $options
* @return My_Controller_Action_Helper_ModuleLayoutLoader
*/
public function setOptions(array $options)
{
foreach ($options as $module => $layout) {
$this->addModuleLayout($module, $layout);
}
return $this;
}
/**
* Add module layout option
*
* @param string $module Module name
* @param string|array $layout Layout script name or array containing
* 'layout' and 'layoutPath' keys
*
* @return My_Controller_Action_Helper_ModuleLayoutLoader
* @throws Zend_Controller_Action_Exception
*/
public function addModuleLayout($module, $layout)
{
if (is_string($layout)) {
$layout = array(
'layout' => $layout
);
}
if (!is_array($layout)) {
throw new Zend_Controller_Action_Exception('Invalid module layout configuration');
}
$this->_moduleLayouts[(string) $module] = $layout;
return $this;
}
/**
* Has a layout option been configured for the named module
*
* @param string $module Module name
* @return bool
*/
public function hasModuleLayout($module)
{
return array_key_exists($module, $this->_moduleLayouts);
}
/**
* Get layout options for named module
*
* @param string $module Module name
* @return array
*/
public function getModuleLayout($module)
{
if ($this->hasModuleLayout($module)) {
return $this->_moduleLayouts[$module];
}
return null;
}
/**
* Hook into action controller preDispatch() workflow
*
* Switches layout options based on configuration
*
* @return void
*/
public function preDispatch()
{
$layout = Zend_Layout::getMvcInstance();
if (null !== $layout && $layout->isEnabled()) {
if ($moduleLayout = $this->getModuleLayout($this->getRequest()->getModuleName())) {
if (array_key_exists('layout', $moduleLayout)) {
$layout->setLayout($moduleLayout['layout']);
}
if (array_key_exists('layoutPath', $moduleLayout)) {
$layout->setLayoutPath($moduleLayout['layoutPath']);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.