You are here

public function FeaturesCommands::add in Features 8.3

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

Add a config item to a feature package.

@command features:add

@todo @option bundle Use a specific bundle namespace.

@aliases fa,fe,features-add

Parameters

array|null $components: Patterns of config to add, see features:components for the format to use.

$feature Feature package to export and add config to.:

Throws

\Drush\Exceptions\UserAbortException

\Exception

File

src/Commands/FeaturesCommands.php, line 444

Class

FeaturesCommands
Drush commands for Features.

Namespace

Drupal\features\Commands

Code

public function add($components = NULL, $options = self::OPTIONS_ADD) {
  if ($components) {
    $assigner = $this
      ->featuresOptions($options);
    $manager = $this->manager;
    $generator = $this->generator;
    $current_bundle = $assigner
      ->getBundle();
    $module = array_shift($args);
    if (empty($args)) {
      throw new \Exception('No components supplied.');
    }
    $components = $this
      ->componentList();
    $options = [
      'exported' => FALSE,
    ];
    $filtered_components = $this
      ->componentFilter($components, $args, $options);
    $items = $filtered_components['components'];
    if (empty($items)) {
      throw new \Exception('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) {
        $this
          ->output()
          ->writeln(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 (!$this
        ->io()
        ->confirm($message)) {
        throw new UserAbortException();
      }
    }
    else {
      $package = $manager
        ->initPackage($module, NULL, '', 'module', $current_bundle);
      list($full_name, $path) = $manager
        ->getExportInfo($package, $current_bundle);
      $this
        ->output()
        ->writeln(dt('Will create a new extension @name in @directory', [
        '@name' => $full_name,
        '@directory' => $path,
      ]));
      if (!$this
        ->io()
        ->confirm(dt('Do you really want to continue?'))) {
        throw new UserAbortException();
      }
    }
    $config = $this
      ->buildConfig($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) {
      $method = $message['success'] ? 'success' : 'error';
      $this
        ->logger()
        ->{$method}(dt($message['message'], $message['variables']));
    }
  }
  else {
    throw new \Exception('No feature name given.');
  }
}