function SetProperties::apply

Applies the config action.

Parameters

string $configName: The name of the config to apply the action to.

mixed $value: The value for the action to use.

Overrides ConfigActionPluginInterface::apply

File

core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/SetProperties.php, line 44

Class

SetProperties
@internal This API is experimental.

Namespace

Drupal\Core\Config\Action\Plugin\ConfigAction

Code

public function apply(string $configName, mixed $values) : void {
  $entity = $this->configManager
    ->loadConfigEntityByName($configName);
  assert($entity instanceof ConfigEntityInterface);
  assert(is_array($values));
  assert(!array_is_list($values));
  // Don't allow the ID or UUID to be changed.
  $entity_keys = $entity->getEntityType()
    ->getKeys();
  $forbidden_keys = array_filter([
    $entity_keys['id'],
    $entity_keys['uuid'],
  ]);
  foreach ($values as $property_name => $value) {
    if (in_array($property_name, $forbidden_keys, TRUE)) {
      throw new ConfigActionException("Entity key '{$property_name}' cannot be changed by the setProperties config action.");
    }
    $parts = explode('.', $property_name);
    $property_value = $entity->get($parts[0]);
    if (count($parts) > 1) {
      if (isset($property_value) && !is_array($property_value)) {
        throw new ConfigActionException('The setProperties config action can only set nested values on arrays.');
      }
      $property_value ??= [];
      NestedArray::setValue($property_value, array_slice($parts, 1), $value);
    }
    else {
      $property_value = $value;
    }
    $entity->set($parts[0], $property_value);
  }
  $entity->save();
}

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