How to extend a plugin indirectly
Wednesday, September 16th, 2009I’m using CakePHP1.2.4
At times, I want to modify/extend a plugin indirectly.
How do I do that?
It’s very simple, only import and inherit.
Here is the plugin(cakeplus) I made on the github.
http://github.com/ichikaway/cakeplus/tree
In this example , I use the behavior(add_validation_rule.php) of the cakeplus plugin and modify some method.
1. download the cakeplus plugin, and set in the “app/plugins” directory.
2. make new directory(cakeplusplus) in the “app/plugins”.
Now you can see directories as follow.
plugins/cakeplus/models/behaviors/add_validation_rule.php plugins/cakeplusplus/models/behaviors/
3. create “ext_add_validation_rule.php” file in “cakeplusplus/models/behaviors/” directory .
App::import('Model', 'cakeplus.AddValidationRule');
class ExtAddValidationRuleBehavior extends AddValidationRuleBehavior {
}
4. Now you can use the “ExtAddValidationRuleBehavior” class which has same functions as the “AddValidationRuleBehavior” class.
class Post extends AppModel {
var $name = 'Post';
//var $actsAs = array('Cakeplus.AddValidationRule');
var $actsAs = array('Cakeplusplus.ExtAddValidationRule');
}
5.If you want to modify or extend, you just override a method of the “AddValidationRuleBehavior” class in the “ExtAddValidationRuleBehavior” class as follow.
In this example, override the maxLengthJP method and change that function.
App::import('Model', 'cakeplus.AddValidationRule');
class ExtAddValidationRuleBehavior extends AddValidationRuleBehavior {
function maxLengthJP( &$model, $wordvalue, $length ) {
$word = array_shift($wordvalue);
//extend
$length = $length * 2;
return( mb_strlen( $word ) <= $length );
}
}




