You are here

public function FeaturesManager::assignConfigPackage in Features 8.3

Same name and namespace in other branches
  1. 8.4 src/FeaturesManager.php \Drupal\features\FeaturesManager::assignConfigPackage()

Assigns a set of configuration items to a given package or profile.

Parameters

string $package_name: Machine name of a package or the profile.

string[] $item_names: Configuration item names.

bool $force: (optional) If TRUE, assign config regardless of restrictions such as it being already assigned to a package.

Throws

Exception

Overrides FeaturesManagerInterface::assignConfigPackage

2 calls to FeaturesManager::assignConfigPackage()
FeaturesManager::assignConfigByPattern in src/FeaturesManager.php
Assigns configuration items with names matching given strings to given packages.
FeaturesManager::assignConfigDependents in src/FeaturesManager.php
For given configuration items, assigns any dependent configuration to the same package.

File

src/FeaturesManager.php, line 597

Class

FeaturesManager
The FeaturesManager provides helper functions for building packages.

Namespace

Drupal\features

Code

public function assignConfigPackage($package_name, array $item_names, $force = FALSE) {
  $config_collection = $this
    ->getConfigCollection();
  $module_list = $this->moduleHandler
    ->getModuleList();
  $current_bundle = $this->assigner
    ->getBundle();
  $packages =& $this->packages;
  if (isset($packages[$package_name])) {
    $package =& $packages[$package_name];
  }
  elseif (isset($current_bundle) && isset($this->packages[$current_bundle
    ->getFullName($package_name)])) {
    $package =& $this->packages[$current_bundle
      ->getFullName($package_name)];
  }
  else {
    throw new \Exception($this
      ->t('Failed to package @package_name. Package not found.', [
      '@package_name' => $package_name,
    ]));
  }
  foreach ($item_names as $item_name) {
    if (isset($config_collection[$item_name])) {

      // Add to the package if:
      // - force is set or
      //   - the item hasn't already been assigned elsewhere, and
      //   - the package hasn't been excluded.
      // - and the item isn't already in the package.
      $item =& $config_collection[$item_name];
      $already_assigned = !empty($item
        ->getPackage());

      // If this is the profile package, we can reassign extension-provided
      // configuration.
      $package_bundle = $this
        ->getAssigner()
        ->getBundle($package
        ->getBundle());
      $is_profile_package = isset($package_bundle) ? $package_bundle
        ->isProfilePackage($package_name) : FALSE;

      // An item is assignable if:
      // - it is not provider excluded or this is the profile package, and
      // - it is not flagged as excluded.
      $assignable = (!$item
        ->isProviderExcluded() || $is_profile_package) && !$item
        ->isExcluded();

      // An item is assignable if it was provided by the current package.
      $assignable = $assignable || $item
        ->getProvider() == $package
        ->getMachineName();
      $excluded_from_package = in_array($package_name, $item
        ->getPackageExcluded());
      $already_in_package = in_array($item_name, $package
        ->getConfig());
      if (($force || !$already_assigned && $assignable && !$excluded_from_package) && !$already_in_package) {

        // Add the item to the package's config array.
        $package
          ->appendConfig($item_name);

        // Mark the item as already assigned.
        $item
          ->setPackage($package_name);
        $module_dependencies = $this
          ->getConfigDependency($item, $module_list);
        $package
          ->setDependencies($this
          ->mergeUniqueItems($package
          ->getDependencies(), $module_dependencies));
      }

      // Return memory.
      unset($item);
    }
  }
  $this
    ->setConfigCollection($config_collection);
}