/** * Populates target values with the source values. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity being translated. * @param \Drupal\Core\Language\LanguageInterface $source * The language to be used as source. * @param \Drupal\Core\Language\LanguageInterface $target * The language to be used as target. */ public function prepareTranslation(ContentEntityInterface $entity, LanguageInterface $source, LanguageInterface $target) { /* @var \Drupal\Core\Entity\ContentEntityInterface $source_translation */ $source_translation = $entity->getTranslation($source->getId()); $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->entityManager()->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); }
/** * Form submission handler for ContentTranslationHandler::entityFormAlter(). * * Updates metadata fields, which should be updated only after the validation * has run and before the entity is saved. */ function entityFormSubmit($form, FormStateInterface $form_state) { /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */ $form_object = $form_state->getFormObject(); /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $form_object->getEntity(); // ContentEntityForm::submit will update the changed timestamp on submit // after the entity has been validated, so that it does not break the // EntityChanged constraint validator. The content translation metadata // field for the changed timestamp does not have such a constraint defined // at the moment, but it is correct to update it's value in a submission // handler as well and have the same logic like in the Form API. if ($entity->hasField('content_translation_changed')) { $metadata = $this->manager->getTranslationMetadata($entity); $metadata->setChangedTime(REQUEST_TIME); } }
/** * Entity builder method. * * @param string $entity_type * The type of the entity. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity whose form is being built. * * @see \Drupal\content_translation\ContentTranslationHandler::entityFormAlter() */ public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) { $form_object = $form_state->getFormObject(); $form_langcode = $form_object->getFormLangcode($form_state); $values =& $form_state->getValue('content_translation', array()); $metadata = $this->manager->getTranslationMetadata($entity); $metadata->setAuthor(!empty($values['uid']) ? User::load($values['uid']) : User::load(0)); $metadata->setPublished(!empty($values['status'])); $metadata->setCreatedTime(!empty($values['created']) ? strtotime($values['created']) : REQUEST_TIME); $metadata->setChangedTime(REQUEST_TIME); $source_langcode = $this->getSourceLangcode($form_state); if ($source_langcode) { $metadata->setSource($source_langcode); } $metadata->setOutdated(!empty($values['outdated'])); if (!empty($values['retranslate'])) { $this->retranslate($entity, $form_langcode); } }