isDefaultTranslation example

// @todo Find a more reliable way to determine if a form element concerns a     // multilingual value.     if (!isset($ignored_types)) {
      $ignored_types = array_flip(['actions', 'value', 'hidden', 'vertical_tabs', 'token', 'details', 'link']);
    }

    /** @var \Drupal\Core\Entity\ContentEntityForm $form_object */
    $form_object = $form_state->getFormObject();
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $form_object->getEntity();
    $display_translatability_clue = !$entity->isDefaultTranslationAffectedOnly();
    $hide_untranslatable_fields = $entity->isDefaultTranslationAffectedOnly() && !$entity->isDefaultTranslation();
    $translation_form = $form_state->get(['content_translation', 'translation_form']);
    $display_warning = FALSE;

    // We use field definitions to identify untranslatable field widgets to be     // hidden. Fields that are not involved in translation changes checks should     // not be affected by this logic (the "revision_log" field, for instance).     $field_definitions = array_diff_key($entity->getFieldDefinitions()array_flip($this->getFieldsToSkipFromTranslationChangesCheck($entity)));

    foreach (Element::children($element) as $key) {
      if (!isset($element[$key]['#type'])) {
        $this->entityFormSharedElements($element[$key]$form_state$form);
      }
    // new default revision without reverting changes in other languages.     if (!$entity->isNew() && !$entity->isDefaultRevision() && $entity->isTranslatable() && $this->isAnyRevisionTranslated($entity)) {
      $active_langcode = $entity->language()->getId();
      $skipped_field_names = array_flip($this->getRevisionTranslationMergeSkippedFieldNames());

      // By default we copy untranslatable field values from the default       // revision, unless they are configured to affect only the default       // translation. This way we can ensure we always have only one affected       // translation in pending revisions. This constraint is enforced by       // EntityUntranslatableFieldsConstraintValidator.       if (!isset($keep_untranslatable_fields)) {
        $keep_untranslatable_fields = $entity->isDefaultTranslation() && $entity->isDefaultTranslationAffectedOnly();
      }

      /** @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);
        
public function rollback(array $destination_identifier) {
    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);
    }
  }
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $original_entity = $items->getEntity();

    $default = $this->moderationInformation->getOriginalState($entity);

    // If the entity already exists, grab the most recent revision and load it.     // The moderation state of the saved revision will be used to display the     // current state as well determine the appropriate transitions.     if (!$entity->isNew()) {
      /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */
      $original_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadRevision($entity->getLoadedRevisionId());
      if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) {
        $original_entity = $original_entity->getTranslation($entity->language()->getId());
      }
    }
    // For a new entity, ensure the moderation state of the original entity is     // always the default state. Despite the entity being unsaved, it may have     // previously been set to a new target state, for example previewed entities     // are retrieved from temporary storage with field values set.     else {
      $original_entity->set('moderation_state', $default->id());
    }

    

  protected function handleTranslationAccess(AccessResult $result$operation, AccountInterface $account) {
    $entity = $this->getEntity();
    // Access is always denied on non-default translations.     return $result->andIf(AccessResult::allowedIf(!($entity instanceof TranslatableInterface && !$entity->isDefaultTranslation())))->addCacheableDependency($entity);
  }

  /** * {@inheritdoc} */
  public function isApplicable(RefinableCacheableDependencyInterface $cacheability) {
    $default_section_storage = $this->getDefaultSectionStorage();
    $cacheability->addCacheableDependency($default_section_storage)->addCacheableDependency($this);
    // Check that overrides are enabled and have at least one section.     return $default_section_storage->isOverridable() && $this->isOverridden();
  }

  
public function buildForm(array $form, FormStateInterface $form_state$node_revision = NULL, $langcode = NULL) {
    $this->langcode = $langcode;
    $form = parent::buildForm($form$form_state$node_revision);

    // Unless untranslatable fields are configured to affect only the default     // translation, we need to ask the user whether they should be included in     // the revert process.     $default_translation_affected = $this->revision->isDefaultTranslationAffectedOnly();
    $form['revert_untranslated_fields'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Revert content shared among translations'),
      '#default_value' => $default_translation_affected && $this->revision->getTranslation($this->langcode)->isDefaultTranslation(),
      '#access' => !$default_translation_affected,
    ];

    return $form;
  }

  /** * {@inheritdoc} */
  protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
    $revert_untranslated_fields = (bool) $form_state->getValue('revert_untranslated_fields');
    
foreach ($this->selection as $id => $selected_langcodes) {
      $entity = $entities[$id];
      foreach ($selected_langcodes as $langcode) {
        $key = $id . ':' . $langcode;
        if ($entity instanceof TranslatableInterface) {
          $entity = $entity->getTranslation($langcode);
          $default_key = $id . ':' . $entity->getUntranslated()->language()->getId();

          // Build a nested list of translations that will be deleted if the           // entity has multiple translations.           $entity_languages = $entity->getTranslationLanguages();
          if (count($entity_languages) > 1 && $entity->isDefaultTranslation()) {
            $names = [];
            foreach ($entity_languages as $translation_langcode => $language) {
              $names[] = $language->getName();
              unset($items[$id . ':' . $translation_langcode]);
            }
            $items[$default_key] = [
              'label' => [
                '#markup' => $this->t('@label (Original translation) - <em>The following @entity_type translations will be deleted:</em>',
                  [
                    '@label' => $entity->label(),
                    '@entity_type' => $this->entityType->getSingularLabel(),
                  ]),
getCancelUrl as traitGetCancelUrl;
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form$form_state);

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->getEntity();
    if ($entity->isDefaultTranslation()) {
      if (count($entity->getTranslationLanguages()) > 1) {
        $languages = [];
        foreach ($entity->getTranslationLanguages() as $language) {
          $languages[] = $language->getName();
        }

        $form['deleted_translations'] = [
          '#theme' => 'item_list',
          '#title' => $this->t('The following @entity-type translations will be deleted:', [
            '@entity-type' => $entity->getEntityType()->getSingularLabel(),
          ]),
          
/** * {@inheritdoc} */
  protected function getDeletionMessage() {
    /** @var \Drupal\node\NodeInterface $entity */
    $entity = $this->getEntity();

    $node_type_storage = $this->entityTypeManager->getStorage('node_type');
    $node_type = $node_type_storage->load($entity->bundle());

    if (!$entity->isDefaultTranslation()) {
      return $this->t('@language translation of the @type %label has been deleted.', [
        '@language' => $entity->language()->getName(),
        '@type' => $node_type->label(),
        '%label' => $entity->label(),
      ]);
    }

    return $this->t('The @type %title has been deleted.', [
      '@type' => $node_type->label(),
      '%title' => $this->getEntity()->label(),
    ]);
  }
// If the revision identifier field is being populated with the original         // value, we need to make sure the "new revision" flag is reset         // accordingly.         if ($key === 'revision' && $this->getRevisionId() == $this->getLoadedRevisionId() && !$this->isNew()) {
          $this->newRevision = FALSE;
        }
      }
    }

    switch ($name) {
      case $this->langcodeKey:
        if ($this->isDefaultTranslation()) {
          // Update the default internal language cache.           $this->setDefaultLangcode();
          if (isset($this->translations[$this->defaultLangcode])) {
            $message = new FormattableMarkup('A translation already exists for the specified language (@langcode).', ['@langcode' => $this->defaultLangcode]);
            throw new \InvalidArgumentException($message);
          }
          $this->updateFieldLangcodes($this->defaultLangcode);
        }
        else {
          // @todo Allow the translation language to be changed. See           // https://www.drupal.org/node/2443989.


  /** * {@inheritdoc} */
  public function getOriginalState(ContentEntityInterface $entity) {
    $state = NULL;
    $workflow_type = $this->getWorkflowForEntity($entity)->getTypePlugin();
    if (!$entity->isNew() && !$this->isFirstTimeModeration($entity)) {
      /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */
      $original_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadRevision($entity->getLoadedRevisionId());
      if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) {
        $original_entity = $original_entity->getTranslation($entity->language()->getId());
      }
      if ($workflow_type->hasState($original_entity->moderation_state->value)) {
        $state = $workflow_type->getState($original_entity->moderation_state->value);
      }
    }
    return $state ?: $workflow_type->getInitialState($entity);
  }

  /** * Determines if this entity is being moderated for the first time. * * If the previous version of the entity has no moderation state, we assume * that means it predates the presence of moderation states. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity being moderated. * * @return bool * TRUE if this is the entity's first time being moderated, FALSE otherwise. */

  protected function getOriginalTranslation(ContentEntityInterface $entity, ContentEntityInterface $original) {
    // 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);
  }


  /** * {@inheritdoc} */
  public function delete() {
    parent::delete();
    $entity = $this->getEntity();

    // If a translation is deleted only decrement the file usage by one. If the     // default translation is deleted remove all file usages within this entity.     $count = $entity->isDefaultTranslation() ? 0 : 1;
    foreach ($this->referencedEntities() as $file) {
      \Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId()$entity->id()$count);
    }
  }

  /** * {@inheritdoc} */
  public function deleteRevision() {
    parent::deleteRevision();
    $entity = $this->getEntity();

    

      $entity = reset($entities);
      // If the entity type is translatable, ensure we return the proper       // translation object for the current context.       if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {
        // @see https://www.drupal.org/project/drupal/issues/2624770         $entity = $this->entityRepository->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']);
        // JSON:API always has only one method per route.         $method = $defaults[RouteObjectInterface::ROUTE_OBJECT]->getMethods()[0];
        if (in_array($method['PATCH', 'DELETE'], TRUE)) {
          $current_content_language = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
          if ($method === 'DELETE' && (!$entity->isDefaultTranslation() || $entity->language()->getId() !== $current_content_language)) {
            throw new MethodNotAllowedHttpException(['GET'], 'Deleting a resource object translation is not yet supported. See https://www.drupal.org/docs/8/modules/jsonapi/translations.');
          }
          if ($method === 'PATCH' && $entity->language()->getId() !== $current_content_language) {
            $available_translations = implode(', ', array_keys($entity->getTranslationLanguages()));
            throw new MethodNotAllowedHttpException(['GET']sprintf('The requested translation of the resource object does not exist, instead modify one of the translations that do exist: %s.', $available_translations));
          }
        }
      }
      return $entity;
    }
    return NULL;
  }
    try {
      $translation->{$langcode_key}->value = $langcode;
    }
    catch (\LogicException $e) {
      $this->fail('The translation language can be reassigned the same value.');
    }

    // Verify that changing the default translation flag causes an exception to     // be thrown.     foreach ($entity->getTranslationLanguages() as $t_langcode => $language) {
      $translation = $entity->getTranslation($t_langcode);
      $default = $translation->isDefaultTranslation();

      try {
        $translation->{$default_langcode_key}->value = $default;
      }
      catch (\LogicException $e) {
        $this->fail('The default translation flag can be reassigned the same value.');
      }

      try {
        $translation->{$default_langcode_key}->value = !$default;
        $this->fail('The default translation flag cannot be changed.');
      }
Home | Imprint | This part of the site doesn't use cookies.