language_get_default_langcode example

public function getFormId() {
    return 'language_elements_configuration_element_test';
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['langcode'] = [
      '#title' => t('Language select'),
      '#type' => 'language_select',
      '#default_value' => language_get_default_langcode('entity_test', 'some_bundle'),
    ];
    return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

}
// Make node body translatable.     $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $field_storage->setTranslatable(TRUE);
    $field_storage->save();
  }

  /** * Tests whether field languages are correctly set through the node form. */
  public function testMultilingualNodeForm() {
    // Create "Basic page" content.     $langcode = language_get_default_langcode('node', 'page');
    $title_key = 'title[0][value]';
    $title_value = $this->randomMachineName(8);
    $body_key = 'body[0][value]';
    $body_value = $this->randomMachineName(16);

    // Create node to edit.     $edit = [];
    $edit[$title_key] = $title_value;
    $edit[$body_key] = $body_value;
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');

    
'id' => $language_code,
        'label' => $this->randomMachineName(),
      ])->save();
    }

    // Fixed language.     ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
      ->setLanguageAlterable(TRUE)
      ->setDefaultLangcode('bb')
      ->save();

    $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
    $this->assertEquals('bb', $langcode);

    // Current interface.     ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
      ->setLanguageAlterable(TRUE)
      ->setDefaultLangcode('current_interface')
      ->save();

    $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
    $this->assertEquals($langcode$language_interface->getId());

    
/** * Provides default language code of given entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity whose language code to be loaded. * * @return string * A string language code. */
  public function getDefaultLangcode(EntityInterface $entity) {
    return language_get_default_langcode($entity->getEntityTypeId()$entity->bundle());
  }

}
Home | Imprint | This part of the site doesn't use cookies.