How to deploy fields values in a multilingual context on a newly created ConfigPage?

Created on 22 June 2023, over 1 year ago

So I create a new config page type and consider language for it's context. I add some fields then I export the configurations.

However, before the deployment on production, I'd like to add default values for the fields I created, in english as well as in the other language.

I thought it would be a good place to do it in HOOK_deploy_NAME() function, so I wrote some code to set the values on my newly created ConfigPage entity. Interestingly, the entity outputs null. So I create it manually instead. My problem here is that I can't find the way to set the values by specifying the language. Heres my code so far. Thanks for the help!

function MYMODULE_deploy_NAMEHERE(): void {
  $bundle = 'newbundle';
  $type = ConfigPagesType::load($bundle);
  $config_pages = \Drupal::service('config_pages.loader');

  foreach (['en', 'fr'] as $langcode) {
    $options = ['langcode' => $langcode];
    $config_page = $config_pages->load($bundle, $options);
    if (!$config_page) {
      $config_page = ConfigPages::create([
        'type' => $bundle,
        'label' => $type->label(),
        'context' => $type->getContextData(), // Here maybe?
      ]);
      $config_page->save();
    }
    $config_page->set('field_x', t("My string", [], $options));
    $config_page->save();

    // I also tried the following, without success
    // if (!$config_page->hasTranslation($langcode)) {
    //   $translation = $config_page->addTranslation($langcode, $config_page->toArray());
    // } else {
    //   $translation = $config_page->getTranslation($langcode);
    // }
    // $translation->set('field_x', t("My string", [], $options));
    // $translation->save();

  }
}
💬 Support request
Status

Fixed

Version

3.0

Component

Code

Created by

🇨🇦Canada JFKiwad Montreal

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • 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
  • 🇵🇱Poland shumer Wroclaw

    Glad that it worked!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024