Newer
Older
<?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',
],
],
'Contact' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Site manager',
'Content author',
'Content editor',
],
'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',
],
],
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'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',
],
],
'Profile' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Site manager',
'Content author',
'Content editor',
],
'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' => [
'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, []);
$childweight = 0;
foreach ($term as $child) {
_uw_cfg_common_create_term($child, 'uw_vocab_audience', $childweight, [$parent]);
$childweight++;
}
}
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.
'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();
Eric Bremner
committed
/**
* Update listing blocks items per page when needed.
*
* For listing blocks items per page, when default settings were used,
* change to the original default number of items.
Eric Bremner
committed
*/
function uw_cfg_common_update_8101() {
// Block ids that need to be changed, and what the original default was.
// For some reason, we didn't originally use the same number for everything.
Eric Bremner
committed
$block_ids = [
'views_block:uw_view_blogs-blogs_listing_block' => 5,
'views_block:uw_view_events-events_listing_block' => 10,
'views_block:uw_view_news_items-news_items_listing_block' => 10,
Eric Bremner
committed
];
// Load all the nodes.
Eric Bremner
committed
// Step through each node and set any that were using the default setting
// to the number that was the default before this change.
Eric Bremner
committed
foreach ($nodes as $node) {
// Flag to save the node, have this to save processing
// time if we don't need to save the node after the checks.
$save_node_flag = FALSE;
// Load the layout and sections.
$layout = $node->get('layout_builder__layout');
$sections = $layout->getSections();
// Step through each of the sections.
foreach ($sections as $section) {
// Load the components for the section.
$components = $section->getComponents();
// Step through each of the components.
foreach ($components as $component) {
// If this component is one that needs to be changed,
// then check for setting and change if required.
if (in_array($component->getPluginId(), array_keys($block_ids))) {
Eric Bremner
committed
// Load the config for the block.
$configurations = $component->get('configuration');
// If the config is set to "none", it needs to be changed to
// be set to the number that was the old default value.
if ($configurations['items_per_page'] == 'none') {
Eric Bremner
committed
// Change the config and save the component.
$configurations['items_per_page'] = $block_ids[$component->getPluginId()];
Eric Bremner
committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
$component->setConfiguration($configurations);
// Set the save node flag so that we know to
// save this node as the last step in the loop.
$save_node_flag = TRUE;
}
}
}
// If we need to save the node, then save it.
if ($save_node_flag) {
$node->save();
}
}
// Load all the revisions for the node.
$vids = \Drupal::service('entity_type.manager')->getStorage('node')->revisionIds($node);
// Step through each revision, and check if we have to
// change the settings for the listing blocks.
foreach ($vids as $vid) {
// Flag to see if we have to save the revision.
$save_revision_flag = FALSE;
// Load the revision node.
$revision_node = \Drupal::service('entity_type.manager')->getStorage('node')->loadRevision($vid);
Eric Bremner
committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Comments from here down are the same as above.
$layout = $revision_node->get('layout_builder__layout');
$sections = $layout->getSections();
foreach ($sections as $section) {
$components = $section->getComponents();
foreach ($components as $component) {
if (in_array($component->getPluginId(), $block_ids)) {
$configurations = $component->get('configuration');
if (
$configurations['items_per_page'] == 'none' ||
$configurations['items_per_page'] == '5'
) {
$configurations['items_per_page'] = '3';
$component->setConfiguration($configurations);
$save_revision_flag = TRUE;
}
}
}
}
if ($save_revision_flag) {
$revision_node->save();
}
}
}
}