作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com
对于UI模块,我们将原来自己编写的UI部分代码也搬了过来,同时也完善了Ctools 导出UI里面的逻辑,为其加上了AJAX效果。里面有段代码需要学习一下:
if (!empty($default_validator)) {
$plugin = ctools_get_plugins('field_validation', 'validator', $default_validator);
$class = ctools_plugin_get_class($plugin, 'handler');
$validator_class = new $class();
$validator_class->settings_form($form, $form_state);
$output = '<p>' . t('The following tokens are available for error message.' . '</p>');
$token_help = $validator_class->token_help();
if (!empty($token_help)) {
$items = array();
foreach ($token_help as $key => $value) {
$items[] = $key . ' == ' . $value;
}
$output .= theme('item_list',
array(
'items' => $items,
));
}
$form['token_help'] = array(
'#type' => 'fieldset',
'#title' => t('Replacement patterns'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#value' => $output,
'#id' => 'error-message-token-help',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#weight' => 6,
);
}
占位符的描述部分的代码是从views模块里面搬过来的。这里面值得学习的是如何获取插件:
$plugin = ctools_get_plugins('field_validation', 'validator', $default_validator);
从插件里面获取类:
$class = ctools_plugin_get_class($plugin, 'handler');
实例化类,调用成员函数settings_form:
$validator_class = new $class();
$validator_class->settings_form($form, $form_state);
这些代码和module文件中的加载插件对象的代码类似,只不过这里调用了settings_form。我前面讲的,为构造函数的参数设置默认值,主要就是为了方便这里的调用。
其它的改进,就是添加了所有的插件,很多插件都作了改进;除此以外,还编写了一些插件,为了模块升级的需要,都放在了field_validation_deprecated模块里面。这里给出一个插件的代码,这是field_validation_numeric2_validator的代码:
<?php
/**
* @file
* Field validation numeric validator.
*
*/
$plugin = array(
'label' => t('Numeric values'),
'description' => t('Verifies that user-entered values are numeric, with the option to specify min and / or max values.'),
'handler' => array(
'class' => 'field_validation_numeric2_validator',
),
);
class field_validation_numeric2_validator extends field_validation_validator {
/**
* Validate field.
*/
public function validate() {
$settings = $this->rule->settings;
if ($this->value != '') {
$flag = TRUE;
if (!is_numeric($this->value)) {
$flag = FALSE;
}
else{
if (isset($settings['min']) && $settings['min'] != '' && $this->value < $settings['min']) {
$flag = FALSE;
}
if (isset($settings['max']) && $settings['max'] != '' && $this->value > $settings['max']) {
$flag = FALSE;
}
}
if (!$flag) {
$token = array(
'[min]' => isset($settings['min']) ? $settings['min'] : '',
'[max]' => isset($settings['max']) ? $settings['max'] : '',
);
$this->set_error($token);
}
}
}
/**
* Provide settings option
*/
function settings_form(&$form, &$form_state) {
$default_settings = $this->get_default_settings($form, $form_state);
//print debug($default_settings);
$form['settings']['min'] = array(
'#title' => t('Minimum value'),
'#description' => t("Optionally specify the minimum value to validate the user-entered numeric value against."),
'#type' => 'textfield',
'#default_value' => isset($default_settings['min']) ? $default_settings['min'] : '',
);
$form['settings']['max'] = array(
'#title' => t('Maximum value'),
'#description' => t("Optionally specify the maximum value to validate the user-entered numeric value against."),
'#type' => 'textfield',
'#default_value' => isset($default_settings['max']) ? $default_settings['max'] : '',
);
}
/**
* Provide token help info for error message.
*/
public function token_help() {
$token_help = parent::token_help();
$token_help += array(
'[min]' => t('Minimum value'),
'[max]' => t('Maximum value'),
);
return $token_help;
}
}
这是Beta1里面包含的所有可用的插件: