isNewTranslation example


  protected function createRevision(ContentEntityInterface $translation$default = TRUE) {
    if (!$translation->isNewTranslation()) {
      $langcode = $translation->language()->getId();
      $revision_id = $this->storage->getLatestTranslationAffectedRevisionId($translation->id()$langcode);
      /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
      $revision = $this->storage->loadRevision($revision_id);
      $translation = $revision->getTranslation($langcode);
    }
    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
    $revision = $this->storage->createRevision($translation$default);
    return $revision;
  }

  
$entity = $this->entityTypeManager
      ->getStorage($entity_type)
      ->create(['name' => $this->randomMachineName()$langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED]);

    $entity->save();
    $hooks = $this->getHooksInfo();
    $this->assertEmpty($hooks, 'No entity translation hooks are fired when creating an entity.');

    // Verify that we obtain the entity object itself when we attempt to     // retrieve a translation referring to it.     $translation = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED);
    $this->assertFalse($translation->isNewTranslation(), 'Existing translations are not marked as new.');
    $this->assertSame($entity$translation, 'The translation object corresponding to a non-default language is the entity object itself when the entity is language-neutral.');
    $entity->{$langcode_key}->value = $default_langcode;
    $translation = $entity->getTranslation($default_langcode);
    $this->assertSame($entity$translation, 'The translation object corresponding to the default language (explicit) is the entity object itself.');
    $translation = $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
    $this->assertSame($entity$translation, 'The translation object corresponding to the default language (implicit) is the entity object itself.');
    $this->assertTrue($entity->{$default_langcode_key}->value, 'The translation object is the default one.');

    // Verify that trying to retrieve a translation for a locked language when     // the entity is language-aware causes an exception to be thrown.     try {
      
/** * {@inheritdoc} */
  public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */

    $metadata = $this->manager->getTranslationMetadata($entity);
    $form_object = $form_state->getFormObject();
    $form_langcode = $form_object->getFormLangcode($form_state);
    $entity_langcode = $entity->getUntranslated()->language()->getId();

    $new_translation = $entity->isNewTranslation();
    $translations = $entity->getTranslationLanguages();
    if ($new_translation) {
      // Make sure a new translation does not appear as existing yet.       unset($translations[$form_langcode]);
    }
    $is_translation = $new_translation || ($entity->language()->getId() != $entity_langcode);
    $has_translations = count($translations) > 1;

    // Adjust page title to specify the current language being edited, if we     // have at least one translation.     $languages = $this->languageManager->getLanguages();
    
$entity = clone $this->translations[$active_langcode];
      $previous_revision_id = $this->previousRevisionId[$active_langcode];
      $previous_untranslatable_field_value = $this->previousUntranslatableFieldValue[$previous_untranslatable_field_langcode];
    }

    // Check that after instantiating a new revision for the specified     // translation, we are resuming work from where we left the last time. If     // that is the case, the label generated for the previous revision should     // match the stored one.     if (!$entity->isNew()) {
      $previous_label = NULL;
      if (!$entity->isNewTranslation()) {
        $previous_label = $this->generateNewEntityLabel($entity$previous_revision_id);
        $latest_affected_revision_id = $this->storage->getLatestTranslationAffectedRevisionId($entity->id()$entity->language()->getId());
      }
      else {
        // Normally it would make sense to load the default revision in this         // case, however that would mean simulating here the logic that we need         // to test, thus "masking" possible flaws. To avoid that, we simply         // pretend we are starting from an earlier non translated revision.         // This ensures that the we can check that the merging logic is applied         // also when adding a new translation.         $latest_affected_revision_id = 1;
      }
Home | Imprint | This part of the site doesn't use cookies.