Skip to content

Commit d477655

Browse files
committed
Adds widget hooks
1 parent 8b3dbfa commit d477655

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

Ajax/common/Widget.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
use Ajax\semantic\html\collections\form\traits\FormTrait;
1919
use Ajax\common\html\BaseWidget;
2020
use Ajax\semantic\html\modules\HtmlModal;
21+
use Ajax\common\html\traits\BaseHooksTrait;
2122

2223
abstract class Widget extends HtmlDoubleElement {
23-
use FieldAsTrait,FormTrait;
24+
use FieldAsTrait,FormTrait,BaseHooksTrait;
2425

2526
/**
2627
* @var string classname
@@ -52,7 +53,6 @@ abstract class Widget extends HtmlDoubleElement {
5253

5354
protected $_generated;
5455

55-
5656
public function __construct($identifier,$model,$modelInstance=NULL) {
5757
parent::__construct($identifier);
5858
$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Ajax\common\html\traits;
4+
5+
trait BaseHooksTrait {
6+
protected $_hooks=[];
7+
8+
/**
9+
* @param string $hookKey
10+
* @return boolean
11+
*/
12+
public function hookExists($hookKey){
13+
return isset($this->_hooks[$hookKey]);
14+
}
15+
16+
/**
17+
* @param string $hookKey
18+
* @return callable|NULL
19+
*/
20+
public function getHook($hookKey){
21+
if(isset($this->_hooks[$hookKey])){
22+
return $this->_hooks[$hookKey];
23+
}
24+
return null;
25+
}
26+
27+
/**
28+
* Adds a new Hook
29+
* @param String $hookKey
30+
* @param callable $callable
31+
*/
32+
public function addHook($hookKey,$callable){
33+
$this->_hooks[$hookKey]=$callable;
34+
}
35+
36+
/**
37+
* Executes the hook with key $hookKey
38+
* @param string $hookKey
39+
* @param mixed|null $variable
40+
* @return void|mixed
41+
*/
42+
public function execHook($hookKey,$variable=null){
43+
if(($hook=$this->getHook($hookKey))!=null){
44+
return $hook($variable);
45+
}
46+
return;
47+
}
48+
}
49+

Ajax/common/html/traits/BaseHtmlPropertiesTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function addProperties($properties) {
6565
return $this;
6666
}
6767

68-
protected function removePropertyValue($name, $value) {
68+
public function removePropertyValue($name, $value) {
6969
$this->_self->properties[$name]=\str_replace($value, "", $this->_self->properties[$name]);
7070
return $this;
7171
}
@@ -100,7 +100,7 @@ public function removeProperty($name) {
100100
unset($this->_self->properties[$name]);
101101
return $this;
102102
}
103-
103+
104104
public function propertyContains($propertyName, $value) {
105105
$values=$this->_self->getProperty($propertyName);
106106
if (isset($values)) {

Ajax/semantic/widgets/dataform/DataForm.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ protected function _generateFields($form,$values,$headers,$sepFirst,$wrappers){
8888
if(isset($wrapper)){
8989
$added->wrap($wrapper[0],$wrapper[1]);
9090
}
91+
$this->execHook("onGenerateFields",$added);
92+
}
93+
94+
/**
95+
* Function called when a field is generated
96+
* the generated field is the first parameter
97+
* @param callable $callback the fonction to call when a field is generated
98+
*/
99+
public function onGenerateField($callback){
100+
$this->addHook("onGenerateFields",$callback);
91101
}
92102

93103
/**

0 commit comments

Comments
 (0)