You are here

function drush_features_add in Features 8.3

Same name and namespace in other branches
  1. 8.4 drush/features.drush8.inc \drush_features_add()
  2. 6 features.drush.inc \drush_features_add()
  3. 7.2 features.drush.inc \drush_features_add()
  4. 7 features.drush.inc \drush_features_add()

Adds a component to a features module.

Parameters

The selected components.:

File

drush/features.drush8.inc, line 362
Features module drush integration.

Code

function drush_features_add() {
  if ($args = func_get_args()) {
    $assigner = _drush_features_options();

    /** @var \Drupal\features\FeaturesManagerInterface $manager */
    $manager = \Drupal::service('features.manager');

    /** @var \Drupal\features\FeaturesGeneratorInterface $generator */
    $generator = \Drupal::service('features_generator');
    $current_bundle = $assigner
      ->getBundle();
    $module = array_shift($args);
    if (empty($args)) {
      return drush_set_error('', 'No components supplied.');
    }
    $components = _drush_features_component_list();
    $options = [
      'exported' => FALSE,
    ];
    $filtered_components = _drush_features_component_filter($components, $args, $options);
    $items = $filtered_components['components'];
    if (empty($items)) {
      return drush_set_error('', 'No components to add.');
    }
    $packages = [
      $module,
    ];

    // If any packages exist, confirm before overwriting.
    if ($existing_packages = $manager
      ->listPackageDirectories($packages)) {
      foreach ($existing_packages as $name => $directory) {
        drush_print(dt("The extension @name already exists at @directory.", [
          '@name' => $name,
          '@directory' => $directory,
        ]));
      }

      // Apparently, format_plural is not always available.
      if (count($existing_packages) == 1) {
        $message = dt('Would you like to overwrite it?');
      }
      else {
        $message = dt('Would you like to overwrite them?');
      }
      if (!drush_confirm($message)) {
        return drush_user_abort();
      }
    }
    else {
      $package = $manager
        ->initPackage($module, NULL, '', 'module', $current_bundle);
      list($full_name, $path) = $manager
        ->getExportInfo($package, $current_bundle);
      drush_print(dt('Will create a new extension @name in @directory', [
        '@name' => $full_name,
        '@directory' => $path,
      ]));
      if (!drush_confirm(dt('Do you really want to continue?'))) {
        drush_die('Aborting.');
      }
    }
    $config = _drush_features_build_config($items);
    $manager
      ->assignConfigPackage($module, $config);

    // Use the write generation method.
    $method_id = FeaturesGenerationWrite::METHOD_ID;
    $result = $generator
      ->generatePackages($method_id, $current_bundle, $packages);
    foreach ($result as $message) {
      $type = $message['success'] ? 'success' : 'error';
      drush_log($message['message'], $message['variables'], $type);
    }
  }
  else {
    return drush_set_error('', 'No feature name given.');
  }
}