public
Last active

Yii => custom WidgetFactory, adds onBeforeCreateWidget, onAfterCreateWidget events. An example of how the custom class can be used has been provided as well, in the process solving a typical Yii issue, regarding how to set the default pagesize for widgets that use dataproviders (due to dataproviders initializing pagination, by which widget pager settings are typically ignored).

  • Download Gist
WidgetFactory.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
<?php
 
// components/WidgetFactory.php
 
/**
* Custom WidgetFactory class
* Provides two new events:
* - onBeforeCreateWidget
* - onAfterCreateWidget
*
* Allows for advanced global widget alteration, going a step further than CWidgetFactory's
* typical process which allows you to define default values for widgets.
*
* @author Jon Langevin (intel352) <jon@langevin.me>
* @link https://gist.github.com/4513672
*/
class WidgetFactory extends CWidgetFactory
{
 
/**
* Raised right BEFORE a widget is created.
* @param CEvent $event the event parameter
*/
public function onBeforeCreateWidget(CEvent $event)
{
$this->raiseEvent('onBeforeCreateWidget',$event);
}
 
/**
* Raised right AFTER a widget is created.
* @param CEvent $event the event parameter
*/
public function onAfterCreateWidget(CEvent $event)
{
$this->raiseEvent('onAfterCreateWidget',$event);
}
 
/**
* Creates a new widget based on the given class name and initial properties.
* @param CBaseController $owner the owner of the new widget
* @param string $className the class name of the widget. This can also be a path alias (e.g. system.web.widgets.COutputCache)
* @param array $properties the initial property values (name=>value) of the widget.
* @return CWidget the newly created widget whose properties have been initialized with the given values.
*/
public function createWidget($owner,$className,$properties=array())
{
if (! ($this->hasEventHandler('onBeforeCreateWidget') || $this->hasEventHandler('onAfterCreateWidget')))
return parent::createWidget($owner, $className, $properties);
 
$event=new WidgetEvent($this, $owner, $className, $properties);
if ($this->hasEventHandler('onBeforeCreateWidget'))
$this->raiseEvent('onBeforeCreateWidget', $event);
$event->widget=parent::createWidget($owner, $className, $properties);
if ($this->hasEventHandler('onAfterCreateWidget'))
$this->raiseEvent('onAfterCreateWidget', $event);
return $event->widget;
}
 
}
 
class WidgetEvent extends CEvent
{
/**
* @var CBaseController Owner of the new widget
*/
public $owner;
 
/**
* @var string Widget class name
*/
public $className;
 
/**
* @var CWidget The newly created widget
*/
public $widget;
 
/**
* Constructor.
* @param WidgetFactory $sender The WidgetFactory instance
* @param CBaseController $owner The owner of the new widget
* @param string $className The class name of the widget. This can also be a path alias.
* @param array $params The initial property values (name=>value) of the widget.
*/
public function __construct(WidgetFactory $sender, CBaseController $owner, $className, array $params=array())
{
parent::__construct($sender, $params);
$this->owner=$owner;
$this->className=$className;
}
}
main.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
<?php
 
// config/main.php
 
return array(
// ...
'components'=>array(
// ...
'widgetFactory'=>array(
'class'=>'WidgetFactory',
'onAfterCreateWidget'=>function(WidgetEvent $event){
static $defaultPageSize=50;
$widget=$event->widget;
if ($widget instanceof CBaseListView) {
/** @var CBaseListView $widget */
if ($widget->dataProvider!==null && $widget->dataProvider->pagination!==false)
$widget->dataProvider->pagination->pageSize=$defaultPageSize;
}
},
),
// ...
),
);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.