class SetProperties
@internal This API is experimental.
Hierarchy
- class \Drupal\Core\Config\Action\Plugin\ConfigAction\SetProperties implements \Drupal\Core\Config\Action\ConfigActionPluginInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface
Expanded class hierarchy of SetProperties
2 string references to 'SetProperties'
- EntityMethodConfigActionsTest::testSetNestedProperty in core/
tests/ Drupal/ KernelTests/ Core/ Recipe/ EntityMethodConfigActionsTest.php - Test setting a nested property on a config entity.
- EntityMethodConfigActionsTest::testSetPropertiesWillNotChangeEntityKeys in core/
tests/ Drupal/ KernelTests/ Core/ Recipe/ EntityMethodConfigActionsTest.php - Tests that the setProperties action refuses to modify entity IDs or UUIDs.
File
-
core/
lib/ Drupal/ Core/ Config/ Action/ Plugin/ ConfigAction/ SetProperties.php, line 21
Namespace
Drupal\Core\Config\Action\Plugin\ConfigActionView source
final class SetProperties implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {
public function __construct(ConfigManagerInterface $configManager) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container->get(ConfigManagerInterface::class));
}
/**
* {@inheritdoc}
*/
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();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
SetProperties::apply | public | function | Overrides ConfigActionPluginInterface::apply | |
SetProperties::create | public static | function | Overrides ContainerFactoryPluginInterface::create | |
SetProperties::__construct | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.