uw_sites_all.install 8.01 KB
Newer Older
Liam Morland's avatar
Liam Morland committed
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Install, update and uninstall for UW Sites all.
 */

8 9
use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
10 11
use Drupal\user\Entity\Role;

Liam Morland's avatar
Liam Morland committed
12 13 14 15 16 17
/**
 * Implements hook_install().
 */
function uw_sites_all_install() {
  // Add web resources news feed.
  $title = 'Web-resources news';
18 19
  $database = \Drupal::database();
  $fid = $database->query('SELECT fid FROM {aggregator_feed} WHERE title = :title', [':title' => $title])->fetchCol();
Liam Morland's avatar
Liam Morland committed
20 21 22 23 24 25 26 27 28 29

  if (empty($fid)) {
    $feed = \Drupal::entityTypeManager()->getStorage('aggregator_feed')->create([
      'uuid' => \Drupal::service('uuid')->generate(),
      'title' => $title,
      'url' => 'https://uwaterloo.ca/web-resources/news/news.xml',
      'created' => time(),
    ]);
    $feed->save();
  }
30

Liam Morland's avatar
Liam Morland committed
31
  // Make sure the theme is installed.
32
  \Drupal::service('theme_installer')->install(['uw_theme_admin']);
33

Liam Morland's avatar
Liam Morland committed
34
  // Set it as the admin theme.
35 36
  $config = \Drupal::service('config.factory')->getEditable('system.theme');
  $config->set('admin', 'uw_theme_admin')->save();
37 38 39

  // Remove default "contact" form.
  \Drupal::entityTypeManager()->getStorage('webform')->load('contact')->delete();
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

  // Get the menu items.
  $menu_items = _uw_sites_all_get_menu_items();

  // Step through each menu item and create a menu link.
  foreach ($menu_items as $menu_item) {

    // Create the URL from the route name.
    $url = URL::fromRoute($menu_item['route_name'])->toString();

    // Create the actual menu link.
    MenuLinkContent::create([
      'title' => $menu_item['title'],
      'link' => ['uri' => 'internal:' . $url],
      'menu_name' => $menu_item['menu_name'],
      'weight' => $menu_item['weight'],
56
      'enabled' => $menu_item['enabled'],
57 58 59 60 61 62 63 64 65 66 67 68 69
    ])->save();
  }
}

/**
 * Helper function to return the UW default menu links.
 *
 * @return array[]
 *   Array of UW specific menu links.
 */
function _uw_sites_all_get_menu_items(): array {

  return [
70 71 72 73 74 75 76 77 78 79 80 81 82 83
    'Contacts' => [
      'title' => 'Contacts',
      'menu_name' => 'main',
      'route_name' => 'view.uw_view_contacts.contact_page',
      'weight' => 40,
      'enabled' => TRUE,
    ],
    'Profiles' => [
      'title' => 'Profiles',
      'menu_name' => 'main',
      'route_name' => 'view.uw_view_profiles.profile_page',
      'weight' => 45,
      'enabled' => TRUE,
    ],
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    'Blog' => [
      'title' => 'Blog',
      'menu_name' => 'main',
      'route_name' => 'view.uw_view_blogs.blog_page',
      'weight' => 50,
      'enabled' => TRUE,
    ],
    'Events' => [
      'title' => 'Events',
      'menu_name' => 'main',
      'route_name' => 'view.uw_view_events.event_page',
      'weight' => 55,
      'enabled' => TRUE,
    ],
    'News' => [
      'title' => 'News',
      'menu_name' => 'main',
      'route_name' => 'view.uw_view_news_items.news_page',
      'weight' => 60,
      'enabled' => TRUE,
    ],
    'Catalogs' => [
      'title' => 'Catalogs',
      'menu_name' => 'main',
108
      'route_name' => 'view.uw_view_catalogs.catalogs_page',
109
      'weight' => 65,
110 111 112
      'enabled' => FALSE,
    ],
  ];
Liam Morland's avatar
Liam Morland committed
113 114 115 116 117 118
}

/**
 * Implements hook_uninstall().
 */
function uw_sites_all_uninstall() {
Liam Morland's avatar
Liam Morland committed
119
  // phpcs:disable DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable
Liam Morland's avatar
Liam Morland committed
120 121 122
  $transaction = \Drupal::database()->startTransaction();

  $title = "Web-resources news";
123 124
  $database = \Drupal::database();
  $fid = $database->query('SELECT fid FROM {aggregator_feed} WHERE title = :title', [':title' => $title])->fetchCol();
Liam Morland's avatar
Liam Morland committed
125
  if (!empty($fid)) {
126 127 128 129 130 131 132 133

    $query = $database->delete('aggregator_feed');
    $query->condition('fid', $fid);
    $query->execute();

    $query = $database->delete('aggregator_item');
    $query->condition('fid', $fid);
    $query->execute();
Liam Morland's avatar
Liam Morland committed
134 135
  }
}
136

Liam Morland's avatar
Liam Morland committed
137 138 139 140
/**
 * Implements hook_update_N().
 */

141 142 143 144 145 146
/**
 * Enables OFIS module.
 */
function uw_sites_all_update_8101(&$sandbox) {
  \Drupal::service('module_installer')->install(['uw_ws_ofis']);
}
147 148 149 150 151 152 153

/**
 * Enables UW CKEditor Plugins module.
 */
function uw_sites_all_update_8102(&$sandbox) {
  \Drupal::service('module_installer')->install(['uw_ckeditor_plugins']);
}
154 155 156 157 158 159 160 161

/**
 * Protect all Webforms with Honeypot.
 */
function uw_sites_all_update_8103() {
  $config = \Drupal::service('config.factory')->getEditable('webform.settings');
  $config->set('third_party_settings.honeypot.honeypot', TRUE)->save();
}
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

/**
 * Enable Override Node Options module and assigns permissions to roles.
 */
function uw_sites_all_update_8104() {
  \Drupal::service('module_installer')->install(['override_node_options']);

  // List of permissions needed to be applied to roles.
  $permissions = [
    'enter uw_ct_blog revision log entry',
    'enter uw_ct_catalog_item revision log entry',
    'enter uw_ct_contact revision log entry',
    'enter uw_ct_event revision log entry',
    'enter uw_ct_news_item revision log entry',
    'enter uw_ct_profile revision log entry',
    'enter uw_ct_web_page revision log entry',
    'override uw_ct_blog promote to front page option',
    'override uw_ct_blog published option',
    'override uw_ct_blog revision option',
    'override uw_ct_blog sticky option',
    'override uw_ct_catalog_item published option',
    'override uw_ct_catalog_item revision option',
    'override uw_ct_contact published option',
    'override uw_ct_contact revision option',
    'override uw_ct_event promote to front page option',
    'override uw_ct_event published option',
    'override uw_ct_event revision option',
    'override uw_ct_event sticky option',
    'override uw_ct_news_item promote to front page option',
    'override uw_ct_news_item published option',
    'override uw_ct_news_item revision option',
    'override uw_ct_news_item sticky option',
    'override uw_ct_profile published option',
    'override uw_ct_profile revision option',
    'override uw_ct_web_page published option',
    'override uw_ct_web_page revision option',
  ];

  // Roles that require updated permissions.
  $role_ids = [
    'uw_role_site_manager',
    'uw_role_content_editor',
    'uw_role_content_author',
  ];

  $roles = Role::loadMultiple($role_ids);

  // Grant all all roles.
  foreach ($permissions as $permission) {
    $roles['uw_role_site_manager']->grantPermission($permission);
    $roles['uw_role_content_editor']->grantPermission($permission);
    $roles['uw_role_content_author']->grantPermission($permission);
  }

  // Save role with new permissions.
  $roles['uw_role_site_manager']->save();
  $roles['uw_role_content_editor']->save();
  $roles['uw_role_content_author']->save();
}
221 222 223 224 225 226 227

/**
 * Enables Quick Node Clone module.
 */
function uw_sites_all_update_8105(&$sandbox) {
  \Drupal::service('module_installer')->install(['quick_node_clone']);
}
228 229 230 231 232 233

/**
 * Uninstall all modules to do with config sync distro.
 */
function uw_sites_all_update_8106() {

234 235 236 237 238 239
  // Delete all references of config_snapshot from database,
  // so that features does not show as overridden.
  $query = \Drupal::database()->delete('config');
  $query->condition('name', '%config_snapshot%', 'LIKE');
  $query->execute();

240 241 242 243 244 245 246 247 248 249 250 251 252 253
  // Modules to uninstall.
  $modules = [
    'config_distro',
    'config_filter',
    'config_merge',
    'config_normalizer',
    'config_provider',
    'config_snapshot',
    'config_sync',
    'config_translation',
  ];

  \Drupal::service('module_installer')->uninstall($modules);
}
254 255 256 257 258 259 260 261 262 263 264 265 266

/**
 * Uninstall structure sync.
 */
function uw_sites_all_update_8107() {

  // Modules to uninstall.
  $modules = [
    'structure_sync',
  ];

  \Drupal::service('module_installer')->uninstall($modules);
}
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

/**
 * Recreate main menu items.
 */
function uw_sites_all_update_8108(&$sandbox) {
  // Get the menu items.
  $menu_items = _uw_sites_all_get_menu_items();

  // Step through each menu item and create a menu link.
  foreach ($menu_items as $menu_item) {

    // Create the URL from the route name.
    $url = URL::fromRoute($menu_item['route_name'])->toString();

    // Create the actual menu link.
    MenuLinkContent::create([
      'title' => $menu_item['title'],
      'link' => ['uri' => 'internal:' . $url],
      'menu_name' => $menu_item['menu_name'],
      'weight' => $menu_item['weight'],
      'enabled' => $menu_item['enabled'],
    ])->save();
  }

}