通过钩子ContentHandlerDefaultModelFor
实现。
public static function onContentHandlerDefaultModelFor( Title $title, &$model ) { ... }
-
$title
用于判断选择内容模型。 -
&$model
模型引用,string类型。一般是全局常量,或者自定义模型名称。
define( 'CONTENT_MODEL_WIKITEXT', 'wikitext' );
define( 'CONTENT_MODEL_JAVASCRIPT', 'javascript' );
define( 'CONTENT_MODEL_CSS', 'css' );
define( 'CONTENT_MODEL_TEXT', 'text' );
define( 'CONTENT_MODEL_JSON', 'json' );
实现钩子只需两步。
扩展extension.json中配置
{
"Hooks": {
"ContentHandlerDefaultModelFor": "GoatHooks::onContentHandlerDefaultModelFor"
}
}
编写钩子
Title.php
类有许多方法,依据具体需求编写判断条件。
例如:新建页面默认使用自定义钩子goat
。
$title->exists()
判断是页面否存在。
<?php
class GoatHooks {
public static function onContentHandlerDefaultModelFor( $title, &$model ) {
if (!$title->exists()) {
$model = 'goat';
}
}
}
GitHub地址:Goat Demo
官方接口地址:ContentHandlerDefaultModelFor
推荐:自定义内容模型基础
网友评论