FeaturesAssignmentMethodBase.php
3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
<?php
/**
* @file
* Contains \Drupal\features\FeaturesAssignmentMethodBase.
*/
namespace Drupal\features;
use Drupal\features\FeaturesManagerInterface;
use Drupal\features\FeaturesAssignerInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Base class for package assignment methods.
*/
abstract class FeaturesAssignmentMethodBase implements FeaturesAssignmentMethodInterface {
use StringTranslationTrait;
/**
* The features manager.
*
* @var \Drupal\features\FeaturesManagerInterface
*/
protected $featuresManager;
/**
* The features assigner.
*
* @var \Drupal\features\FeaturesAssignerInterface
*/
protected $assigner;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* {@inheritdoc}
*/
public function setfeaturesManager(FeaturesManagerInterface $features_manager) {
$this->featuresManager = $features_manager;
}
/**
* {@inheritdoc}
*/
public function setAssigner(FeaturesAssignerInterface $assigner) {
$this->assigner = $assigner;
}
/**
* {@inheritdoc}
*/
public function setEntityManager(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* Assigns configuration of the types specified in a setting to a package.
*
* @param string $method_id
* The ID of an assignment method.
* @param string $machine_name
* Machine name of the package.
* @param bool $force
* (optional) If TRUE, assign config regardless of restrictions such as it
* being already assigned to a package.
*/
protected function assignPackageByConfigTypes($method_id, $machine_name, $force = FALSE) {
$current_bundle = $this->assigner->getBundle();
$settings = $current_bundle->getAssignmentSettings($method_id);
$types = $settings['types']['config'];
$config_collection = $this->featuresManager->getConfigCollection();
foreach ($config_collection as $item_name => $item) {
if (in_array($item['type'], $types) && !isset($item['package'])) {
try {
$this->featuresManager->assignConfigPackage($machine_name, [$item_name]);
}
catch (\Exception $exception) {
\Drupal::logger('features')->error($exception->getMessage());
}
}
}
}
/**
* Assigns a given subdirectory to configuration of specified types.
*
* @param string $method_id
* The ID of an assignment method.
* @param string $subdirectory
* The subdirectory that designated configuration should be exported to.
*/
protected function assignSubdirectoryByConfigTypes($method_id, $subdirectory) {
$current_bundle = $this->assigner->getBundle();
$settings = $current_bundle->getAssignmentSettings($method_id);
$types = $settings['types']['config'];
$config_collection = $this->featuresManager->getConfigCollection();
foreach ($config_collection as &$item) {
if (in_array($item['type'], $types)) {
$item['subdirectory'] = $subdirectory;
}
}
// Clean up the $item pass by reference.
unset($item);
$this->featuresManager->setConfigCollection($config_collection);
}
}