loadByEntityTypeBundle example

/** * {@inheritdoc} */
  public function getFormId() {
    return 'language_elements_configuration_element';
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle');

    $form['lang_configuration'] = [
      '#type' => 'language_configuration',
      '#entity_information' => [
        'entity_type' => 'entity_test',
        'bundle' => 'some_bundle',
      ],
      '#default_value' => $conf,
    ];

    $form['submit'] = [
      
'settings[entity_test_mul][entity_test_mul][fields][name]' => TRUE,
      'settings[entity_test_mul][entity_test_mul][fields][user_id]' => FALSE,
    ];
    $this->assertSettings('entity_test_mul', 'entity_test_mul', TRUE, $edit);
    $field_override = BaseFieldOverride::loadByName('entity_test_mul', 'entity_test_mul', 'name');
    $this->assertTrue($field_override->isTranslatable(), 'Base fields can be overridden with a base field bundle override entity.');
    $definitions = $entity_field_manager->getFieldDefinitions('entity_test_mul', 'entity_test_mul');
    $this->assertTrue($definitions['name']->isTranslatable());
    $this->assertFalse($definitions['user_id']->isTranslatable());

    // Test that language settings are correctly stored.     $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_article');
    $this->assertEquals('current_interface', $language_configuration->getDefaultLangcode(), 'The default language for article comments is set to the interface text language selected for page.');
    $this->assertTrue($language_configuration->isLanguageAlterable(), 'The language selector for article comments is shown.');

    // Verify language widget appears on comment type form.     $this->drupalGet('admin/structure/comment/manage/comment_article');
    $this->assertSession()->fieldExists('language_configuration[content_translation]');
    $this->assertSession()->checkboxChecked('edit-language-configuration-content-translation');

    // Verify that translation may be enabled for the article content type.     $edit = [
      'language_configuration[content_translation]' => TRUE,
    ];
'delete any article content',
      'administer content translation',
      'translate any entity',
      'create content translations',
      'administer languages',
      'administer content types',
      'administer node fields',
    ]);
    $this->drupalLogin($user);

    // Enable translation for article.     $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article');
    $config->setDefaultLangcode(LanguageInterface::LANGCODE_SITE_DEFAULT);
    $config->setLanguageAlterable(TRUE);
    $config->save();

    $content_translation_manager = $this->container->get('content_translation.manager');
    $content_translation_manager->setEnabled('node', 'article', TRUE);
    $content_translation_manager->setBundleTranslationSettings('node', 'article', [
      'untranslatable_fields_hide' => FALSE,
    ]);

    $this->rebuildContainer();

    
/** @var \Drupal\user\UserInterface $user */
    $user = $this->createUser([], '', TRUE);
    $this->drupalLogin($user);
    ConfigurableLanguage::createFromLangcode('es')->save();

    // Create a page node type and make it translatable.     NodeType::create([
      'type' => 'page',
      'name' => 'Page',
    ])->save();

    $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page');
    $config->setDefaultLangcode('en')
      ->setLanguageAlterable(TRUE)
      ->save();

    // Create a Node with title 'English' and translate it to Spanish.     $node = Node::create([
      'type' => 'page',
      'title' => 'English',
    ]);
    $node->save();
    $node->addTranslation('es', ['title' => 'EspaƱol']);
    
'#title' => $this->t('Target entity type'),
      ];
    }

    if ($this->moduleHandler->moduleExists('content_translation')) {
      $form['language'] = [
        '#type' => 'details',
        '#title' => $this->t('Language settings'),
        '#group' => 'additional_settings',
      ];

      $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', $comment_type->id());
      $form['language']['language_configuration'] = [
        '#type' => 'language_configuration',
        '#entity_information' => [
          'entity_type' => 'comment',
          'bundle' => $comment_type->id(),
        ],
        '#default_value' => $language_configuration,
      ];

      $form['#submit'][] = 'language_configuration_element_submit';
    }

    

  public function testApiTranslation(): void {
    $this->container->get('module_installer')->install(['language', 'content_translation']);
    $this->resetAll();
    ConfigurableLanguage::create(['id' => 'fi'])->save();
    $this->container->get('config.factory')->getEditable('language.negotiation')
      ->set('url.source', 'path_prefix')
      ->set('url.prefixes.fi', 'fi')
      ->save();
    $this->rebuildContainer();
    ContentLanguageSettings::loadByEntityTypeBundle('media', 'image')
      ->setDefaultLangcode('en')
      ->setLanguageAlterable(TRUE)
      ->save();
    $media_fi = Media::load($this->mediaImage->id())->addTranslation('fi');
    $media_fi->field_media_image->setValue([
      [
        'target_id' => '1',
        // cSpell:disable-next-line         'alt' => 'oletus alt-teksti kuvalle',
      ],
    ]);
    
'revision' => $this->t('Create new revision'),
      ],
      '#description' => $this->t('Users with sufficient access rights will be able to override these options.'),
    ];
    if ($this->moduleHandler->moduleExists('language')) {
      $form['language'] = [
        '#type' => 'details',
        '#title' => $this->t('Language settings'),
        '#group' => 'additional_settings',
      ];

      $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', $type->id());
      $form['language']['language_configuration'] = [
        '#type' => 'language_configuration',
        '#entity_information' => [
          'entity_type' => 'node',
          'bundle' => $type->id(),
        ],
        '#default_value' => $language_configuration,
      ];
    }
    $form['display'] = [
      '#type' => 'details',
      
/** * Tests migration of content language settings. */
  public function testLanguageCommentSettings() {
    // Confirm there is no message about a missing bundle.     $this->assertEmpty($this->migrateMessages, $this->migrateMessages['error'][0] ?? '');

    // Article and Blog content type have multilingual settings of 'Enabled,     // with Translation'. Assert that comments are translatable and the default     // language is 'current_interface'.     $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_article');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_article', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $third_party_settings = [
      'content_translation' => [
        'enabled' => FALSE,
      ],
    ];
    $this->assertSame($third_party_settings$config->get('third_party_settings'));

    
'd6_language_content_comment_settings',
    ]);
  }

  /** * Tests migration of comment content language settings. */
  public function testLanguageCommentSettings() {
    // Article and Employee content type have multilingual settings of 'Enabled,     // with Translation'. Assert that comments are not translatable and the     // default language is 'current_interface'.     $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_article');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_article', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $third_party_settings = [
      'content_translation' => [
        'enabled' => FALSE,
      ],
    ];
    $this->assertSame($third_party_settings$config->get('third_party_settings'));

    
$this->drupalLogin($user);
  }

  /** * Tests the language settings have been saved. */
  public function testLanguageConfigurationElement() {
    $this->drupalGet('language-tests/language_configuration_element');
    $edit['lang_configuration[langcode]'] = 'current_interface';
    $edit['lang_configuration[language_alterable]'] = FALSE;
    $this->submitForm($edit, 'Save');
    $lang_conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle');

    // Check that the settings have been saved.     $this->assertEquals('current_interface', $lang_conf->getDefaultLangcode());
    $this->assertFalse($lang_conf->isLanguageAlterable());
    $this->drupalGet('language-tests/language_configuration_element');
    $this->assertTrue($this->assertSession()->optionExists('edit-lang-configuration-langcode', 'current_interface')->isSelected());
    $this->assertSession()->checkboxNotChecked('edit-lang-configuration-language-alterable');

    // Reload the page and save again.     $this->drupalGet('language-tests/language_configuration_element');
    $edit['lang_configuration[langcode]'] = 'authors_default';
    

  protected function createContentType($name$langcode) {
    $content_type = $this->container->get('entity_type.manager')->getStorage('node_type')->create([
      'name' => 'Test ' . $name,
      'title_label' => 'Title',
      'type' => $name,
      'create_body' => FALSE,
    ]);
    $content_type->save();
    ContentLanguageSettings::loadByEntityTypeBundle('node', $name)
      ->setLanguageAlterable(FALSE)
      ->setDefaultLangcode($langcode)
      ->save();

  }

  /** * Creates a new node of given type and language using Entity API. * * @param string $type * The node content type. * @param string $langcode * (optional) Language code to pass to entity create. * * @return \Drupal\node\NodeInterface * The node created. */

    $config->save();

    \Drupal::configFactory()->getEditable('language.negotiation')
      ->set('url.prefixes.aa', 'aa')
      ->set('url.prefixes.bb', 'bb')
      ->set('url.prefixes.cc', 'cc')
      ->set('selected_langcode', 'dd')
      ->save();

    // Set default language code for content type page to 'dd'.     ContentLanguageSettings::loadByEntityTypeBundle('node', 'page')
      ->setDefaultLangcode('dd')
      ->setLanguageAlterable(TRUE)
      ->save();
    // Set default language code to for menu_link_content 'dd'.     ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
      ->setDefaultLangcode('dd')
      ->setLanguageAlterable(TRUE)
      ->save();
    $this->config('system.feature_flags')
      ->set('linkset_endpoint', TRUE)
      ->save(TRUE);
    
/** * Tests filtering entries with different translatability. */
  public function testEntityFieldFilter() {
    NodeType::create(['type' => 'bundle1'])->save();
    NodeType::create(['type' => 'bundle2'])->save();

    // Create some example content.     ConfigurableLanguage::create(['id' => 'es'])->save();
    ConfigurableLanguage::create(['id' => 'fr'])->save();

    ContentLanguageSettings::loadByEntityTypeBundle('node', 'bundle1')
      ->setDefaultLangcode('es')
      ->setLanguageAlterable(TRUE)
      ->save();
    ContentLanguageSettings::loadByEntityTypeBundle('node', 'bundle2')
      ->setDefaultLangcode('es')
      ->setLanguageAlterable(TRUE)
      ->save();

    $field_translation_map = [
      1 => ['bundle1' => TRUE, 'bundle2' => TRUE],
      2 => ['bundle1' => FALSE, 'bundle2' => FALSE],
      

  public function testLanguageContent() {
    // Assert that a translatable content is still translatable.     $config = $this->config('language.content_settings.node.blog');
    $this->assertSame($config->get('target_entity_type_id'), 'node');
    $this->assertSame($config->get('target_bundle'), 'blog');
    $this->assertSame($config->get('default_langcode'), 'current_interface');
    $this->assertFalse($config->get('language_alterable'));
    $this->assertTrue($config->get('third_party_settings.content_translation.enabled'));

    // Assert that a translatable content is translatable.     $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page');
    $this->assertFalse($config->isDefaultConfiguration());
    $this->assertTrue($config->isLanguageAlterable());
    $this->assertSame($config->getDefaultLangcode(), 'current_interface');

    // Assert that a non-translatable content is not translatable.     $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'forum');
    $this->assertTrue($config->isDefaultConfiguration());
    $this->assertFalse($config->isLanguageAlterable());
    $this->assertSame($config->getDefaultLangcode(), 'site_default');

    // Make sure there's no migration exceptions.
->with('language_content_settings')
      ->willReturn($this->configEntityStorageInterface);

    $entity_type_repository = $this->getMockForAbstractClass(EntityTypeRepositoryInterface::class);
    $entity_type_repository->expects($this->any())
      ->method('getEntityTypeFromClass')
      ->with(ContentLanguageSettings::class)
      ->willReturn('language_content_settings');

    \Drupal::getContainer()->set('entity_type.repository', $entity_type_repository);

    $config = ContentLanguageSettings::loadByEntityTypeBundle($type$bundle);

    $this->assertSame($expected_langcode$config->getDefaultLangcode());
    $this->assertSame($expected_language_alterable$config->isLanguageAlterable());
  }

  public static function providerLoadByEntityTypeBundle() {
    $alteredLanguage = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_bundle',
    ], 'language_content_settings');
    $alteredLanguage->setLanguageAlterable(TRUE);

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