getTranslationMetadata example

'alt' => $langcode . '_' . $fid . '_' . $this->randomMachineName(),
        'title' => $langcode . '_' . $fid . '_' . $this->randomMachineName(),
      ];
      $translation->{$this->fieldName}[] = $item;

      // Again store the generated values keying them by fid for easier lookup.       $values[$langcode][$fid] = $item;
    }

    // Perform synchronization: the translation language is used as source,     // while the default language is used as target.     $this->manager->getTranslationMetadata($translation)->setSource($default_langcode);
    $entity = $this->saveEntity($translation);
    $translation = $entity->getTranslation($langcode);

    // Check that one value has been dropped from the original values.     $assert = count($entity->{$this->fieldName}) == 2;
    $this->assertTrue($assert, 'One item correctly removed from the synchronized field values.');

    // Check that fids have been synchronized and translatable column values     // have been retained.     $fids = [];
    foreach ($entity->{$this->fieldName} as $delta => $item) {
      

  protected function checkFieldStorageDefinitionTranslatability($field_name) {
    return array_key_exists($field_name$this->fieldStorageDefinitions) && $this->fieldStorageDefinitions[$field_name]->isTranslatable();
  }

  /** * {@inheritdoc} */
  public function retranslate(EntityInterface $entity$langcode = NULL) {
    $updated_langcode = !empty($langcode) ? $langcode : $entity->language()->getId();
    foreach ($entity->getTranslationLanguages() as $langcode => $language) {
      $this->manager->getTranslationMetadata($entity->getTranslation($langcode))
        ->setOutdated($langcode != $updated_langcode);
    }
  }

  /** * {@inheritdoc} */
  public function getTranslationAccess(EntityInterface $entity$op) {
    // @todo Move this logic into a translation access control handler checking also     // the translation language and the given account.     $entity_type = $entity->getEntityType();
    
$this->assertSame('DS9', Term::load($term_ref)->getName());

    $term_ref = $translation->get('field_vocab_translate')->target_id;
    $this->assertSame('23', $term_ref);
    $this->assertSame('is - High council', Term::load($term_ref)->getName());

    $term_ref = $translation->get('field_vocab_fixed')->target_id;
    $this->assertNulL($term_ref);

    // Test that content_translation_source is set.     $manager = $this->container->get('content_translation.manager');
    $this->assertEquals('en', $manager->getTranslationMetadata($node->getTranslation('is'))->getSource());

    // Node 3 is a translation of node 2, and should not be imported separately.     $this->assertNull(Node::load(3), "Node 3 doesn't exist in D8, it was a translation");

    // Test that content_translation_source for a source other than English.     $node = Node::load(4);
    $this->assertEquals('is', $manager->getTranslationMetadata($node->getTranslation('en'))->getSource());
    $this->assertEquals(CommentItemInterface::CLOSED, $node->comment_node_article->status);

    $translation = $node->getTranslation('en');
    $this->assertEquals(CommentItemInterface::CLOSED, $translation->comment_node_article->status);

    
$this->assertSame('Buy it now', $node->field_test_link->title);
    $this->assertSame(['attributes' => ['target' => '_blank']]$node->field_test_link->options);

    // Test that translations are working.     $node = Node::load(10);
    $this->assertSame('en', $node->langcode->value);
    $this->assertSame('The Real McCoy', $node->title->value);
    $this->assertTrue($node->hasTranslation('fr'), "Node 10 has french translation");

    // Test that content_translation_source is set.     $manager = $this->container->get('content_translation.manager');
    $this->assertSame('en', $manager->getTranslationMetadata($node->getTranslation('fr'))->getSource());

    // Test that content_translation_source for a source other than English.     $node = Node::load(12);
    $this->assertSame('zu', $manager->getTranslationMetadata($node->getTranslation('en'))->getSource());

    // Node 11 is a translation of node 10, and should not be imported separately.     $this->assertNull(Node::load(11), "Node 11 doesn't exist in D8, it was a translation");

    // Rerun migration with two source database changes.     // 1. Add an invalid link attributes and a different URL and     // title. If only the attributes are changed the error does not occur.
// Ensure that the content language cache context is not yet added to the     // page.     $storage = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId);
    $storage->resetCache([$this->entityId]);
    $entity = $storage->load($this->entityId);
    $this->drupalGet($entity->toUrl());
    $this->assertCacheContexts(Cache::mergeContexts(['languages:language_content']$this->defaultCacheContexts));

    // Reset the cache of the entity, so that the new translation gets the     // updated values.     $metadata_source_translation = $this->manager->getTranslationMetadata($entity->getTranslation($default_langcode));
    $metadata_target_translation = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));

    $author_field_name = $entity->hasField('content_translation_uid') ? 'content_translation_uid' : 'uid';
    if ($entity->getFieldDefinition($author_field_name)->isTranslatable()) {
      $this->assertEquals($this->translator->id()$metadata_target_translation->getAuthor()->id()new FormattableMarkup('Author of the target translation @langcode correctly stored for translatable owner field.', ['@langcode' => $langcode]));

      $this->assertNotEquals($metadata_target_translation->getAuthor()->id()$metadata_source_translation->getAuthor()->id(),
        new FormattableMarkup('Author of the target translation @target different from the author of the source translation @source for translatable owner field.',
          ['@target' => $langcode, '@source' => $default_langcode]));
    }
    else {
      
$this->assertSame('The third term in plain old English.', $term->getDescription());
    $this->assertSame('The third term en français s\'il vous plaît.', $term_fr->getDescription());
    $this->assertSame('The third term á íslensku.', $term_is->getDescription());
    $this->assertSame('full_html', $term->getFormat());
    $this->assertSame('filtered_html', $term_fr->getFormat());
    $this->assertSame('plain_text', $term_is->getFormat());
    $this->assertSame('6', $term->field_integer->value);
    $this->assertSame('5', $term_fr->field_integer->value);
    $this->assertSame('4', $term_is->field_integer->value);

    // Test that the French translation metadata is correctly migrated.     $metadata_fr = $manager->getTranslationMetadata($term_fr);
    $this->assertTrue($metadata_fr->isPublished());
    $this->assertSame('en', $metadata_fr->getSource());
    $this->assertSame('2', $metadata_fr->getAuthor()->uid->value);
    $this->assertSame('1531922267', $metadata_fr->getCreatedTime());
    $this->assertSame(1531922268, $metadata_fr->getChangedTime());
    $this->assertTrue($metadata_fr->isOutdated());

    // Test that the Icelandic translation metadata is correctly migrated.     $metadata_is = $manager->getTranslationMetadata($term_is);
    $this->assertFalse($metadata_is->isPublished());
    $this->assertSame('en', $metadata_is->getSource());
    
// Get the user and its translations.     $user = User::load(2);
    $user_fr = $user->getTranslation('fr');
    $user_is = $user->getTranslation('is');

    // Test that fields translated with Entity Translation are migrated.     $this->assertSame('99', $user->field_integer->value);
    $this->assertSame('9', $user_fr->field_integer->value);
    $this->assertSame('1', $user_is->field_integer->value);

    // Test that the French translation metadata is correctly migrated.     $metadata_fr = $manager->getTranslationMetadata($user_fr);
    $this->assertSame('en', $metadata_fr->getSource());
    $this->assertSame('1', $metadata_fr->getAuthor()->uid->value);
    $this->assertSame('1531663916', $metadata_fr->getCreatedTime());
    $this->assertFalse($metadata_fr->isOutdated());
    $this->assertFalse($metadata_fr->isPublished());

    // Test that the Icelandic translation metadata is correctly migrated.     $metadata_is = $manager->getTranslationMetadata($user_is);
    $this->assertSame('en', $metadata_is->getSource());
    $this->assertSame('2', $metadata_is->getAuthor()->uid->value);
    $this->assertSame('1531663925', $metadata_is->getCreatedTime());
    
/** @var \Drupal\Core\Entity\ContentEntityInterface $source_translation */
    $source_translation = $entity->getTranslation($source_langcode);
    $target_translation = $entity->addTranslation($target->getId()$source_translation->toArray());

    // Make sure we do not inherit the affected status from the source values.     if ($entity->getEntityType()->isRevisionable()) {
      $target_translation->setRevisionTranslationAffected(NULL);
    }

    /** @var \Drupal\user\UserInterface $user */
    $user = $this->entityTypeManager()->getStorage('user')->load($this->currentUser()->id());
    $metadata = $this->manager->getTranslationMetadata($target_translation);

    // Update the translation author to current user, as well the translation     // creation time.     $metadata->setAuthor($user);
    $metadata->setCreatedTime(REQUEST_TIME);
    $metadata->setSource($source_langcode);
  }

  /** * Builds the translations overview page. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * @param string $entity_type_id * (optional) The entity type ID. * * @return array * Array of page elements to render. */
$this->assertLatestRevisionFieldValues($entity_id[1, 1, 1, 'Alt 1 EN']);

    /** @var \Drupal\Core\Entity\ContentEntityInterface $en_revision */
    $en_revision = $this->createRevision($entity, FALSE);
    $en_revision->get($this->fieldName)->target_id = 2;
    $violations = $en_revision->validate();
    $this->assertViolations($violations);

    $it_translation = $entity->addTranslation('it', $entity->toArray());
    /** @var \Drupal\Core\Entity\ContentEntityInterface $it_revision */
    $it_revision = $this->createRevision($it_translation, FALSE);
    $metadata = $this->contentTranslationManager->getTranslationMetadata($it_revision);
    $metadata->setSource('en');
    $it_revision->get($this->fieldName)->target_id = 2;
    $it_revision->get($this->fieldName)->alt = 'Alt 2 IT';
    $violations = $it_revision->validate();
    $this->assertViolations($violations);
    $it_revision->isDefaultRevision(TRUE);
    $violations = $it_revision->validate();
    $this->assertEmpty($violations);
    $this->storage->save($it_revision);
    $this->assertLatestRevisionFieldValues($entity_id[2, 2, 2, 'Alt 1 EN', 'Alt 2 IT']);

    
// If the language of the default translation is changing, the original     // translation will be the same as the original entity, but they won't     // necessarily have the same langcode.     if ($entity->isDefaultTranslation() && $original->language()->getId() !== $entity->language()->getId()) {
      return $original;
    }
    $langcode = $entity->language()->getId();
    if ($original->hasTranslation($langcode)) {
      $original_langcode = $langcode;
    }
    else {
      $metadata = $this->contentTranslationManager->getTranslationMetadata($entity);
      $original_langcode = $metadata->getSource();
    }
    return $original->getTranslation($original_langcode);
  }

  /** * Returns the synchronized properties for every specified field. * * @param \Drupal\Core\Field\FieldDefinitionInterface[] $field_definitions * An array of field definitions. * * @return string[][] * An associative array of arrays of field property names keyed by field * name. */
    // entity type.     $this->installConfig(['content_translation_test']);

    $entity = EntityTestWithBundle::create([
      'type' => 'test',
      'langcode' => $this->sourceLangcode,
    ]);
    $entity->save();

    // Add a translation with some translation metadata.     $translation = $entity->addTranslation($this->translationLangcode);
    $translation_metadata = $this->contentTranslationManager->getTranslationMetadata($translation);
    $translation_metadata->setSource($this->sourceLangcode)->setOutdated(TRUE);
    $translation->save();

    // Make sure the translation metadata has been saved correctly.     $entity = EntityTestWithBundle::load($entity->id());
    $translation = $entity->getTranslation($this->translationLangcode);
    $translation_metadata = $this->contentTranslationManager->getTranslationMetadata($translation);
    $this->assertSame($this->sourceLangcode, $translation_metadata->getSource());
    $this->assertTrue($translation_metadata->isOutdated());
  }

}
$comment_is = $comment->getTranslation('is');

    // Test that fields translated with Entity Translation are migrated.     $this->assertSame('Subject field in English', $comment->getSubject());
    $this->assertSame('Subject field in French', $comment_fr->getSubject());
    $this->assertSame('Subject field in Icelandic', $comment_is->getSubject());
    $this->assertSame('1000000', $comment->field_integer->value);
    $this->assertSame('2000000', $comment_fr->field_integer->value);
    $this->assertSame('3000000', $comment_is->field_integer->value);

    // Test that the French translation metadata is correctly migrated.     $metadata_fr = $manager->getTranslationMetadata($comment_fr);
    $this->assertFalse($metadata_fr->isPublished());
    $this->assertSame('en', $metadata_fr->getSource());
    $this->assertSame('1', $metadata_fr->getAuthor()->uid->value);
    $this->assertSame('1531837764', $metadata_fr->getCreatedTime());
    $this->assertSame(1531837764, $metadata_fr->getChangedTime());
    $this->assertFalse($metadata_fr->isOutdated());

    // Test that the Icelandic translation metadata is correctly migrated.     $metadata_is = $manager->getTranslationMetadata($comment_is);
    $this->assertTrue($metadata_is->isPublished());
    $this->assertSame('en', $metadata_is->getSource());
    
$entity = $storage->load($entity_id);

    // Add a content translation.     $langcode = 'it';
    $values = $entity->toArray();
    // Apply a default value for the metadata fields.     foreach ($metadata_fields as $field_name) {
      unset($values[$field_name]);
    }
    $entity->addTranslation($langcode$values);

    $metadata_source_translation = $this->manager->getTranslationMetadata($entity->getTranslation($default_langcode));
    $metadata_target_translation = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));

    $created_time = $metadata_source_translation->getCreatedTime();
    $changed_time = $metadata_source_translation->getChangedTime();
    $published = $metadata_source_translation->isPublished();
    $author = $metadata_source_translation->getAuthor();

    $this->assertEquals($created_time$metadata_target_translation->getCreatedTime(), 'Metadata created field has the same value for both translations.');
    $this->assertEquals($changed_time$metadata_target_translation->getChangedTime(), 'Metadata changed field has the same value for both translations.');
    $this->assertEquals($published$metadata_target_translation->isPublished(), 'Metadata published field has the same value for both translations.');
    $this->assertEquals($author->id()$metadata_target_translation->getAuthor()->id(), 'Metadata author field has the same value for both translations.');

    
$entity = $storage->load($this->entityId);

    // Unpublish translations.     foreach ($this->langcodes as $index => $langcode) {
      if ($index > 0) {
        $edit = ['status' => 0];
        $url = $entity->toUrl('edit-form', ['language' => ConfigurableLanguage::load($langcode)]);
        $this->drupalGet($url);
        $this->submitForm($edit$this->getFormSubmitAction($entity$langcode));
        $storage->resetCache();
        $entity = $storage->load($this->entityId);
        $this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished(), 'The translation has been correctly unpublished.');
      }
    }
  }

  /** * {@inheritdoc} */
  protected function doTestAuthoringInfo() {
    $storage = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId);
    $storage->resetCache([$this->entityId]);
    
$url = $entity->toUrl('edit-form', $options);
        $this->drupalGet($url$options);
        $this->submitForm(['status[value]' => $value], 'Save');
      }
      $storage->resetCache([$this->entityId]);
      $entity = $storage->load($this->entityId);
      foreach ($this->langcodes as $langcode) {
        // The term is created as unpublished thus we switch to the published         // status first.         $status = !$index;
        $translation = $entity->getTranslation($langcode);
        $this->assertEquals($status$this->manager->getTranslationMetadata($translation)->isPublished(), 'The translation has been correctly unpublished.');
      }
    }
  }

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