uw_sites_all.install 5.25 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\user\Entity\Role;

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

  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();
  }
28

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

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

  // Remove default "contact" form.
  \Drupal::entityTypeManager()->getStorage('webform')->load('contact')->delete();
Liam Morland's avatar
Liam Morland committed
38 39 40 41 42 43
}

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

  $title = "Web-resources news";
48 49
  $database = \Drupal::database();
  $fid = $database->query('SELECT fid FROM {aggregator_feed} WHERE title = :title', [':title' => $title])->fetchCol();
Liam Morland's avatar
Liam Morland committed
50
  if (!empty($fid)) {
51 52 53 54 55 56 57 58

    $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
59 60
  }
}
61

Liam Morland's avatar
Liam Morland committed
62 63 64 65
/**
 * Implements hook_update_N().
 */

66 67 68 69 70 71
/**
 * Enables OFIS module.
 */
function uw_sites_all_update_8101(&$sandbox) {
  \Drupal::service('module_installer')->install(['uw_ws_ofis']);
}
72 73 74 75 76 77 78

/**
 * Enables UW CKEditor Plugins module.
 */
function uw_sites_all_update_8102(&$sandbox) {
  \Drupal::service('module_installer')->install(['uw_ckeditor_plugins']);
}
79 80 81 82 83 84 85 86

/**
 * 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();
}
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

/**
 * 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();
}
146 147 148 149 150 151 152

/**
 * Enables Quick Node Clone module.
 */
function uw_sites_all_update_8105(&$sandbox) {
  \Drupal::service('module_installer')->install(['quick_node_clone']);
}
153 154 155 156 157 158

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

159 160 161 162 163 164
  // 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();

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  // Modules to uninstall.
  $modules = [
    'config_distro',
    'config_filter',
    'config_merge',
    'config_normalizer',
    'config_provider',
    'config_snapshot',
    'config_sync',
    'config_translation',
    'config_update',
  ];

  \Drupal::service('module_installer')->uninstall($modules);
}