public
Last active

Module layout loader action helper

  • Download Gist
ModuleLayoutLoader.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
<?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']);
}
}
}
}
}
Modulelayout.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?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;
}
}
application.ini
INI
1 2 3 4
pluginPaths.My_Application_Resource = "My/Application/Resource"
 
resources.modulelayout.module.layout = "layout-script-name"
resources.modulelayout.module.layoutPath = "path/to/module/layout"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.