You are here

public function FeaturesManager::assignConfigByPattern in Features 8.3

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

Assigns configuration items with names matching given strings to given packages.

Parameters

array $patterns: Array with string patterns as keys and package machine names as values.

Overrides FeaturesManagerInterface::assignConfigByPattern

File

src/FeaturesManager.php, line 655

Class

FeaturesManager
The FeaturesManager provides helper functions for building packages.

Namespace

Drupal\features

Code

public function assignConfigByPattern(array $patterns) {

  // Regular expressions for items that are likely to generate false
  // positives when assigned by pattern.
  $false_positives = [
    // Blocks with the page title should not be assigned to a 'page' package.
    '/block\\.block\\..*_page_title/',
  ];
  $config_collection = $this
    ->getConfigCollection();

  // Sort by key so that specific package will claim items before general
  // package. E.g., event_registration and registration_event will claim
  // before event.
  uksort($patterns, function ($a, $b) {

    // Count underscores to determine specificity of the package.
    return (int) (substr_count($a, '_') <= substr_count($b, '_'));
  });
  foreach ($patterns as $pattern => $machine_name) {
    if (isset($this->packages[$machine_name])) {
      foreach ($config_collection as $item_name => $item) {

        // Test for and skip false positives.
        foreach ($false_positives as $false_positive) {
          if (preg_match($false_positive, $item_name)) {
            continue 2;
          }
        }
        if (!$item
          ->getPackage() && preg_match('/(\\.|-|_|^)' . $pattern . '(\\.|-|_|$)/', $item
          ->getShortName())) {
          try {
            $this
              ->assignConfigPackage($machine_name, [
              $item_name,
            ]);
          } catch (\Exception $exception) {
            \Drupal::logger('features')
              ->error($exception
              ->getMessage());
          }
        }
      }
    }
  }
}