You are here

class ConfigProviderOptional in Configuration Provider 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/ConfigProvider/ConfigProviderOptional.php \Drupal\config_provider\Plugin\ConfigProvider\ConfigProviderOptional

Class for providing configuration from an install directory.

Plugin annotation


@ConfigProvider(
  id = \Drupal\config_provider\Plugin\ConfigProvider\ConfigProviderOptional::ID,
  weight = 10,
  label = @Translation("Optional"),
  description = @Translation("Configuration to be installed when an extension is installed."),
)

Hierarchy

Expanded class hierarchy of ConfigProviderOptional

File

src/Plugin/ConfigProvider/ConfigProviderOptional.php, line 22

Namespace

Drupal\config_provider\Plugin\ConfigProvider
View source
class ConfigProviderOptional extends ConfigProviderBase {

  /**
   * The configuration provider ID.
   */
  const ID = InstallStorage::CONFIG_OPTIONAL_DIRECTORY;

  /**
   * {@inheritdoc}
   */
  public function addConfigToCreate(array &$config_to_create, StorageInterface $storage, $collection, $prefix = '', array $profile_storages = []) {

    // Optional configuration is installed subsequently, so we can't add it
    // here.
  }

  /**
   * {@inheritdoc}
   */
  public function addInstallableConfig(InMemoryStorage $installable_config, array $extensions = []) {

    // This method is adapted from ConfigInstaller::installOptionalConfig().
    // Non-default configuration collections are not supported for
    // config/optional.
    $storage = $this
      ->getExtensionInstallStorage(static::ID);
    $enabled_extensions = $this
      ->getEnabledExtensions();
    $existing_config = $this
      ->getActiveStorages()
      ->listAll();
    $list = $this
      ->listConfig($storage, $extensions);
    $list = array_filter($list, function ($config_name) use ($existing_config) {

      // Only list configuration that:
      // - does not already exist
      // - is a configuration entity (this also excludes config that has an
      //   implicit dependency on modules that are not yet installed)
      return !in_array($config_name, $existing_config) && $this->configManager
        ->getEntityTypeIdByName($config_name);
    });
    $all_config = array_merge($existing_config, $list);

    // Merge in the configuration provided already by previous config
    // providers.
    $all_config = array_merge($all_config, $installable_config
      ->listAll());
    $all_config = array_combine($all_config, $all_config);
    $config_to_create = $storage
      ->readMultiple($list);

    // Sort $config_to_create in the order of the least dependent first.
    $dependency_manager = new ConfigDependencyManager();
    $dependency_manager
      ->setData($config_to_create);
    $config_to_create = array_merge(array_flip($dependency_manager
      ->sortAll()), $config_to_create);
    foreach ($config_to_create as $config_name => $data) {

      // Remove configuration where its dependencies cannot be met.
      $remove = !$this
        ->validateDependencies($config_name, $data, $enabled_extensions, $all_config);

      // If $dependency is defined, remove configuration that does not have a
      // matching dependency.
      if (!$remove && !empty($dependency)) {

        // Create a light weight dependency object to check dependencies.
        $config_entity = new ConfigEntityDependency($config_name, $data);
        $remove = !$config_entity
          ->hasDependency(key($dependency), reset($dependency));
      }
      if ($remove) {

        // Remove from the list of configuration to create.
        unset($config_to_create[$config_name]);

        // Remove from the list of all configuration. This ensures that any
        // configuration that depends on this configuration is also removed.
        unset($all_config[$config_name]);
      }
      else {
        $installable_config
          ->write($config_name, $data);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigProviderBase::$activeStorages protected property The active configuration storages, keyed by collection.
ConfigProviderBase::$configFactory protected property The configuration factory.
ConfigProviderBase::$configManager protected property The configuration manager.
ConfigProviderBase::getActiveStorages protected function Gets the configuration storage that provides the active configuration.
ConfigProviderBase::getDirectory public function Returns the configuration directory. Overrides ConfigProviderInterface::getDirectory
ConfigProviderBase::getEnabledExtensions protected function Gets the list of enabled extensions including both modules and themes.
ConfigProviderBase::getExtensionInstallStorage protected function Gets the storage for a designated configuration provider.
ConfigProviderBase::listConfig protected function Returns a list of all configuration items or those of extensions.
ConfigProviderBase::providesFullConfig public function Indicates whether the configuration items returned by the provider are full as opposed to partials. Overrides ConfigProviderInterface::providesFullConfig
ConfigProviderBase::setActiveStorages public function Injects the active configuration storage. Overrides ConfigProviderInterface::setActiveStorages
ConfigProviderBase::setConfigFactory public function Injects the active configuration storage. Overrides ConfigProviderInterface::setConfigFactory
ConfigProviderBase::setConfigManager public function Injects the active configuration storage. Overrides ConfigProviderInterface::setConfigManager
ConfigProviderBase::validateDependencies protected function Validates an array of config data that contains dependency information.
ConfigProviderOptional::addConfigToCreate public function Adds configuration to be installed. Overrides ConfigProviderInterface::addConfigToCreate
ConfigProviderOptional::addInstallableConfig public function Adds configuration that is available to be installed or updated. Overrides ConfigProviderInterface::addInstallableConfig
ConfigProviderOptional::ID constant The configuration provider ID.
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