Skip to content
Snippets Groups Projects
Commit c838f96e authored by Lily Yan's avatar Lily Yan
Browse files

ISTWCMS-4156 Create and pre-load audience taxonomy terms same as those in D7

parent 662394ea
No related branches found
No related tags found
1 merge request!3Feature/istwcms 4156 l26yan set up catalogs terms
...@@ -95,4 +95,82 @@ function uw_cfg_common_install() { ...@@ -95,4 +95,82 @@ function uw_cfg_common_install() {
], ],
]; ];
UwPermissions::grantRevoke($permissions_to_process, 'grant'); 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();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment