FeaturesAssignmentMethodBase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Drupal\features;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\Entity\EntityManagerInterface;
  5. use Drupal\Core\Plugin\PluginBase;
  6. /**
  7. * Base class for package assignment methods.
  8. */
  9. abstract class FeaturesAssignmentMethodBase extends PluginBase implements FeaturesAssignmentMethodInterface {
  10. /**
  11. * The features manager.
  12. *
  13. * @var \Drupal\features\FeaturesManagerInterface
  14. */
  15. protected $featuresManager;
  16. /**
  17. * The features assigner.
  18. *
  19. * @var \Drupal\features\FeaturesAssignerInterface
  20. */
  21. protected $assigner;
  22. /**
  23. * The entity manager.
  24. *
  25. * @var \Drupal\Core\Entity\EntityManagerInterface
  26. */
  27. protected $entityManager;
  28. /**
  29. * The configuration factory.
  30. *
  31. * @var \Drupal\Core\Config\ConfigFactoryInterface
  32. */
  33. protected $configFactory;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function setfeaturesManager(FeaturesManagerInterface $features_manager) {
  38. $this->featuresManager = $features_manager;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function setAssigner(FeaturesAssignerInterface $assigner) {
  44. $this->assigner = $assigner;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function setEntityManager(EntityManagerInterface $entity_manager) {
  50. $this->entityManager = $entity_manager;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function setConfigFactory(ConfigFactoryInterface $config_factory) {
  56. $this->configFactory = $config_factory;
  57. }
  58. /**
  59. * Assigns configuration of the types specified in a setting to a package.
  60. *
  61. * @param string $machine_name
  62. * Machine name of the package.
  63. * @param bool $force
  64. * (optional) If TRUE, assign config regardless of restrictions such as it
  65. * being already assigned to a package.
  66. */
  67. protected function assignPackageByConfigTypes($machine_name, $force = FALSE) {
  68. $current_bundle = $this->assigner->getBundle();
  69. $settings = $current_bundle->getAssignmentSettings($this->getPluginId());
  70. $types = $settings['types']['config'];
  71. $config_collection = $this->featuresManager->getConfigCollection();
  72. foreach ($config_collection as $item_name => $item) {
  73. // Don't assign configuration that's provided by an extension.
  74. if (in_array($item->getType(), $types) && !($item->isProviderExcluded())) {
  75. try {
  76. $this->featuresManager->assignConfigPackage($machine_name, [$item_name]);
  77. }
  78. catch (\Exception $exception) {
  79. \Drupal::logger('features')->error($exception->getMessage());
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * Assigns a given subdirectory to configuration of specified types.
  86. *
  87. * @param string $subdirectory
  88. * The subdirectory that designated configuration should be exported to.
  89. */
  90. protected function assignSubdirectoryByConfigTypes($subdirectory) {
  91. $current_bundle = $this->assigner->getBundle();
  92. $settings = $current_bundle->getAssignmentSettings($this->getPluginId());
  93. $types = $settings['types']['config'];
  94. if (!empty($types)) {
  95. $config_collection = $this->featuresManager->getConfigCollection();
  96. foreach ($config_collection as &$item) {
  97. if (in_array($item->getType(), $types)) {
  98. $item->setSubdirectory($subdirectory);
  99. }
  100. }
  101. // Clean up the $item pass by reference.
  102. unset($item);
  103. $this->featuresManager->setConfigCollection($config_collection);
  104. }
  105. }
  106. }