You are here

class FeaturesAssignmentExclude in Features 8.3

Same name and namespace in other branches
  1. 8.4 src/Plugin/FeaturesAssignment/FeaturesAssignmentExclude.php \Drupal\features\Plugin\FeaturesAssignment\FeaturesAssignmentExclude

Class for excluding configuration from packages.

Plugin annotation


@Plugin(
  id = "exclude",
  weight = -5,
  name = @Translation("Exclude"),
  description = @Translation("Exclude configuration items from packaging by various methods including by configuration type. When configuration is excluded, it won't be automatically reassigned to other packages."),
  config_route_name = "features.assignment_exclude",
  default_settings = {
    "curated" = FALSE,
    "module" = {
      "installed" = FALSE,
      "profile" = FALSE,
      "namespace" = FALSE,
      "namespace_any" = FALSE,
    },
    "types" = { "config" = {} }
  }
)

Hierarchy

Expanded class hierarchy of FeaturesAssignmentExclude

File

src/Plugin/FeaturesAssignment/FeaturesAssignmentExclude.php, line 28

Namespace

Drupal\features\Plugin\FeaturesAssignment
View source
class FeaturesAssignmentExclude extends FeaturesAssignmentMethodBase {

  /**
   * {@inheritdoc}
   */
  public function assignPackages($force = FALSE) {
    $current_bundle = $this->assigner
      ->getBundle();
    $settings = $current_bundle
      ->getAssignmentSettings($this
      ->getPluginId());
    $config_collection = $this->featuresManager
      ->getConfigCollection();

    // Exclude by configuration type.
    $exclude_types = $settings['types']['config'];
    if (!empty($exclude_types)) {
      foreach ($config_collection as $item_name => $item) {

        // Don't exclude already-assigned items.
        if (empty($item
          ->getPackage()) && in_array($item
          ->getType(), $exclude_types)) {
          $item
            ->setExcluded(TRUE);
        }
      }
    }

    // Exclude configuration already provided by modules.
    $exclude_module = $settings['module'];
    if (!empty($exclude_module['installed'])) {
      $install_list = $this->featuresManager
        ->getExtensionStorages()
        ->listAll();

      // There are two settings that can limit what's included.
      // First, we can skip configuration provided by the install profile.
      $module_profile = !empty($exclude_module['profile']);

      // Second, we can skip configuration provided by namespaced modules.
      $module_namespace = !empty($exclude_module['namespace']);
      if ($module_profile || $module_namespace) {
        $profile_list = [];
        $extension_list = [];

        // Load the names of any configuration objects provided by the install
        // profile.
        if ($module_profile) {
          $all_modules = $this->featuresManager
            ->getAllModules();

          // FeaturesBundleInterface::getProfileName() would return the profile
          // for the current bundle, if any. We want the profile that was
          // installed.
          $profile_name = \Drupal::installProfile();
          if (isset($all_modules[$profile_name])) {
            $profile_list = $this->featuresManager
              ->listExtensionConfig($all_modules[$profile_name]);

            // If the configuration has been assigned to a feature that's
            // present on the file system, don't make an exception for it.
            foreach ($all_modules as $name => $extension) {
              if ($name != $profile_name && $this->featuresManager
                ->isFeatureModule($extension)) {
                $profile_list = array_diff($profile_list, $this->featuresManager
                  ->listExtensionConfig($extension));
              }
            }
          }
        }

        // Load the names of any configuration objects provided by modules
        // having the namespace of the current package set.
        if ($module_namespace) {
          $modules = $this->featuresManager
            ->getFeaturesModules($current_bundle);
          foreach ($modules as $extension) {

            // Only make exception for uninstalled modules or
            // if namespace_any is set.
            if (!empty($exclude_module['namespace_any']) || !$this->featuresManager
              ->extensionEnabled($extension)) {
              $extension_list = array_merge($extension_list, $this->featuresManager
                ->listExtensionConfig($extension));
            }
          }
        }

        // If any configuration was found, remove it from the list.
        $install_list = array_diff($install_list, $profile_list, $extension_list);
      }
      foreach ($install_list as $item_name) {
        if (isset($config_collection[$item_name])) {

          // Flag extension-provided configuration, which should not be added
          // to regular features but can be added to an install profile.
          $config_collection[$item_name]
            ->setProviderExcluded(TRUE);
        }
      }
    }

    // Exclude configuration items on a curated list of site-specific
    // configuration.
    if ($settings['curated']) {
      $item_names = [
        'core.extension',
        'field.settings',
        'field_ui.settings',
        'filter.settings',
        'forum.settings',
        'image.settings',
        'node.settings',
        'system.authorize',
        'system.date',
        'system.file',
        'system.diff',
        'system.logging',
        'system.maintenance',
        'system.performance',
        'system.site',
        'update.settings',
      ];
      foreach ($item_names as $item_name) {
        unset($config_collection[$item_name]);
      }

      // Unset role-related actions that are automatically created by the
      // User module.
      // @see user_user_role_insert()
      $prefixes = [
        'system.action.user_add_role_action.',
        'system.action.user_remove_role_action.',
      ];
      foreach (array_keys($config_collection) as $item_name) {
        foreach ($prefixes as $prefix) {
          if (strpos($item_name, $prefix) === 0) {
            unset($config_collection[$item_name]);
          }
        }
      }
    }

    // Register the updated data.
    $this->featuresManager
      ->setConfigCollection($config_collection);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FeaturesAssignmentExclude::assignPackages public function Performs package assignment. Overrides FeaturesAssignmentMethodInterface::assignPackages
FeaturesAssignmentMethodBase::$assigner protected property The features assigner.
FeaturesAssignmentMethodBase::$configFactory protected property The configuration factory.
FeaturesAssignmentMethodBase::$entityTypeManager protected property The entity type manager.
FeaturesAssignmentMethodBase::$featuresManager protected property The features manager.
FeaturesAssignmentMethodBase::assignPackageByConfigTypes protected function Assigns configuration of the types specified in a setting to a package.
FeaturesAssignmentMethodBase::assignSubdirectoryByConfigTypes protected function Assigns a given subdirectory to configuration of specified types.
FeaturesAssignmentMethodBase::setAssigner public function Injects the features assigner. Overrides FeaturesAssignmentMethodInterface::setAssigner
FeaturesAssignmentMethodBase::setConfigFactory public function Injects the configuration factory. Overrides FeaturesAssignmentMethodInterface::setConfigFactory
FeaturesAssignmentMethodBase::setEntityTypeManager public function Injects the entity manager. Overrides FeaturesAssignmentMethodInterface::setEntityTypeManager
FeaturesAssignmentMethodBase::setfeaturesManager public function
FeaturesAssignmentMethodInterface::setFeaturesManager public function Injects the features manager.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.