Something went wrong on our end
-
Tyler Struyk authoredTyler Struyk authored
uw_cfg_common.install 4.77 KiB
<?php
/**
* @file
* Install, update and uninstall for Configuration of all common WCMS.
*/
use Drupal\taxonomy\Entity\Term;
use Drupal\uw_cfg_common\UwPermissions\UwPermissions;
/**
* Implements hook_install().
*/
function uw_cfg_common_install() {
$permissions_to_process = [
'Blog' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit tags' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete tags' => [
'Site manager',
],
],
'Catalog' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit audience' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete audience' => [
'Site manager',
],
'Create/edit categories' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete categories' => [
'Site manager',
],
'Create/edit catalogs' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete catalogs' => [
'Site manager',
],
],
'Event' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit tags' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete tags' => [
'Site manager',
],
'Create/edit types' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete types' => [
'Site manager',
],
],
'News' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit tags' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete tags' => [
'Site manager',
],
],
'Sidebar' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
],
'Site footer' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
],
'Special alert' => [
'Administer Display' => [
'Site manager',
],
],
'Web page' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
],
];
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' => 'Faculty',
'Staff' => 'Staff',
'Alumni' => 'Alumni',
'Parents' => 'Parents',
'Donors | Friends | Supporters' => 'Donors | Friends | Supporters',
'Employers' => 'Employers',
'International' => 'International',
'Media' => '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 $taxonomy_name
* - Term Name.
* @param string $vocab_machine_name
* - System id of the vocabulary term will be added to.
* @param string|int $weight
* - Taxonomy term weight.
* @param array $parent_tid
* - 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.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function _uw_cfg_common_create_term($taxonomy_name, $vocab_machine_name, $weight, array $parent_tid = []) {
// Create the taxonomy term.
$new_term = 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();
}