domain_alpha.install 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @file
  4. * Pre-release update hooks for Domain modules.
  5. */
  6. /**
  7. * Implements hook_install().
  8. *
  9. * We accidentally released the 8001 update, which will not run on a fresh install. So
  10. * lets for it to run now.
  11. */
  12. function domain_alpha_install() {
  13. $sandbox = array();
  14. domain_alpha_update_8001($sandbox);
  15. }
  16. /**
  17. * Update domain id to new value.
  18. */
  19. function domain_alpha_update_8001(&$sandbox) {
  20. // Set the node_access rebuild flag. Only update if an id changes.
  21. $rebuild = FALSE;
  22. // Load all domains and update the id, if necessary.
  23. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  24. foreach ($domains as $domain) {
  25. /** @var $domain \Drupal\domain\Entity\Domain */
  26. // Existing id.
  27. $id = $domain->getDomainId();
  28. // New id.
  29. $domain->createDomainId();
  30. $new_id = $domain->getDomainId();
  31. // Check to see if this update is needed.
  32. if ($id != $new_id) {
  33. $domain->save();
  34. $rebuild = TRUE;
  35. }
  36. }
  37. if ($rebuild) {
  38. // Trigger permissions rebuild action.
  39. node_access_needs_rebuild(TRUE);
  40. }
  41. }
  42. /**
  43. * Provide a new update for 8001, for users who never ran 8001.
  44. *
  45. * See https://github.com/agentrickard/domain/issues/310.
  46. */
  47. function domain_alpha_update_8002(&$sandbox) {
  48. domain_alpha_update_8001($sandbox);
  49. }
  50. /**
  51. * Set the Domain Admin field to use the proper plugin.
  52. */
  53. function domain_alpha_update_8003(&$sandbox) {
  54. $id = 'user.user.field_domain_admin';
  55. $storage = \Drupal::entityTypeManager()->getStorage('field_config');
  56. if ($field = $storage->load($id)) {
  57. $new_field = $field->toArray();
  58. if ($new_field['settings']['handler'] != 'domain:domain') {
  59. $new_field['settings']['handler'] = 'domain:domain';
  60. $field_config = $storage->create($new_field);
  61. $field_config->original = $field;
  62. $field_config->enforceIsNew(FALSE);
  63. $field_config->save();
  64. }
  65. }
  66. }
  67. /**
  68. * Make all affiliates default value configurable.
  69. */
  70. function domain_alpha_update_8004() {
  71. $config = \Drupal::configFactory();
  72. $field_name = '.' . DOMAIN_ACCESS_ALL_FIELD;
  73. // Iterate over all node & user fields.
  74. foreach (['node', 'user'] as $type) {
  75. foreach ($config->listAll('field.field.' . $type . '.') as $field_id) {
  76. if (substr($field_id, -strlen($field_name)) === $field_name) {
  77. $field = $config->getEditable($field_id);
  78. if ($field->get('default_value_callback') === 'Drupal\domain_access\DomainAccessManager::getDefaultAllValue') {
  79. $field
  80. ->clear('default_value_callback')
  81. ->save(TRUE);
  82. }
  83. }
  84. }
  85. }
  86. }