5 * Contains Drupal\Console\Command\Shared\FeatureTrait.
8 namespace Drupal\Console\Command\Shared;
10 use Drupal\Console\Core\Style\DrupalStyle;
11 use Drupal\features\FeaturesManagerInterface;
12 use Drupal\features\ConfigurationItem;
13 use Drupal\features\Plugin\FeaturesGeneration\FeaturesGenerationWrite;
14 use Drupal\config_update\ConfigRevertInterface;
19 * @package Drupal\Console\Command
23 public function packageQuestion(DrupalStyle $io)
25 $packages = $this->getPackagesByBundle($bundle);
27 if (empty($packages)) {
29 $this->trans('commands.features.message.no-packages')
33 $package = $io->choiceNoList(
34 $this->trans('commands.features.import.questions.packages'),
43 * @param bool $bundle_name
45 * @return \Drupal\features\FeaturesAssignerInterface
47 protected function getAssigner($bundle_name)
50 * @var \Drupal\features\FeaturesAssignerInterface $assigner
52 $assigner = \Drupal::service('features_assigner');
53 if (!empty($bundle_name)) {
54 $bundle = $assigner->applyBundle($bundle_name);
56 if ($bundle->getMachineName() != $bundle_name) {
59 // return configuration for default bundle
61 $assigner->assignConfigPackages();
67 * Get a list of features.
73 protected function getFeatureList($bundle)
76 $manager = $this->getFeatureManager();
77 $modules = $this->getPackagesByBundle($bundle);
79 foreach ($modules as $module_name) {
80 $feature = $manager->loadPackage($module_name, true);
81 $overrides = $manager->detectOverrides($feature);
83 $state = $feature->getState();
85 if (!empty($overrides) && ($feature->getStatus() != FeaturesManagerInterface::STATUS_NO_EXPORT)) {
86 $state = FeaturesManagerInterface::STATE_OVERRIDDEN;
89 if ($feature->getStatus() != FeaturesManagerInterface::STATUS_NO_EXPORT) {
90 $features[$feature->getMachineName()] = [
91 'name' => $feature->getName(),
92 'machine_name' => $feature->getMachineName(),
93 'bundle_name' => $feature->getBundle(),
94 'status' => $manager->statusLabel($feature->getStatus()),
95 'state' => ($state != FeaturesManagerInterface::STATE_DEFAULT) ? $manager->stateLabel($state) : '',
104 protected function importFeature(DrupalStyle $io, $packages)
106 $manager = $this->getFeatureManager();
108 $modules = (is_array($packages)) ? $packages : [$packages];
110 foreach ($modules as $module_name) {
111 $package = $manager->loadPackage($module_name, true);
113 if (empty($package)) {
116 $this->trans('commands.features.import.messages.not-available'),
123 if ($package->getStatus() != FeaturesManagerInterface::STATUS_INSTALLED) {
126 $this->trans('commands.features.import.messages.uninstall'),
133 $overrides = $manager->detectOverrides($package);
134 $missing = $manager->reorderMissing($manager->detectMissing($package));
136 if (!empty($overrides) || !empty($missing) && ($package->getStatus() == FeaturesManagerInterface::STATUS_INSTALLED)) {
137 $overridden[] = array_merge($missing, $overrides);
141 // Process only missing or overriden features
142 $components = $overridden;
144 if (empty($components)) {
147 $this->trans('commands.features.import.messages.nothing'),
154 $this->import($io, $components);
158 public function import($io, $components)
160 $manager = $this->getFeatureManager();
162 * @var \Drupal\config_update\ConfigRevertInterface $config_revert
164 $config_revert = \Drupal::service('features.config_update');
166 $config = $manager->getConfigCollection();
168 foreach ($components as $component) {
169 foreach ($component as $feature) {
170 if (!isset($config[$feature])) {
172 //Import missing component.
173 $item = $manager->getConfigType($feature);
174 $type = ConfigurationItem::fromConfigStringToConfigType($item['type']);
175 $config_revert->import($type, $item['name_short']);
178 $this->trans('commands.features.import.messages.importing'),
183 // Revert existing component.
184 $item = $config[$feature];
185 $type = ConfigurationItem::fromConfigStringToConfigType($item->getType());
186 $config_revert->revert($type, $item->getShortName());
189 $this->trans('commands.features.import.messages.reverting'),
199 public function getPackagesByBundle($bundle)
201 $manager = $this->getFeatureManager();
202 $assigner = $this->getAssigner($bundle);
203 $current_bundle = $assigner->getBundle();
205 // List all packages availables
206 if ($current_bundle->getMachineName() == 'default') {
207 $current_bundle = null;
210 $packages = array_keys($manager->getFeaturesModules($current_bundle));
215 public function getFeatureManager()
217 return \Drupal::service('features.manager');