diff --git a/tide_core.install b/tide_core.install index 294ef7577..c556c44f2 100644 --- a/tide_core.install +++ b/tide_core.install @@ -71,7 +71,12 @@ function tide_core_install() { ->save(TRUE); // Creates terms for Topic vocabulary. - _tide_core_create_topic_terms(); + $vocabulary_details = [ + 'vid' => 'content_category', + 'description' => 'Categories assigned to all content to assist with filtering in content collection and search', + 'name' => 'Content Category', + ]; + _tide_core_adding_default_taxonomy(_return_content_category_terms(), $vocabulary_details); // Update default Editorial workflow of Content Moderation. _tide_core_update_editorial_workflow(); @@ -2021,33 +2026,22 @@ function tide_core_update_8098() { * Run _add_default_content_category_taxonomy(). */ function tide_core_update_8099() { - _add_default_content_category_taxonomy(); + $vocabulary_details = [ + 'vid' => 'content_category', + 'description' => 'Categories assigned to all content to assist with filtering in content collection and search', + 'name' => 'Content Category', + ]; + _tide_core_adding_default_taxonomy(_return_content_category_terms(), $vocabulary_details); if (\Drupal::moduleHandler()->moduleExists('term_reference_tree') === FALSE) { \Drupal::service('module_installer')->install(['term_reference_tree']); } } /** - * Adds or updates the default 'Content Category' taxonomy. - * - * This function creates a new vocabulary named 'Content Category' if - * it doesn't exist. It then populates this vocabulary with a predefined - * set of terms and their respective child terms. Each term is assigned a weight - * for ordering purposes. - */ -function _add_default_content_category_taxonomy() { - $vocabulary_id = 'content_category'; - // Load the vocabulary. If it doesn't exist, create it. - $vocabulary = Vocabulary::load($vocabulary_id); - if (!$vocabulary) { - $vocabulary = Vocabulary::create([ - 'vid' => $vocabulary_id, - 'description' => 'Categories assigned to all content to assist with filtering in content collection and search', - 'name' => 'Content category', - ]); - $vocabulary->save(); - } - $terms = [ + * Returns predefined content_category terms. + */ +function _return_content_category_terms() { + return [ 'Events' => [ 'Events', ], @@ -2105,22 +2099,71 @@ function _add_default_content_category_taxonomy() { 'Memo', ], ]; - $weight = 0; - // Iterate over each parent term. - foreach ($terms as $parent_term_name => $child_terms) { - // Create or load the parent term, then assign a weight for ordering. - $parent_term = _create_or_load_term($vocabulary_id, $parent_term_name, 0, $weight); - // Initialize the weight for child terms. - $child_weight = 0; - // Iterate over each child term of the current parent term. - foreach ($child_terms as $child_term_name) { - // Create or load the child term, then assign it a parent and a weight. - _create_or_load_term($vocabulary_id, $child_term_name, $parent_term->id(), $child_weight); - $child_weight++; - } - // Increment the weight for the next parent term. - $weight++; +} + +/** + * Adds or updates a taxonomy with nested terms up to a specified depth. + * + * @param array $terms + * An associative array of terms where keys are term names and values are + * either arrays of child term names or associative arrays with nested terms. + * Example structure for $terms: + * $terms = [ + * 'Events' => [ + * 'Events', + * ], + * 'Policy and legislation' => [ + * 'Legislation', + * 'Regulation' => [ + * 'hello' => [ + * 'world', + * 'new' => [ + * 'era', + * ], + * ], + * ], + * ], + * ]; + * This represents a hierarchy with 'Events' and 'Policy and legislation' + * as top-level terms, and nested children underneath them. + * @param array $vocabulary_details + * An associative array with vocabulary details. Example: + * $vocabulary_details = [ + * 'vid' => 'content_category', + * 'description' => 'Categories for content filtering and search', + * 'name' => 'Content Category', + * ];. + * @param int $max_depth + * The maximum hierarchy depth to create, defaulting to 4. Deeper nested + * terms beyond this depth will be ignored. + */ +function _tide_core_adding_default_taxonomy(array $terms, array $vocabulary_details, $max_depth = 4) { + $vocabulary_id = $vocabulary_details['vid']; + // Load or create the vocabulary. + $vocabulary = Vocabulary::load($vocabulary_id); + if (!$vocabulary) { + $vocabulary = Vocabulary::create($vocabulary_details); + $vocabulary->save(); } + + // Function to recursively add terms to the vocabulary. + $add_terms_recursively = function ($parent_tid, array $terms, $weight, $depth) use (&$add_terms_recursively, $vocabulary_id, $max_depth) { + if ($depth > $max_depth) { + return; + } + + foreach ($terms as $term_name => $child_terms) { + if (is_array($child_terms)) { + $parent_term = _create_or_load_term($vocabulary_id, $term_name, $parent_tid, $weight++); + $add_terms_recursively($parent_term->id(), $child_terms, 0, $depth + 1); + } + else { + _create_or_load_term($vocabulary_id, $child_terms, $parent_tid, $weight++); + } + } + }; + + $add_terms_recursively(0, $terms, 0, 1); } /**