View source
<?php
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'field_validation') . '/' . 'field_validation.validators.inc';
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'field_validation') . '/' . 'field_validation.rules.inc';
function field_validation_menu() {
$items = array();
foreach (entity_get_info() as $entity_type => $entity_info) {
if ($entity_info['fieldable']) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
if (isset($bundle_info['admin'])) {
$path = $bundle_info['admin']['path'];
if (isset($bundle_info['admin']['bundle argument'])) {
$bundle_arg = $bundle_info['admin']['bundle argument'];
$bundle_pos = (string) $bundle_arg;
}
else {
$bundle_arg = $bundle_name;
$bundle_pos = '0';
}
$field_position = count(explode('/', $path)) + 1;
$access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array(
'access callback',
'access arguments',
)));
$access += array(
'access callback' => 'user_access',
'access arguments' => array(
'administer site configuration',
),
);
$items["{$path}/fields/%field_ui_menu/validation"] = array(
'load arguments' => array(
$entity_type,
$bundle_arg,
$bundle_pos,
'%map',
),
'title' => 'Validation',
'page callback' => 'field_validation_callback_dispatch',
'page arguments' => array(
$field_position,
),
'type' => MENU_LOCAL_TASK,
'file' => 'field_validation.admin.inc',
) + $access;
$items[$path . '/fields/%field_ui_menu/validation/add'] = array(
'load arguments' => array(
$entity_type,
$bundle_arg,
$bundle_pos,
'%map',
),
'title' => 'Add validation',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'field_validation_manage_rule',
$field_position,
'add',
),
'type' => MENU_CALLBACK,
'file' => 'field_validation.admin.inc',
) + $access;
$items["{$path}/fields/%field_ui_menu/validation/edit"] = array(
'load arguments' => array(
$entity_type,
$bundle_arg,
$bundle_pos,
'%map',
),
'title' => 'Edit rule',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'field_validation_manage_rule',
$field_position,
'edit',
),
'type' => MENU_CALLBACK,
'file' => 'field_validation.admin.inc',
) + $access;
$items["{$path}/fields/%field_ui_menu/validation/overwrite"] = array(
'load arguments' => array(
$entity_type,
$bundle_arg,
$bundle_pos,
'%map',
),
'title' => 'Overwrite rule',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'field_validation_manage_rule',
$field_position,
'overwrite',
),
'type' => MENU_CALLBACK,
'file' => 'field_validation.admin.inc',
) + $access;
$items["{$path}/fields/%field_ui_menu/validation/delete"] = array(
'load arguments' => array(
$entity_type,
$bundle_arg,
$bundle_pos,
'%map',
),
'title' => 'Delete rule',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'field_validation_delete_rule',
),
'type' => MENU_CALLBACK,
'file' => 'field_validation.admin.inc',
) + $access;
}
}
}
}
return $items;
}
function field_validation_theme() {
return array(
'field_validation_manage_add_rule' => array(
'variables' => array(
'instance' => NULL,
),
),
'field_validation_manage_overview' => array(
'variables' => array(
'rules' => NULL,
'instance' => NULL,
),
),
);
}
function field_validation_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter' && array_key_exists('field_validation', $implementations)) {
$group = $implementations['field_validation'];
unset($implementations['field_validation']);
$implementations['field_validation'] = $group;
}
}
function field_validation_field_attach_validate($entity_type, $entity, &$errors) {
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
ctools_include('export');
$rules = ctools_export_load_object('field_validation_rule', 'conditions', array(
'entity_type' => $entity_type,
'bundle' => $bundle,
));
if ($rules) {
foreach ($rules as $rule_obj) {
$rule = (array) $rule_obj;
if (!empty($rule['disabled'])) {
continue;
}
$field_name = $rule['field_name'];
$field = field_info_field($field_name);
$languages = field_available_languages($entity_type, $field);
foreach ($languages as $langcode) {
$items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
foreach (module_implements("field_validation_validate") as $module) {
$function = $module . '_' . "field_validation_validate";
if (function_exists($function)) {
$function($rule['validator'], $rule, $entity, $langcode, $items, $errors);
}
}
}
}
}
}
function theme_field_validation_manage_add_rule($variables) {
$instance = $variables['instance'];
$output = '';
$validators = field_validation_get_validators();
if ($validators) {
$output = '<h3>' . t('Add a validation rule') . '</h3>';
$output .= '<dl>';
foreach ($validators as $validator_key => $validator_info) {
$item = '';
$path = isset($_GET['q']) ? $_GET['q'] : '';
$url = $path . '/add/' . $validator_key;
$components = ' (' . implode(', ', $validator_info['field_types']) . ')';
$item = '<dt>' . l($validator_info['name'], $url, array(
"query" => drupal_get_destination(),
)) . '</dt>';
$item .= '<dd>';
if ($validator_info['description']) {
$item .= $validator_info['description'] . ' ';
}
$item .= $components . '</dd>';
$output .= $item;
}
$output .= '</dl>';
}
return $output;
}
function field_validation_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
$rules = field_validation_get_field_rules($instance);
if ($rules) {
foreach (array_keys($rules) as $ruleid) {
field_validation_dynamic_delete_rule($ruleid);
}
}
}
function field_validation_rule_save($values) {
if ($values['action'] == 'add' || $values['action'] == 'overwrite') {
drupal_write_record('field_validation_rule', $values);
$ruleid = $values['ruleid'];
if ($ruleid) {
module_invoke_all('field_validation', 'rule', 'add', $values);
}
}
if ($values['action'] == 'edit') {
drupal_write_record('field_validation_rule', $values, 'ruleid');
$ruleid = $values['ruleid'];
if ($ruleid) {
module_invoke_all('field_validation', 'rule', 'edit', $values);
}
}
}
function field_validation_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$bundle = field_extract_bundle($entity_type, $bundle);
$admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
$table =& $form['fields'];
$table['#header'][6]['colspan'] = 3;
$instances = field_info_instances($entity_type, $bundle);
foreach (element_children($table) as $key) {
if (array_key_exists($key, $instances)) {
$field = field_info_field($instances[$key]['field_name']);
$admin_field_path = $admin_path . '/fields/' . $instances[$key]['field_name'];
$table[$key]['validation'] = array(
'#type' => 'link',
'#title' => t('validate'),
'#href' => $admin_field_path . '/validation',
'#options' => array(
'attributes' => array(
'title' => t('Manage validation rules.'),
),
),
);
}
else {
$table[$key]['validation'] = array(
'#markup' => '',
);
}
}
}
function field_validation_rule_load($name) {
ctools_include('export');
$result = ctools_export_load_object('field_validation_rule', 'names', array(
$name,
));
if (isset($result[$name])) {
return $result[$name];
}
}
function field_validation_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && $plugin == 'export_ui') {
return 'plugins/' . $plugin;
}
}
function field_validation_ctools_plugin_api($owner, $api) {
if ($owner == 'field_validation' && $api == 'default_field_validation_rules') {
return array(
'version' => 1,
);
}
}
function field_validation_dynamic_delete_rule($ruleid) {
db_delete('field_validation_rule')
->condition('ruleid', $ruleid)
->execute();
}