From 96ababd133f357001b28638be274ef6d67143210 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Fri, 15 Jan 2010 10:49:46 +0000 Subject: [PATCH] - Patch #611752 by catch: taxonomy_field_validate() calls taxonomy_allowed_values(). --- modules/taxonomy/taxonomy.module | 45 +++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index ab8c87c06b8..cad3049f86c 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -1045,16 +1045,53 @@ function taxonomy_field_schema($field) { /** * Implements hook_field_validate(). * + * Taxonomy field settings allow for either a single vocabulary ID, multiple + * vocabulary IDs, or sub-trees of a vocabulary to be specified as allowed + * values, although only the first of these is supported via the field UI. + * Confirm that terms entered as values meet at least one of these conditions. + * * Possible error codes: * - 'taxonomy_term_illegal_value': The value is not part of the list of allowed values. */ function taxonomy_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) { - $allowed_values = taxonomy_allowed_values($field); - $widget = field_info_widget_types($instance['widget']['type']); - + // Build an array of term IDs so they can be loaded with + // taxonomy_term_load_multiple(); foreach ($items as $delta => $item) { if (!empty($item['tid'])) { - if (!isset($allowed_values[$item['tid']])) { + $tids[] = $item['tid']; + } + } + if (!empty($tids)) { + $terms = taxonomy_term_load_multiple($tids); + + // Check each item to ensure it can be found in the allowed values for this + // field. + foreach ($items as $delta => $item) { + $validate = TRUE; + if (!empty($item['tid'])) { + $validate = FALSE; + foreach ($field['settings']['allowed_values'] as $settings) { + // If no parent is specified, check if the term is in the vocabulary. + if (isset($settings['vid']) && empty($settings['parent'])) { + if ($settings['vid'] == $terms[$item['tid']]->vid) { + $validate = TRUE; + break; + } + } + // If a parent is specified, then to validate it must appear in the + // array returned by taxonomy_get_parents_all(). + elseif (!empty($settings['parent'])) { + $ancestors = taxonomy_get_parents_all($item['tid']); + foreach ($ancestors as $ancestor) { + if ($ancestor->tid == $settings['parent']) { + $validate = TRUE; + break 2; + } + } + } + } + } + if (!$validate) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'taxonomy_term_reference_illegal_value', 'message' => t('%name: illegal value.', array('%name' => t($instance['label']))),