FeaturesAssignmentExclude.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Drupal\features\Plugin\FeaturesAssignment;
  3. use Drupal\features\FeaturesAssignmentMethodBase;
  4. /**
  5. * Class for excluding configuration from packages.
  6. *
  7. * @Plugin(
  8. * id = "exclude",
  9. * weight = -5,
  10. * name = @Translation("Exclude"),
  11. * description = @Translation("Exclude configuration items from packaging by various methods including by configuration type."),
  12. * config_route_name = "features.assignment_exclude",
  13. * default_settings = {
  14. * "curated" = FALSE,
  15. * "module" = {
  16. * "installed" = FALSE,
  17. * "profile" = FALSE,
  18. * "namespace" = FALSE,
  19. * "namespace_any" = FALSE,
  20. * },
  21. * "types" = { "config" = {} }
  22. * }
  23. * )
  24. */
  25. class FeaturesAssignmentExclude extends FeaturesAssignmentMethodBase {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function assignPackages($force = FALSE) {
  30. $current_bundle = $this->assigner->getBundle();
  31. $settings = $current_bundle->getAssignmentSettings($this->getPluginId());
  32. $config_collection = $this->featuresManager->getConfigCollection();
  33. // Exclude by configuration type.
  34. $exclude_types = $settings['types']['config'];
  35. if (!empty($exclude_types)) {
  36. foreach ($config_collection as $item_name => $item) {
  37. // Don't exclude already-assigned items.
  38. if (empty($item->getPackage()) && in_array($item->getType(), $exclude_types)) {
  39. $item->setExcluded(TRUE);
  40. }
  41. }
  42. }
  43. // Exclude configuration already provided by modules.
  44. $exclude_module = $settings['module'];
  45. if (!empty($exclude_module['installed'])) {
  46. $install_list = $this->featuresManager->getExtensionStorages()->listAll();
  47. // There are two settings that can limit what's included.
  48. // First, we can skip configuration provided by the install profile.
  49. $module_profile = !empty($exclude_module['profile']);
  50. // Second, we can skip configuration provided by namespaced modules.
  51. $module_namespace = !empty($exclude_module['namespace']);
  52. if ($module_profile || $module_namespace) {
  53. $profile_list = [];
  54. $extension_list = [];
  55. // Load the names of any configuration objects provided by the install
  56. // profile.
  57. if ($module_profile) {
  58. $all_modules = $this->featuresManager->getAllModules();
  59. // FeaturesBundleInterface::getProfileName() would return the profile
  60. // for the current bundle, if any. We want the profile that was
  61. // installed.
  62. $profile_name = drupal_get_profile();
  63. if (isset($all_modules[$profile_name])) {
  64. $profile_list = $this->featuresManager->listExtensionConfig($all_modules[$profile_name]);
  65. // If the configuration has been assigned to a feature that's
  66. // present on the file system, don't make an exception for it.
  67. foreach ($all_modules as $name => $extension) {
  68. if ($name != $profile_name && $this->featuresManager->isFeatureModule($extension)) {
  69. $profile_list = array_diff($profile_list, $this->featuresManager->listExtensionConfig($extension));
  70. }
  71. }
  72. }
  73. }
  74. // Load the names of any configuration objects provided by modules
  75. // having the namespace of the current package set.
  76. if ($module_namespace) {
  77. $modules = $this->featuresManager->getFeaturesModules($current_bundle);
  78. foreach ($modules as $extension) {
  79. // Only make exception for non-exported modules
  80. if (!empty($exclude_module['namespace_any']) || !isset($all_modules[$extension->getName()])) {
  81. $extension_list = array_merge($extension_list, $this->featuresManager->listExtensionConfig($extension));
  82. }
  83. }
  84. }
  85. // If any configuration was found, remove it from the list.
  86. $install_list = array_diff($install_list, $profile_list, $extension_list);
  87. }
  88. foreach ($install_list as $item_name) {
  89. if (isset($config_collection[$item_name])) {
  90. // Flag extension-provided configuration, which should not be added
  91. // to regular features but can be added to an install profile.
  92. $config_collection[$item_name]->setProviderExcluded(TRUE);
  93. }
  94. }
  95. }
  96. // Exclude configuration items on a curated list of site-specific
  97. // configuration.
  98. if ($settings['curated']) {
  99. $item_names = [
  100. 'core.extension',
  101. 'field.settings',
  102. 'field_ui.settings',
  103. 'filter.settings',
  104. 'forum.settings',
  105. 'image.settings',
  106. 'node.settings',
  107. 'system.authorize',
  108. 'system.date',
  109. 'system.file',
  110. 'system.diff',
  111. 'system.logging',
  112. 'system.maintenance',
  113. 'system.performance',
  114. 'system.site',
  115. 'update.settings',
  116. ];
  117. foreach ($item_names as $item_name) {
  118. unset($config_collection[$item_name]);
  119. }
  120. // Unset role-related actions that are automatically created by the
  121. // User module.
  122. // @see user_user_role_insert()
  123. $prefixes = [
  124. 'system.action.user_add_role_action.',
  125. 'system.action.user_remove_role_action.',
  126. ];
  127. foreach (array_keys($config_collection) as $item_name) {
  128. foreach ($prefixes as $prefix) {
  129. if (strpos($item_name, $prefix) === 0) {
  130. unset($config_collection[$item_name]);
  131. }
  132. }
  133. }
  134. }
  135. // Register the updated data.
  136. $this->featuresManager->setConfigCollection($config_collection);
  137. }
  138. }