0

I am programmatically creating some content during profile installation and since Drupal uses "en" langcode during this period I am getting the language from config and it is working fine.

The issue I have is that I am creating a node and I will end up with it being in English. So I have set the "langcode" property of the node during creation which worked as expected. But the fields are not responding to this fact(I have only the body field attached).

So I have tried to key the value(s) by the langcode(Drupal 7 style) but it resulted in the field not being populated in DB at all.

The node entity nor field item list nor field item have methods for setting the langcode so I am unsure how to proceed with this.

Any ideas?

This is my code:

// Create 'terms of service' page
  $tos = entity_create('node', [
    'type' => 'page',
    'title' => t('Terms of service', [], ['langcode' => $langcode]),
    'uid' => 1,
    'status' => 1,
    'langcode' => $langcode, // this is working on node only
    'defaultLangcode' => $langcode, // no change on field
    'activeLangcode' => $langcode, // no change on field
    'body' => [
      $langcode => [ // this is not working
        [
          'value' => '<p>' . t('This is a placeholder. Please edit this page and use your own text.', [], ['langcode' => $langcode]) . '</p>',
          'summary' => '',
          'format' => FULL_HTML_FORMAT
        ]
      ]
    ]
  ]);
  $tos->save();

I have found setLangcode() method on the FieldItemList but that didn't work as well.


I have tried to save the node without body, then set the body and save the node again, but the field is in English again(and the node in installed language). Is it possible that this is kind of a (edge case)bug that relates to language manager?


I have checked the entity constructor and this is also not working:

'body' => [
      $langcode => [
        [
          'value' => [
            'value' => 'some text',
            'summary' => '',
            'format' => FULL_HTML_FORMAT
          ]
        ]
      ]
    ]
2
  • I don't understand what you mean with fields not responding to the fact? Based on what do you think that? The fields will definitely be in the language that you specify for the node. To add values for additional languages, you can use createTranslation.
    – Berdir
    Feb 10, 2016 at 17:29
  • @Berdir I mean that in DB th "en" langcode is used on the FIELDS(node__body), the node(node, node_revision, node_field_data) is fine.
    – user21641
    Feb 10, 2016 at 18:57

1 Answer 1

0

I have found a solution. As I have mentioned this was during the profile installation process and it is related to How to get correct language code during installation?

The solution was to make sure the installed language exists before the modules are being installed and set it as default. Normally, translation are the last installation task. So I have copied the following code from install_download_additional_translations_operations() into a task in my profile that runs before modules are installed.

if ($langcode != 'en') {
    \Drupal::service('module_installer')->install(['language']);
    if (!($language = entity_load('configurable_language', $langcode))) {
      // Create the language if not already shipped with a profile.
      $language = ConfigurableLanguage::createFromLangcode($langcode);
    }
    $language->save();

    \Drupal::service('language.default')->set($language);
  }

I have also removed the $langcode from my code since translations were now working as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.