作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com
2.0-RC1版本里面,修正了三个小问题,一个是文字错误,一个是日期验证里面星期的日期范围验证有问题,一个是机读名字有可能超过32个字符的问题。都是很小的问题,很快就修正了。在2.0的正式版里面,新增了一个date range2 验证器,删除了date validation模块,将它迁移到了field_validation_deprecated模块里面,修正了数值验证里面的一个小问题。之后基于Drupal8的插件系统,开发了Drupal8下面的1.0alpha1版,学习了Drupal8里面的插件机制。在后面是2.1版,这里面修正了部分验证规则只适用于管理员的问题,我们使用下面的代码搞定了这个问题:
//Always bypass all access checkings.
$query->addMetaData('account', user_load(1));
修正了唯一值验证在field collection item的编辑页面不能正常工作的问题。同时增加了property_validation模块,用来解决实体里面属性的验证问题。
在2.2里面,又新增了一个field_validation_extras模块,里面包含了10多个验证器;同时,新增了允许特定角色跳过验证的功能,我们为field_validation_validator增加了一个函数,bypass_validation,用来检查当前角色是否可以跳过验证:
/**
* Bypass validation.
*/
public function bypass_validation() {
global $user;
if (!empty($this->rule->settings['bypass']) && !empty($this->rule->settings['roles'])) {
$roles = array_filter($this->rule->settings['roles']);
$user_roles = array_keys($user->roles);
foreach ($roles as $role) {
if (in_array($role, $user_roles)) {
return TRUE;
}
}
}
return FALSE;
}
同时在设置表单里面新增了以下代码:
$form['settings']['bypass'] = array(
'#title' => t('Bypass validation'),
'#type' => 'checkbox',
'#default_value' => isset($default_settings['bypass']) ? $default_settings['bypass'] : FALSE,
);
$roles_options = user_roles(TRUE);
$form['settings']['roles'] = array(
'#title' => t('Roles'),
'#description' => t("Only the checked roles will be able to bypass this validation rule."),
'#type' => 'checkboxes',
'#options' => $roles_options,
'#default_value' => isset($default_settings['roles']) ? $default_settings['roles'] : array(),
'#states' => array(
'visible' => array(
':input[name="settings[bypass]"]' => array('checked' => TRUE),
),
),
);
这里面值得学习的是这段代码:
'#states' => array(
'visible' => array(
':input[name="settings[bypass]"]' => array('checked' => TRUE),
),
),
这里的':input[name="settings[bypass]"]',这里有点类似于jQuery的味道,这里面settings[bypass]可以在firefox下面的firebug里面查到。我们在第一集的表单系统里面,有一个非常简单的例子,我也是在编写这段代码的时候,才对这个结构有了更深的认识。因为这个表单是settings元素里面的,我开始觉得应该这样设置:
':input[name="bypass"]' => array('checked' => TRUE),
但是很遗憾,不起作用。多试了几种可能,才最终明白这里的用法。
当然,在2.2里面,还增加了对Feeds模块的集成,使得Feeds的导入,可以根据一个唯一的字段进行更新,这需要应用http://drupal.org/node/661606#comment-6481214里面所给的补丁。