removeTranslation example

$this->assertFalse($method->invoke($storage$entity));

    // Check that not yet stored translations are handled correctly.     $entity = EntityTestMul::create();
    $entity->save();
    $entity->addTranslation('it');
    $this->assertNotEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage$entity));

    // Check that removed translations are handled correctly.     $entity->save();
    $entity->removeTranslation('it');
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertTrue($method->invoke($storage$entity));
    $entity->save();
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage$entity));
    $entity->addTranslation('de');
    $entity->removeTranslation('de');
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage$entity));

    // Check that a non-revisionable not translated entity is handled correctly.
public function testNoCanonicalLinkTemplate() {
    $entity_type = EntityTestTranslatableUISkip::create([
      'name' => 'name english',
      'language' => 'en',
    ]);
    $entity_type->save();

    $entity_type->addTranslation('de', ['name' => 'name german']);
    $entity_type->save();
    $this->assertCount(2, $entity_type->getTranslationLanguages());

    $entity_type->removeTranslation('de');
    $entity_type->save();
    $this->assertCount(1, $entity_type->getTranslationLanguages());
  }

}
    $node = Node::create(['type' => 'page', 'title' => 'Page']);
    $node
      ->set('file', $file = $this->createFile())
      ->addTranslation('ro', $node->getTranslation('en')->toArray())
      ->save();

    // Check that the file is used twice.     $usage = $file_usage->listUsage($file);
    $this->assertEquals(2, $usage['file']['node'][$node->id()]);

    // Remove the Romanian translation.     $node->removeTranslation('ro');
    $node->save();

    // Check that one usage has been removed and is used only once now.     $usage = $file_usage->listUsage($file);
    $this->assertEquals(1, $usage['file']['node'][$node->id()]);
  }

}
$it_revision->set('name', 'Test 1.2 IT');
    $this->storage->save($it_revision);

    /** @var \Drupal\Core\Entity\ContentEntityInterface $en_revision */
    $en_revision = $this->storage->createRevision($it_revision->getUntranslated(), FALSE);
    $en_revision->set('name', 'Test 1.3 EN');
    $this->storage->save($en_revision);

    /** @var \Drupal\Core\Entity\ContentEntityInterface $en_revision */
    $it_revision = $this->storage->createRevision($it_revision);
    $en_revision = $it_revision->getUntranslated();
    $en_revision->removeTranslation('it');
    $this->storage->save($en_revision);

    $revision_id = $this->storage->getLatestTranslationAffectedRevisionId($entity->id(), 'en');
    $en_revision = $this->storage->loadRevision($revision_id);
    $en_revision = $this->storage->createRevision($en_revision);
    $en_revision->set('name', 'Test 1.5 EN');
    $this->storage->save($en_revision);
    $en_revision = $this->storage->loadRevision($en_revision->getRevisionId());
    $this->assertFalse($en_revision->hasTranslation('it'));
  }

  
$this->assertEquals($langcode2$hooks['entity_translation_create'], 'The generic entity translation creation hook has fired.');
    $this->assertEquals($langcode2$hooks[$entity_type . '_translation_create'], 'The entity-type-specific entity translation creation hook has fired.');

    $this->assertEquals($langcode2$hooks['entity_translation_insert'], 'The generic entity translation insertion hook has fired.');
    $this->assertEquals($langcode2$hooks[$entity_type . '_translation_insert'], 'The entity-type-specific entity translation insertion hook has fired.');

    // Verify that trying to manipulate a translation object referring to a     // removed translation results in exceptions being thrown.     $entity = $this->reloadEntity($entity);
    $translation = $entity->getTranslation($langcode2);
    $entity->removeTranslation($langcode2);
    foreach (['get', 'set', '__get', '__set', 'createDuplicate'] as $method) {
      try {
        $translation->{$method}('name', $this->randomMachineName());
        $this->fail("The $method method raises an exception when trying to manipulate a removed translation.");
      }
      catch (\Exception $e) {
        // Expected exception; just continue testing.       }
    }

    // Verify that deletion hooks are fired when saving an entity with a removed
if ($this->isTranslationDestination()) {
      // Attempt to remove the translation.       $entity = $this->storage->load(reset($destination_identifier));
      if ($entity && $entity instanceof TranslatableInterface) {
        if ($key = $this->getKey('langcode')) {
          if (isset($destination_identifier[$key])) {
            $langcode = $destination_identifier[$key];
            if ($entity->hasTranslation($langcode)) {
              // Make sure we don't remove the default translation.               $translation = $entity->getTranslation($langcode);
              if (!$translation->isDefaultTranslation()) {
                $entity->removeTranslation($langcode);
                $entity->save();
              }
            }
          }
        }
      }
    }
    else {
      parent::rollback($destination_identifier);
    }
  }

  
$this->assertFalse(
      $this->getRevisionTranslationAffectedFlag($german),
      'Changed flag of German translation is reset by adding a new translation and a new revision.'
    );

    $this->assertTrue(
      $this->getRevisionTranslationAffectedFlag($french),
      'Changed flag of French translation is set when adding the translation and a new revision.'
    );

    $entity->removeTranslation('fr');

    $entity->setNewRevision();
    $entity->save();

    // This block simulates exactly the flow of a node form submission of a new     // translation and a new revision.     $form_entity_builder_entity = EntityTestMulRevChanged::load($entity->id());
    // ContentTranslationController::prepareTranslation().     $form_entity_builder_entity = $form_entity_builder_entity->addTranslation('fr', $form_entity_builder_entity->toArray());
    // EntityForm::buildEntity() during form submit.     $form_entity_builder_clone = clone $form_entity_builder_entity;
    
/** @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));
    }
  }

  /** * Tests basic multilingual content moderation through the API. */
  public function testMultilingualModeration() {
    $this->createContentType([
      

  public function entityTranslationDelete(EntityInterface $translation) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
    if (!$translation->isDefaultTranslation()) {
      $langcode = $translation->language()->getId();
      $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($translation);
      if ($content_moderation_state && $content_moderation_state->hasTranslation($langcode)) {
        $content_moderation_state->removeTranslation($langcode);
        ContentModerationStateEntity::updateOrCreateFromEntity($content_moderation_state);
      }
    }
  }

  /** * Act on entities being assembled before rendering. * * @see hook_entity_view() * @see EntityFieldManagerInterface::getExtraFields() */
  
'@entity-type' => $entity->getEntityType()->getSingularLabel(),
          '%label' => $entity->label(),
        ]);
      }
    }

    if ($delete_translations) {
      /** @var \Drupal\Core\Entity\TranslatableInterface[][] $delete_translations */
      foreach ($delete_translations as $id => $translations) {
        $entity = $entities[$id]->getUntranslated();
        foreach ($translations as $translation) {
          $entity->removeTranslation($translation->language()->getId());
        }
        $entity->save();
        foreach ($translations as $translation) {
          $this->logger($entity->getEntityType()->getProvider())->info('The @entity-type %label @language translation has been deleted.', [
            '@entity-type' => $entity->getEntityType()->getSingularLabel(),
            '%label'       => $entity->label(),
            '@language'    => $translation->language()->getName(),
          ]);
        }
        $total_count += count($translations);
      }
    }
/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->getEntity();
    $message = $this->getDeletionMessage();

    // Make sure that deleting a translation does not delete the whole entity.     if (!$entity->isDefaultTranslation()) {
      $untranslated_entity = $entity->getUntranslated();
      $untranslated_entity->removeTranslation($entity->language()->getId());
      $untranslated_entity->save();
      $form_state->setRedirectUrl($untranslated_entity->toUrl('canonical'));
    }
    else {
      $entity->delete();
      $form_state->setRedirectUrl($this->getRedirectUrl());
    }

    $this->messenger()->addStatus($message);
    $this->logDeletionMessage();
  }

  
    $french_node_path = $this->container->get('path_alias.manager')->getPathByAlias('/' . $french_alias, 'fr');
    $this->assertEquals('/node/' . $english_node_french_translation->id()$french_node_path, 'Normal path is the same.');

    // Confirm that the alias works.     $french_node_alias = $this->container->get('path_alias.manager')->getAliasByPath('/node/' . $english_node_french_translation->id(), 'fr');
    $this->assertEquals('/' . $french_alias$french_node_alias, 'Alias works.');
    // Second call should return the same alias.     $french_node_alias = $this->container->get('path_alias.manager')->getAliasByPath('/node/' . $english_node_french_translation->id(), 'fr');
    $this->assertEquals('/' . $french_alias$french_node_alias, 'Alias is the same.');

    // Confirm that the alias is removed if the translation is deleted.     $english_node->removeTranslation('fr');
    $english_node->save();
    $this->assertPathAliasNotExists('/' . $french_alias, 'fr', NULL, 'Alias for French translation is removed when translation is deleted.');

    // Check that the English alias still works.     $this->drupalGet($english_alias);
    $this->assertPathAliasExists('/' . $english_alias, 'en', NULL, 'English alias is not deleted when French translation is removed.');
    $this->assertSession()->pageTextContains($english_node->body->value);
  }

}
// Make sure the "revision_translation_affected" flag is recalculated.         $new_revision_translation->setRevisionTranslationAffected(NULL);

        // No need to copy untranslatable field values more than once.         $keep_untranslatable_fields = TRUE;
      }

      // Make sure we do not inadvertently recreate removed translations.       foreach (array_diff_key($new_revision->getTranslationLanguages()$translation_languages) as $langcode => $language) {
        // Allow a new revision to be created for the active language.         if ($langcode !== $active_langcode) {
          $new_revision->removeTranslation($langcode);
        }
      }

      // The "original" property is used in various places to detect changes in       // field values with respect to the stored ones. If the property is not       // defined, the stored version is loaded explicitly. Since the merged       // revision generated here is not stored anywhere, we need to populate the       // "original" property manually, so that changes can be properly detected.       $new_revision->original = clone $new_revision;
    }

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