From 9214d72a245903a1570132439c6d0dec87dab264 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Sun, 24 May 2020 15:47:32 -0400 Subject: [PATCH] Add a general-purpose farm_term_parse_names() function that farm_area_parse_names() can delegate to. --- modules/farm/farm_area/farm_area.module | 28 ++--------------- modules/farm/farm_term/farm_term.module | 41 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/modules/farm/farm_area/farm_area.module b/modules/farm/farm_area/farm_area.module index f1c1e6ccb..a8227a23c 100644 --- a/modules/farm/farm_area/farm_area.module +++ b/modules/farm/farm_area/farm_area.module @@ -143,33 +143,11 @@ function farm_area_type_options() { * @return array * Returns an array of area objects. If the area names exist, they will be * loaded from the database. Otherwise, they will be created. + * + * @see farm_term_parse_names() */ function farm_area_parse_names($names, $create = FALSE) { - - // Start with an empty array. - $areas = array(); - - // Explode the value into an array and only take the first value. - // (Same behavior as taxonomy autocomplete widget.) - $values = drupal_explode_tags($names); - - // If the value is empty, bail. - if (empty($values)) { - return $areas; - } - - // Iterate through the values and built an array of areas. - foreach ($values as $value) { - - // Create/load the area term. - $area = farm_term($value, 'farm_areas', $create); - - // Add to the array of areas. - $areas[] = $area; - } - - // Return the array of areas. - return $areas; + return farm_term_parse_names($names, 'farm_areas', $create); } /** diff --git a/modules/farm/farm_term/farm_term.module b/modules/farm/farm_term/farm_term.module index ff1421291..1d26b4083 100644 --- a/modules/farm/farm_term/farm_term.module +++ b/modules/farm/farm_term/farm_term.module @@ -56,3 +56,44 @@ function farm_term($name, $vocabulary, $create = TRUE, $load = TRUE) { return FALSE; } } + +/** + * Parse a string of term names and return an array of loaded term entities. If + * term names do not exist, they can optionally be created. + * + * @param string $names + * A comma-separated list of term names. + * @param bool $create + * Whether or not to create terms that don't exist. Defaults to FALSE. + * + * @return array + * Returns an array of term objects. If the term names exist, they will be + * loaded from the database. Otherwise, they will be created. + */ +function farm_term_parse_names($names, $vocabulary, $create = FALSE) { + + // Start with an empty array. + $terms = array(); + + // Explode the value into an array and only take the first value. + // (Same behavior as taxonomy autocomplete widget.) + $values = drupal_explode_tags($names); + + // If the value is empty, bail. + if (empty($values)) { + return $terms; + } + + // Iterate through the values and built an array of terms. + foreach ($values as $value) { + + // Create/load the term. + $term = farm_term($value, $vocabulary, $create); + + // Add to the array of terms. + $terms[] = $term; + } + + // Return the array of terms. + return $terms; +}