From c838f96e1d252e7f2a4ef7cac790f846d05ab6a7 Mon Sep 17 00:00:00 2001 From: Lily Yan <lily.yan@uwaterloo.ca> Date: Wed, 21 Oct 2020 16:05:27 -0400 Subject: [PATCH] ISTWCMS-4156 Create and pre-load audience taxonomy terms same as those in D7 --- uw_cfg_common.install | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/uw_cfg_common.install b/uw_cfg_common.install index fd0636c9..22a8f5e0 100644 --- a/uw_cfg_common.install +++ b/uw_cfg_common.install @@ -95,4 +95,82 @@ function uw_cfg_common_install() { ], ]; UwPermissions::grantRevoke($permissions_to_process, 'grant'); + + // Add terms to the vocabulary 'uw_vocab_audience'. + $terms = [ + 'Current students' => [ + 'Current undergraduate students', + 'Current graduate students', + ], + 'Future students' => [ + 'Future undergraduate students', + 'Future graduate students', + ], + 'Faculty', + 'Staff', + 'Alumni', + 'Parents', + 'Donors | Friends | Supporters', + 'Employers', + 'International', + 'Media', + ]; + + $weight = 0; + foreach ($terms as $key => $term) { + if (is_array($term)) { + $parent = _uw_cfg_common_create_term($key, 'uw_vocab_audience', $weight, []); + + foreach ($term as $child) { + _uw_cfg_common_create_term($child, 'uw_vocab_audience', $weight, [$parent]); + } + } + else { + _uw_cfg_common_create_term($term, 'uw_vocab_audience', $weight, []); + } + $weight++; + } +} + +/** + * @file + * Contains various helper functions. + */ + +/** + * Helper function to create a taxonomy term programmatically. + * + * @code + * // Create top level term + * $term_id = _nodemaker_term_create('My Term', 'my_vocab', 0, []); + * + * // Create term with parent term with an id of 999 + * $term_id = _nodemaker_term_create('My Term', 'my_vocab', 0, [999]); + * @endcode + * + * @param string $term + * - Term Name. + * @param string $vocabulary + * - System id of the vocabulary term will be added to. + * @param array $parent + * - Array of term ids to be assigned as parent. + * + * @return int|null + * - Returns the term id of the created term on success, null on failure. + */ +function _uw_cfg_common_create_term($taxonomy_name, $vocab_machine_name, $weight, array $parent_tid = []) { + + // Create the taxonomy term. + $new_term = Drupal\taxonomy\Entity\Term::create([ + 'name' => $taxonomy_name, + 'vid' => $vocab_machine_name, + 'parent' => $parent_tid, + 'weight' => $weight, + ]); + + // Save the taxonomy term. + $new_term->save(); + + // Return the taxonomy term id. + return $new_term->id(); } -- GitLab