You are here

public function FeaturesCommands::import in Features 8.4

Same name and namespace in other branches
  1. 8.3 src/Commands/FeaturesCommands.php \Drupal\features\Commands\FeaturesCommands::import()

Import a module config into your site.

@command features:import

@option force Force import even if config is not overridden. @option bundle Use a specific bundle namespace.

@usage drush features-import foo:node.type.page foo:taxonomy.vocabulary.tags bar Import node and taxonomy config of feature "foo". Import all config of feature "bar".

@aliases fim,fr,features-import

Parameters

string $feature: A comma-delimited list of features or feature:component pairs to import.

Throws

\Exception

1 call to FeaturesCommands::import()
FeaturesCommands::importAll in src/Commands/FeaturesCommands.php
Import module config from all installed features.

File

src/Commands/FeaturesCommands.php, line 708

Class

FeaturesCommands
Drush commands for Features.

Namespace

Drupal\features\Commands

Code

public function import($feature, $options = self::OPTIONS_IMPORT) {
  $this
    ->featuresOptions($options);
  $features = StringUtils::csvToArray($feature);
  if (empty($features)) {
    drush_invoke_process('@self', 'features:list:packages', [], $options);
    return;
  }

  // Determine if revert should be forced.
  $force = $this
    ->getOption($options, 'force');

  // Determine if -y was supplied. If so, we can filter out needless output
  // from this command.
  $skip_confirmation = $options['yes'];
  $manager = $this->manager;

  // Parse list of arguments.
  $modules = [];
  foreach ($features as $featureString) {
    list($module, $component) = explode(':', $featureString);

    // We cannot use just a component name without its module.
    if (empty($module)) {
      continue;
    }

    // We received just a feature name, meaning we need all of its components.
    if (empty($component)) {
      $modules[$module] = TRUE;
      continue;
    }
    if (empty($modules[$module])) {
      $modules[$module] = [];
    }
    if ($modules[$module] !== TRUE) {
      $modules[$module][] = $component;
    }
  }

  // Process modules.
  foreach ($modules as $module => $componentsNeeded) {

    // Reset the arguments on each loop pass.
    $dt_args = [
      '@module' => $module,
    ];

    /** @var \Drupal\features\Package $feature */
    $feature = $manager
      ->loadPackage($module, TRUE);
    if (empty($feature)) {
      throw new DomainException(dt('No such feature is available: @module', $dt_args));
    }
    if ($feature
      ->getStatus() != FeaturesManagerInterface::STATUS_INSTALLED) {
      throw new DomainException(dt('No such feature is installed: @module', $dt_args));
    }

    // Forcefully revert all components of a feature.
    if ($force) {
      $components = $feature
        ->getConfigOrig();
    }
    else {
      $overrides = $manager
        ->detectOverrides($feature);
      $missing = $manager
        ->reorderMissing($manager
        ->detectMissing($feature));

      // Be sure to import missing components first.
      $components = array_merge($missing, $overrides);
    }
    if (!empty($componentsNeeded) && is_array($componentsNeeded)) {
      $components = array_intersect($components, $componentsNeeded);
    }
    if (empty($components)) {
      $this
        ->logger()
        ->info(dt('Current state already matches active config, aborting.'));
      continue;
    }

    // Determine which config the user wants to import/revert.
    $configToCreate = [];
    foreach ($components as $component) {
      $dt_args['@component'] = $component;
      $confirmation_message = 'Do you really want to import @module : @component?';
      if ($skip_confirmation || $this
        ->io()
        ->confirm(dt($confirmation_message, $dt_args))) {
        $configToCreate[$component] = '';
      }
    }

    // Perform the import/revert.
    $importedConfig = $manager
      ->createConfiguration($configToCreate);

    // List the results.
    foreach ($components as $component) {
      $dt_args['@component'] = $component;
      if (isset($importedConfig['new'][$component])) {
        $this
          ->logger()
          ->info(dt('Imported @module : @component.', $dt_args));
      }
      elseif (isset($importedConfig['updated'][$component])) {
        $this
          ->logger()
          ->info(dt('Reverted @module : @component.', $dt_args));
      }
      elseif (!isset($configToCreate[$component])) {
        $this
          ->logger()
          ->info(dt('Skipping @module : @component.', $dt_args));
      }
      else {
        $this
          ->logger()
          ->error(dt('Error importing @module : @component.', $dt_args));
      }
    }
  }
}