hasTranslationChanges example

    $entity = $this->updateEntity($entity$row);
    $entity->isDefaultRevision(TRUE);
    if ($entity instanceof EntityChangedInterface && $entity instanceof ContentEntityInterface) {
      // If we updated any untranslatable fields, update the timestamp for the       // other translations.       /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\Core\Entity\EntityChangedInterface $entity */
      foreach ($entity->getTranslationLanguages() as $langcode => $language) {
        // If we updated an untranslated field, then set the changed time for         // for all translations to match the current row that we are saving.         // In this context, getChangedTime() should return the value we just         // set in the updateEntity() call above.         if ($entity->getTranslation($langcode)->hasTranslationChanges()) {
          $entity->getTranslation($langcode)->setChangedTime($entity->getChangedTime());
        }
      }
    }
    return $entity;
  }

  /** * {@inheritdoc} */
  protected function updateEntity(EntityInterface $entity, Row $row) {
    
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->container->get('entity_type.manager')
      ->getStorage('entity_test_mulrev_changed_rev');
    /** @var \Drupal\entity_test\Entity\EntityTestMulRevChangedWithRevisionLog $entity */
    $entity = $storage->create([
      'name' => $this->randomString(),
    ]);
    $entity->setRevisionUserId($user1->id());
    $entity->save();

    $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes after the entity has been saved.');

    // Update the revision metadata fields and the changed field, which should     // be skipped from checking for changes in     // ContentEntityBase::hasTranslationChanges().     $entity_previous_rev_id = $entity->getRevisionId();
    // Revision metadata field revision_timestamp.     $entity->setRevisionCreationTime(time() + 1);
    // Revision metadata field revision_uid.     $entity->setRevisionUserId($user2->id());
    // Revision metadata field revision_log.     $entity->setRevisionLogMessage('test');
    
      // of the entity has changed. This detection does not run on new entities       // and will be turned off if the changed timestamp is set manually before       // save, for example during migrations or by using       // \Drupal\content_translation\ContentTranslationMetadataWrapperInterface::setChangedTime().       /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = $this->getEntity();
      /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
      $original = $entity->original;
      $langcode = $entity->language()->getId();
      if (!$entity->isNew() && $original && $original->hasTranslation($langcode)) {
        $original_value = $original->getTranslation($langcode)->get($this->getFieldDefinition()->getName())->value;
        if ($this->value == $original_value && $entity->hasTranslationChanges()) {
          $this->value = REQUEST_TIME;
        }
      }
    }
  }

}
$synchronized_properties = $this->getSynchronizedPropertiesByField($entity->getFieldDefinitions());
    if (!$synchronized_properties) {
      return;
    }

    /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
    $original = $this->getOriginalEntity($entity);
    $original_translation = $this->getOriginalTranslation($entity$original);
    if ($this->hasSynchronizedPropertyChanges($entity$original_translation$synchronized_properties)) {
      if ($entity->isDefaultTranslationAffectedOnly()) {
        foreach ($entity->getTranslationLanguages(FALSE) as $langcode => $language) {
          if ($entity->getTranslation($langcode)->hasTranslationChanges()) {
            $this->context->addViolation($constraint->defaultTranslationMessage);
            break;
          }
        }
      }
      else {
        $this->context->addViolation($constraint->defaultRevisionMessage);
      }
    }
  }

  
$languages = $entity->getTranslationLanguages();
      foreach ($languages as $langcode => $language) {
        $translation = $entity->getTranslation($langcode);
        $current_affected = $translation->isRevisionTranslationAffected();
        if (!isset($current_affected) || ($entity->isNewRevision() && !$translation->isRevisionTranslationAffectedEnforced())) {
          // When setting the revision translation affected flag we have to           // explicitly set it to not be enforced. By default it will be           // enforced automatically when being set, which allows us to determine           // if the flag has been already set outside the storage in which case           // we should not recompute it.           // @see \Drupal\Core\Entity\ContentEntityBase::setRevisionTranslationAffected().           $new_affected = $translation->hasTranslationChanges() ? TRUE : NULL;
          $translation->setRevisionTranslationAffected($new_affected);
          $translation->setRevisionTranslationAffectedEnforced(FALSE);
        }
      }
    }
  }

  /** * Ensures integer entity key values are valid. * * The identifier sanitization provided by this method has been introduced * as Drupal used to rely on the database to facilitate this, which worked * correctly with MySQL but led to errors with other DBMS such as PostgreSQL. * * @param array $ids * The entity key values to verify. * @param string $entity_key * (optional) The entity key to sanitize values for. Defaults to 'id'. * * @return array * The sanitized list of entity key values. */
    // only case where changes in pending revisions are acceptable is when     // untranslatable fields affect only the default translation, in which case     // a pending revision contains only one affected translation. Even in this     // case, multiple translations would be affected in a single revision, if we     // allowed changes to untranslatable fields while editing non-default     // translations, so that is forbidden too. For the same reason, when changes     // to untranslatable fields affect all translations, we can only allow them     // in default revisions.     if ($this->hasUntranslatableFieldsChanges($entity)) {
      if ($entity->isDefaultTranslationAffectedOnly()) {
        foreach ($entity->getTranslationLanguages(FALSE) as $langcode => $language) {
          if ($entity->getTranslation($langcode)->hasTranslationChanges()) {
            $this->context->addViolation($constraint->defaultTranslationMessage);
            break;
          }
        }
      }
      else {
        $this->context->addViolation($constraint->defaultRevisionMessage);
      }
    }
  }

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