5 * Contains Drupal\Console\Command\Shared\FeatureTrait.
8 namespace Drupal\Console\Command\Shared;
10 use Drupal\features\FeaturesManagerInterface;
11 use Drupal\features\ConfigurationItem;
12 use Drupal\features\Plugin\FeaturesGeneration\FeaturesGenerationWrite;
13 use Drupal\config_update\ConfigRevertInterface;
18 * @package Drupal\Console\Command
22 public function packageQuestion($bundle)
24 $packages = $this->getPackagesByBundle($bundle);
26 if (empty($packages)) {
28 $this->trans('commands.features.message.no-packages')
32 $package = $this->getIo()->choiceNoList(
33 $this->trans('commands.features.import.questions.packages'),
42 * @param bool $bundle_name
44 * @return \Drupal\features\FeaturesAssignerInterface
46 protected function getAssigner($bundle_name)
49 * @var \Drupal\features\FeaturesAssignerInterface $assigner
51 $assigner = \Drupal::service('features_assigner');
52 if (!empty($bundle_name)) {
53 $bundle = $assigner->applyBundle($bundle_name);
55 if ($bundle->getMachineName() != $bundle_name) {
58 // return configuration for default bundle
60 $assigner->assignConfigPackages();
66 * Get a list of features.
72 protected function getFeatureList($bundle)
75 $manager = $this->getFeatureManager();
76 $modules = $this->getPackagesByBundle($bundle);
78 foreach ($modules as $module_name) {
79 $feature = $manager->loadPackage($module_name, true);
80 $overrides = $manager->detectOverrides($feature);
82 $state = $feature->getState();
84 if (!empty($overrides) && ($feature->getStatus() != FeaturesManagerInterface::STATUS_NO_EXPORT)) {
85 $state = FeaturesManagerInterface::STATE_OVERRIDDEN;
88 if ($feature->getStatus() != FeaturesManagerInterface::STATUS_NO_EXPORT) {
89 $features[$feature->getMachineName()] = [
90 'name' => $feature->getName(),
91 'machine_name' => $feature->getMachineName(),
92 'bundle_name' => $feature->getBundle(),
93 'status' => $manager->statusLabel($feature->getStatus()),
94 'state' => ($state != FeaturesManagerInterface::STATE_DEFAULT) ? $manager->stateLabel($state) : '',
103 protected function importFeature($packages)
105 $manager = $this->getFeatureManager();
107 $modules = (is_array($packages)) ? $packages : [$packages];
109 foreach ($modules as $module_name) {
110 $package = $manager->loadPackage($module_name, true);
112 if (empty($package)) {
113 $this->getIo()->warning(
115 $this->trans('commands.features.import.messages.not-available'),
122 if ($package->getStatus() != FeaturesManagerInterface::STATUS_INSTALLED) {
123 $this->getIo()->warning(
125 $this->trans('commands.features.import.messages.uninstall'),
132 $overrides = $manager->detectOverrides($package);
133 $missing = $manager->reorderMissing($manager->detectMissing($package));
135 if (!empty($overrides) || !empty($missing) && ($package->getStatus() == FeaturesManagerInterface::STATUS_INSTALLED)) {
136 $overridden[] = array_merge($missing, $overrides);
140 // Process only missing or overridden features
141 $components = $overridden;
143 if (empty($components)) {
144 $this->getIo()->warning(
146 $this->trans('commands.features.import.messages.nothing')
152 $this->import($this->getIo(), $components);
156 public function import($components)
158 $manager = $this->getFeatureManager();
160 * @var \Drupal\config_update\ConfigRevertInterface $config_revert
162 $config_revert = \Drupal::service('features.config_update');
164 $config = $manager->getConfigCollection();
166 foreach ($components as $component) {
167 foreach ($component as $feature) {
168 if (!isset($config[$feature])) {
169 //Import missing component.
170 $item = $manager->getConfigType($feature);
171 $type = ConfigurationItem::fromConfigStringToConfigType($item['type']);
172 $config_revert->import($type, $item['name_short']);
173 $this->getIo()->info(
175 $this->trans('commands.features.import.messages.importing'),
180 // Revert existing component.
181 $item = $config[$feature];
182 $type = ConfigurationItem::fromConfigStringToConfigType($item->getType());
183 $config_revert->revert($type, $item->getShortName());
184 $this->getIo()->info(
186 $this->trans('commands.features.import.messages.reverting'),
196 public function getPackagesByBundle($bundle)
198 $manager = $this->getFeatureManager();
199 $assigner = $this->getAssigner($bundle);
200 $current_bundle = $assigner->getBundle();
202 // List all packages availables
203 if ($current_bundle->getMachineName() == 'default') {
204 $current_bundle = null;
207 $packages = array_keys($manager->getFeaturesModules($current_bundle));
212 public function getFeatureManager()
214 return \Drupal::service('features.manager');