- Issue created by @JFKiwad
- 🇵🇱Poland shumer Wroclaw
Hello,
you almost did it, the context in Config pages a bit more complex then just a raw value:
The code example is here:
$bundle = 'test'; $config_pages = \Drupal::service('config_pages.loader'); $config_page_ast = $config_pages->load($bundle, 'a:1:{i:0;a:1:{s:8:"language";s:3:"ast";}}'); $config_page_en = $config_pages->load($bundle, 'a:1:{i:0;a:1:{s:8:"language";s:2:"en";}}'); $config_page_ast->set('field_test', 'test AST value'); $config_page_en->set('field_test', 'test EN value'); $config_page_ast->save(); $config_page_en->save();
You can do it like that.
- 🇨🇦Canada JFKiwad Montreal
@shumer, thanks so much, it works perfectly. As for the context, I went for serialize function like in the module. Here's my complete example :
function MYMODULE_deploy_SOMENAME(): void { $bundle = 'newbundle'; $config_pages_loader = \Drupal::service('config_pages.loader'); $type = ConfigPagesType::load($bundle); foreach (['en', 'fr'] as $langcode) { $context = serialize([['language' => $langcode]]); $config_page = $config_pages_loader->load($bundle, $context); if (!$config_page) { $config_page = ConfigPages::create([ 'type' => $bundle, 'label' => $type->label(), 'context' => $context, ]); $config_page->save(); } $config_page->set('field_x', t("My string", [], $options)); $config_page->save(); } }
- Assigned to shumer
- Status changed to Fixed
over 1 year ago 9:03pm 22 June 2023 Automatically closed - issue fixed for 2 weeks with no activity.