class SimpleConfigUpdate

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/SimpleConfigUpdate.php \Drupal\Core\Config\Action\Plugin\ConfigAction\SimpleConfigUpdate

@internal This API is experimental.

Attributes

#[ConfigAction(id: 'simpleConfigUpdate', admin_label: new TranslatableMarkup('Simple configuration update'))]

Hierarchy

Expanded class hierarchy of SimpleConfigUpdate

2 string references to 'SimpleConfigUpdate'
ConfigActionTest::testSimpleConfigUpdate in core/tests/Drupal/KernelTests/Core/Config/Action/ConfigActionTest.php
EntityMethodConfigActionsTest::testSimpleConfigUpdateFailsOnEntities in core/tests/Drupal/KernelTests/Core/Recipe/EntityMethodConfigActionsTest.php
Tests that the simpleConfigUpdate action cannot be used on entities.

File

core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/SimpleConfigUpdate.php, line 20

Namespace

Drupal\Core\Config\Action\Plugin\ConfigAction
View source
final class SimpleConfigUpdate implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {
  public function __construct(private readonly ConfigFactoryInterface $configFactory, private readonly ConfigManagerInterface $configManager) {
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : static {
    return new static($container->get(ConfigFactoryInterface::class), $container->get(ConfigManagerInterface::class));
  }
  
  /**
   * {@inheritdoc}
   */
  public function apply(string $configName, mixed $value) : void {
    if ($this->configManager
      ->getEntityTypeIdByName($configName)) {
      // @todo Make this an exception in https://www.drupal.org/node/3515544.
      @trigger_error('Using the simpleConfigUpdate config action on config entities is deprecated in drupal:11.2.0 and throws an exception in drupal:12.0.0. Use the setProperties action instead. See https://www.drupal.org/node/3515543', E_USER_DEPRECATED);
    }
    $config = $this->configFactory
      ->getEditable($configName);
    if ($config->isNew()) {
      throw new ConfigActionException(sprintf('Config %s does not exist so can not be updated', $configName));
    }
    // Expect $value to be an array whose keys are the config keys to update.
    if (!is_array($value)) {
      throw new ConfigActionException(sprintf('Config %s can not be updated because $value is not an array', $configName));
    }
    foreach ($value as $key => $value) {
      $config->set($key, $value);
    }
    $config->save();
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
SimpleConfigUpdate::apply public function Applies the config action. Overrides ConfigActionPluginInterface::apply
SimpleConfigUpdate::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
SimpleConfigUpdate::__construct public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.