addTranslation example


  protected function setUpTerms() {
    $parent_vid = 0;
    foreach ($this->termTranslationMap as $name => $translation) {

      $term = $this->createTerm($this->vocabulary, [
        'name' => $name,
        'langcode' => $this->baseLangcode,
        'parent' => $parent_vid,
      ]);

      $term->addTranslation($this->translateToLangcode, [
        'name' => $translation,
      ]);
      $term->save();

      // Each term is nested under the last.       $parent_vid = $term->id();

      $this->terms[] = $term;
    }
  }

  
$node->save();
    $published_revision_id = $node->getRevisionId();

    $node->moderation_state = 'draft';
    $node->save();
    $draft_revision_id = $node->getRevisionId();

    $node->moderation_state = 'aa_draft';
    $node->save();
    $aa_draft_revision_id = $node->getRevisionId();

    $translated = $node->addTranslation('fr');
    $translated->moderation_state = 'zz_draft';
    $translated->title = 'Translated';
    $translated->save();
    $zz_draft_revision_id = $translated->getRevisionId();

    // A second aa_draft revision will be created for the non-translated     // revision. Since in this case there will be two revisions with "aa_draft"     // we add another sort in content_moderation_test_views_views_query_alter.     // Secondary sorting is not an option in views when using exposed sorting     // and table click sorting, so in order to maintain the same level of     // coverage this is required.
/** * Tests removal of content moderation state translations. * * @dataProvider basicModerationTestCases */
  public function testContentModerationStateTranslationDataRemoval($entity_type_id) {
    // Test content moderation state translation deletion.     if ($this->entityTypeManager->getDefinition($entity_type_id)->isTranslatable()) {
      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = $this->createEntity($entity_type_id, 'published');
      $langcode = 'fr';
      $translation = $entity->addTranslation($langcode['title' => 'French title test']);
      // Make sure we add values for all of the required fields.       if ($entity_type_id == 'block_content') {
        $translation->info = $this->randomString();
      }
      $translation->save();
      $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
      $this->assertTrue($content_moderation_state->hasTranslation($langcode));
      $entity->removeTranslation($langcode);
      $entity->save();
      $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
      $this->assertFalse($content_moderation_state->hasTranslation($langcode));
    }
$this->setRawContent($renderer->renderRoot($build));

    $elements = $this->cssSelect('input[type="checkbox"]');
    $this->assertCount(2, $elements, 'Both comments are visible.');
    $this->assertText($comment->label());
    $this->assertText($comment_anonymous->label());
    $executable->destroy();

    // Tests comment translation filter.     if (!$comment->hasTranslation('ur')) {
      // If we don't have the translation then create one.       $comment_translation = $comment->addTranslation('ur', ['subject' => 'ur title']);
      $comment_translation->save();
    }
    else {
      // If we have the translation then unpublish it.       $comment_translation = $comment->getTranslation('ur');
      $comment_translation->setUnpublished();
      $comment_translation->save();
    }
    if (!$comment_anonymous->hasTranslation('ur')) {
      // If we don't have the translation then create one.       $comment_anonymous_translation = $comment_anonymous->addTranslation('ur', ['subject' => 'ur Anonymous title']);
      
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
    $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
    $this->assertSame($last_interface_language$default_site_langcode, 'Interface language did not change from the default site language.');
    $this->assertSame($last_content_language$translation->language()->getId(), 'Content language matches the current entity translation language.');
  }

  /** * Creates a translated entity. */
  protected function createTranslatableEntity() {
    $this->entity = EntityTest::create();
    $this->entity->addTranslation('es', ['name' => 'name spanish']);
    $this->entity->addTranslation('fr', ['name' => 'name french']);
    $this->entity->save();
  }

  /** * Sets the current request to a specific path with the corresponding route. * * @param string $path * The path for which the current request should be created. * @param string $route_name * The route name for which the route object for the request should be * created. */

      [],
      [],
      [],
    ];
    $this->searchableNodes = [];
    foreach ($nodes as $setting) {
      $this->searchableNodes[] = $this->drupalCreateNode($setting);
    }

    // Add a single translation to the second node.     $translation = $this->searchableNodes[1]->addTranslation('hu', ['title' => 'Second node hu']);
    $translation->body->value = $this->randomMachineName(32);
    $this->searchableNodes[1]->save();

    // Add two translations to the third node.     $translation = $this->searchableNodes[2]->addTranslation('hu', ['title' => 'Third node this is the Hungarian title']);
    $translation->body->value = $this->randomMachineName(32);
    $translation = $this->searchableNodes[2]->addTranslation('sv', ['title' => 'Third node sv']);
    $translation->body->value = $this->randomMachineName(32);
    $this->searchableNodes[2]->save();

    // Verify that we have 8 nodes left to do.
'name[0][value]' => $this->randomMachineName(),
      'langcode[0][value]' => 'aa',
    ];
    $this->submitForm($edit, 'Save');
    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties([
      'name' => $edit['name[0][value]'],
    ]);
    $term = reset($terms);

    // Add a translation for that term.     $translated_title = $this->randomMachineName();
    $term->addTranslation('bb', [
      'name' => $translated_title,
    ]);
    $term->save();

    // Overview page in the other language shows the translated term     $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
    $this->assertSession()->responseMatches('|<a[^>]*>' . $translated_title . '</a>|');
  }

}
'type' => 'page',
      'title' => $node_title,
      'body' => $this->randomMachineName(16),
      'uid' => $this->editor->id(),
      'status' => 1,
      'langcode' => $langcodes[0],
    ]);
    $node->save();

    // Create translation.     $translated_node_title = $this->randomMachineName(8);
    $node->addTranslation($langcodes[1]['title' => $translated_node_title, 'body' => $this->randomMachineName(16), 'status' => 1]);
    $node->save();

    // Edit the node and create a menu link.     $edit = [
      'menu[enabled]' => 1,
      'menu[title]' => $node_title,
      'menu[weight]' => 17,
    ];
    $options = ['language' => $languages[$langcodes[0]]];
    $url = $node->toUrl('edit-form', $options);
    $this->drupalGet($url);
    
$this->assertSession()->statusCodeEquals(200);

    // Check the node access table.     $this->checkRecords(2, 'hu');

    // Programmatically create a translation. This process lets us check that     // both forms and code behave in the same way.     $storage = \Drupal::entityTypeManager()->getStorage('node');
    // Reload the node.     $node = $storage->load(1);
    // Create an Afrikaans translation.     $translation = $node->addTranslation('af');
    $translation->title->value = $this->randomString();
    $translation->status = 1;
    $node->save();

    // Check the node access table.     $this->checkRecords(3, 'hu');

    // For completeness, edit the Catalan version again.     $this->drupalGet('node/' . $node->id() . '/edit', $url_options);
    $this->assertSession()->statusCodeEquals(200);
    // Save the form.
/** @var \Drupal\Core\Entity\ContentEntityInterface $default_revision */
      $default_revision = $this->load($entity->id());
      $translation_languages = $default_revision->getTranslationLanguages();
      foreach ($translation_languages as $langcode => $language) {
        if ($langcode == $active_langcode) {
          continue;
        }

        $default_revision_translation = $default_revision->getTranslation($langcode);
        $new_revision_translation = $new_revision->hasTranslation($langcode) ?
          $new_revision->getTranslation($langcode) : $new_revision->addTranslation($langcode);

        /** @var \Drupal\Core\Field\FieldItemListInterface[] $sync_items */
        $sync_items = array_diff_key(
          $keep_untranslatable_fields ? $default_revision_translation->getTranslatableFields() : $default_revision_translation->getFields(),
          $skipped_field_names
        );
        foreach ($sync_items as $field_name => $items) {
          $new_revision_translation->set($field_name$items->getValue());
        }

        // Make sure the "revision_translation_affected" flag is recalculated.
for ($i = 0; $i < 3; $i++) {
      $node = Node::create([
        'type' => 'article',
        'title' => 'Title en ' . $i,
        'weight' => ['value' => 3 - $i],
        'text' => ['value' => 'moo en ' . $i],
        'langcode' => 'en',
      ]);
      $node->save();

      $translation = $node->addTranslation('de');
      $translation->title->value = 'Title DE ' . $i;
      $translation->text->value = 'moo DE ' . $i;
      $translation->save();
    }
  }

  /** * Tests sorting on an untranslated field. */
  public function testSortbyUntranslatedIntegerField() {
    $map = [
      

  public function testTranslated() {
    // Create a destination.     $this->createDestination(['translations' => TRUE]);

    // Create some pre-existing entities.     $this->storage->create(['id' => 1, 'langcode' => 'en'])->save();
    $this->storage->create(['id' => 2, 'langcode' => 'fr'])->save();
    $translated = $this->storage->create(['id' => 3, 'langcode' => 'en']);
    $translated->save();
    $translated->addTranslation('fr')->save();

    // Pre-assert that things are as expected.     $this->assertTranslations(1, 'en');
    $this->assertTranslations(2, 'fr');
    $this->assertTranslations(3, 'en', ['fr']);
    $this->assertNull($this->storage->load(4));

    $destination_rows = [
      // Existing default translation.       ['id' => 1, 'langcode' => 'en', 'action' => MigrateIdMapInterface::ROLLBACK_PRESERVE],
      // New translation.
$this->drupalGet('admin/config/regional/content-language');
    $this->submitForm($edit, 'Save configuration');

    // Add a node in English, with title "sandwich".     $values = [
      'title' => 'sandwich',
      'type' => $type->id(),
    ];
    $node = $this->drupalCreateNode($values);

    // "Translate" this node into Spanish, with title "pizza".     $node->addTranslation('es', ['title' => 'pizza', 'status' => NodeInterface::PUBLISHED]);
    $node->save();

    // Run cron so that the search index tables are updated.     $this->cronRun();

    // Test the keyword filter by visiting the page.     // The views are in the test view 'test_search', and they just display the     // titles of the nodes in the result, as links.
    // Page with a keyword filter of 'pizza'. This should find the Spanish     // translated node, which has 'pizza' in the title, but not the English


  /** * Tests translating a non-revisionable field. */
  public function testTranslatingNonRevisionableField() {
    /** @var \Drupal\Core\Entity\ContentEntityBase $entity */
    $entity = EntityTestMulRev::create();
    $entity->set('non_rev_field', 'Hello');
    $entity->save();

    $translation = $entity->addTranslation('es');
    $translation->set('non_rev_field', 'Hola');
    $translation->save();

    $reloaded = EntityTestMulRev::load($entity->id());
    $this->assertEquals('Hello', $reloaded->getTranslation('en')->get('non_rev_field')->value);

    $this->assertEquals('Hola', $reloaded->getTranslation('es')->get('non_rev_field')->value);
  }

}
// The default entity URL tests should pass again with the current     // configuration.     $this->testEntityUrlLanguage();
  }

  /** * Creates a translated entity. */
  protected function createTranslatableEntity() {
    $this->entity = EntityTest::create();
    $this->entity->addTranslation('es', ['name' => 'name spanish']);
    $this->entity->addTranslation('fr', ['name' => 'name french']);
    $this->entity->save();
  }

  /** * Sets the current request to a specific path with the corresponding route. * * @param string $path * The path for which the current request should be created. * @param string $route_name * The route name for which the route object for the request should be * created. */
Home | Imprint | This part of the site doesn't use cookies.