function webform_entity_uuid_save in Webform UUID 7
Implements hook_entity_uuid_save().
File
- ./
webform_uuid.module, line 80 - Code for the Webform UUID module.
Code
function webform_entity_uuid_save($entity, $entity_type) {
// Only edit nodes that have webforms.
if ($entity_type != 'node' || empty($entity->webform)) {
return;
}
$entity->webform['nid'] = $entity->nid;
// Import validation rules if they exist.
if (empty($entity->webform['validation'])) {
return;
}
foreach ($entity->webform['validation'] as $rule) {
$rule['nid'] = $entity->webform['nid'];
// There is no uuid for a rule, so attempt to see if a rule exists already.
// This is imperfect because what if a rule changes dramatically?
// How would you delete the previous version?
// For now assume nid validator data = unique id.
// For now assume nid + validator + data = unique id.
// TODO: turn a rule into an entity or manually assign a UUID.
$rule_exists_ruleid = db_query("SELECT ruleid FROM {webform_validation_rule} WHERE nid = :nid AND validator = :validator AND data = :data", array(
':nid' => $rule['nid'],
':validator' => $rule['validator'],
':data' => $rule['data'],
))
->fetchField();
if (!is_numeric($rule_exists_ruleid)) {
// $rule['ruleid'] will be updated after node creation in hook_nodeapi().
unset($rule['ruleid']);
drupal_write_record('webform_validation_rule', $rule);
// Set $rule_exists_ruleid to our new ruleid for the components to use.
$rule_exists_ruleid = $rule['ruleid'];
}
foreach ($rule['components'] as $component) {
// If this pairing already exists, don't add it again.
$rule_component_exists = db_query("SELECT count(ruleid) FROM {webform_validation_rule_components} WHERE ruleid = :rid AND cid = :cid", array(
':rid' => $rule_exists_ruleid,
':cid' => $component['cid'],
))
->fetchField();
if ($rule_component_exists == 0) {
db_query("INSERT INTO {webform_validation_rule_components} (ruleid, cid) VALUES (:rid, :cid)", array(
':rid' => $rule_exists_ruleid,
':cid' => $component['cid'],
));
}
}
}
}