isDefaultRevision example


  public function validate($entity, Constraint $constraint) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    /** @var \Drupal\Core\Entity\Plugin\Validation\Constraint\EntityUntranslatableFieldsConstraint $constraint */

    // Untranslatable field restrictions apply only to revisions of multilingual     // entities.     if ($entity->isNew() || !$entity->isTranslatable() || !$entity->getEntityType()->isRevisionable()) {
      return;
    }
    if ($entity->isDefaultRevision() && !$entity->isDefaultTranslationAffectedOnly()) {
      return;
    }

    // To avoid unintentional reverts and data losses, we forbid changes to     // untranslatable fields in pending revisions for multilingual entities. The     // 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
/** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */
    if (!$entity->isNew() && !$entity->isSyncing()) {
      // Force a new revision if the entity is not replicating.       $entity->setNewRevision(TRUE);

      // All entities in the non-default workspace are pending revisions,       // regardless of their publishing status. This means that when creating       // a published pending revision in a non-default workspace it will also be       // a published pending revision in the default workspace, however, it will       // become the default revision only when it is replicated to the default       // workspace.       $entity->isDefaultRevision(FALSE);
    }

    // Track the workspaces in which the new revision was saved.     if (!$entity->isSyncing()) {
      $field_name = $entity_type->getRevisionMetadataKey('workspace');
      $entity->{$field_name}->target_id = $this->workspaceManager->getActiveWorkspace()->id();
    }

    // When a new published entity is inserted in a non-default workspace, we     // actually want two revisions to be saved:     // - An unpublished default revision in the default ('live') workspace.
$node_type->save();

    $node = Node::create([
      'type' => 'page',
      'title' => 'test title',
    ]);
    $node->save();

    // Creates the first revision, which is set as default.     $revision = clone $node;
    $revision->setNewRevision(TRUE);
    $revision->isDefaultRevision(TRUE);
    $revision->save();

    // Creates the second revision, which is not set as default.     $revision2 = clone $node;
    $revision2->setNewRevision(TRUE);
    $revision2->isDefaultRevision(FALSE);
    $revision2->save();

    $view = Views::getView('base_and_revision');
    $view->execute();

    

  public function toUrl($rel = 'canonical', array $options = []) {
    if ($this->id() === NULL) {
      throw new EntityMalformedException(sprintf('The "%s" entity cannot have a URI as it does not have an ID', $this->getEntityTypeId()));
    }

    // The links array might contain URI templates set in annotations.     $link_templates = $this->linkTemplates();

    // Links pointing to the current revision point to the actual entity. So     // instead of using the 'revision' link, use the 'canonical' link.     if ($rel === 'revision' && $this instanceof RevisionableInterface && $this->isDefaultRevision()) {
      $rel = 'canonical';
    }

    if (isset($link_templates[$rel])) {
      $route_parameters = $this->urlRouteParameters($rel);
      $route_name = "entity.{$this->entityTypeId}." . str_replace(['-', 'drupal:']['_', '']$rel);
      $uri = new Url($route_name$route_parameters);
    }
    else {
      $bundle = $this->bundle();
      // A bundle-specific callback takes precedence over the generic one for
    $pending_child1_title = $this->randomMachineName(8);
    $child1_pending_revision = $storage->createRevision($child1, FALSE);
    $child1_pending_revision->set('title', $pending_child1_title);
    $child1_pending_revision->set('link', [['uri' => 'internal:/#test']]);

    $violations = $child1_pending_revision->validate();
    $this->assertEmpty($violations);
    $child1_pending_revision->save();

    $storage->resetCache();
    $child1_pending_revision = $storage->loadRevision($child1_pending_revision->getRevisionId());
    $this->assertFalse($child1_pending_revision->isDefaultRevision());
    $this->assertEquals($pending_child1_title$child1_pending_revision->getTitle());
    $this->assertEquals('/#test', $child1_pending_revision->getUrlObject()->toString());

    // Check that saving a pending revision does not affect the menu tree.     $menu_tree = \Drupal::menuTree()->load('menu_test', new MenuTreeParameters());
    $parent_link = reset($menu_tree);
    $this->assertEquals($default_root_1_title$parent_link->link->getTitle());
    $this->assertEquals('/#root_1', $parent_link->link->getUrlObject()->toString());

    $child1_link = reset($parent_link->subtree);
    $this->assertEquals($default_child1_title$child1_link->link->getTitle());
    

  public function validate($value, Constraint $constraint) {
    /** @var \Drupal\content_translation\Plugin\Validation\Constraint\ContentTranslationSynchronizedFieldsConstraint $constraint */
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $value;
    if ($entity->isNew() || !$entity->getEntityType()->isRevisionable()) {
      return;
    }
    // When changes to untranslatable fields are configured to affect all     // revision translations, we always allow changes in default revisions.     if ($entity->isDefaultRevision() && !$entity->isDefaultTranslationAffectedOnly()) {
      return;
    }
    $entity_type_id = $entity->getEntityTypeId();
    if (!$this->contentTranslationManager->isEnabled($entity_type_id$entity->bundle())) {
      return;
    }
    $synchronized_properties = $this->getSynchronizedPropertiesByField($entity->getFieldDefinitions());
    if (!$synchronized_properties) {
      return;
    }

    
/** * Test cannot delete default revision. * * @covers \Drupal\Core\Entity\EntityAccessControlHandler::checkAccess */
  public function testAccessDeleteDefault(): void {
    /** @var \Drupal\entity_test\Entity\EntityTestRevPub $entity */
    $entity = EntityTestRevPub::create();
    $entity->setName('delete revision');
    $entity->save();

    $entity->isDefaultRevision(TRUE);
    $entity->setPublished();
    $entity->setNewRevision();
    $entity->save();
    $revisionId = $entity->getRevisionId();

    $entity->isDefaultRevision(FALSE);
    $entity->setUnpublished();
    $entity->setNewRevision();
    $entity->save();

    // Reload the entity.
use Symfony\Component\Validator\ConstraintValidator;

/** * Constraint validator for changing the menu settings in pending revisions. */
class MenuSettingsConstraintValidator extends ConstraintValidator {

  /** * {@inheritdoc} */
  public function validate($entity, Constraint $constraint) {
    if (isset($entity) && !$entity->isNew() && !$entity->isDefaultRevision()) {
      $defaults = menu_ui_get_menu_link_defaults($entity);

      // If the menu UI entity builder is not present and the menu property has       // not been set, do not attempt to validate the menu settings since they       // are not being modified.       if (!$values = $entity->menu) {
        return;
      }

      if (trim($values['title']) && !empty($values['menu_parent'])) {
        [$menu_name$parent] = explode(':', $values['menu_parent'], 2);
        
'max-age' => $entity->getCacheMaxAge(),
      ],
    ];

    // Add the default #theme key if a template exists for it.     if ($this->themeRegistry->getRuntime()->has($this->entityTypeId)) {
      $build['#theme'] = $this->entityTypeId;
    }

    // Cache the rendered output if permitted by the view mode and global entity     // type configuration.     if ($this->isViewModeCacheable($view_mode) && !$entity->isNew() && $entity->isDefaultRevision() && $this->entityType->isRenderCacheable()) {
      $build['#cache'] += [
        'keys' => [
          'entity_view',
          $this->entityTypeId,
          $entity->id(),
          $view_mode,
        ],
        'bin' => $this->cacheBin,
      ];

      if ($entity instanceof TranslatableDataInterface && count($entity->getTranslationLanguages()) > 1) {
        
$user = $this->createUser();
    $this->drupalLogin($user);

    /** @var \Drupal\entity_test_revlog\Entity\EntityTestWithRevisionLog $entity */
    $entity = EntityTestWithRevisionLog::create([
      'type' => 'entity_test_revlog',
      'name' => 'revert',
    ]);

    $date = new \DateTime('11 January 2009 4:00:00pm');
    $entity->setRevisionCreationTime($date->getTimestamp());
    $entity->isDefaultRevision(TRUE);
    $entity->setNewRevision();
    $entity->save();

    $revisionCreationTime = $date->modify('+1 hour')->getTimestamp();
    $entity->setRevisionCreationTime($revisionCreationTime);
    $entity->setRevisionUserId(0);
    $entity->isDefaultRevision(FALSE);
    $entity->setNewRevision();
    $entity->save();
    $targetRevertRevisionId = $entity->getRevisionId();

    
$node->save();

    // Revision 3.     $node = $storage->load($node->id());
    $node->setNewRevision(TRUE);
    $node->save();

    // Revision 4.     // Trigger translation changes in order to show the revision.     $node = $storage->load($node->id());
    $node->setTitle($this->randomString());
    $node->isDefaultRevision(FALSE);
    $node->setNewRevision(TRUE);
    $node->save();

    // Revision 5.     $node = $storage->load($node->id());
    $node->isDefaultRevision(FALSE);
    $node->setNewRevision(TRUE);
    $node->save();

    $node_id = $node->id();

    
$english_node = Node::create([
      'type' => 'example',
      'title' => 'Test title',
    ]);
    // Revision 1 (en).     $english_node
      ->setUnpublished()
      ->save();
    $this->assertEquals('draft', $english_node->moderation_state->value);
    $this->assertFalse($english_node->isPublished());
    $this->assertTrue($english_node->isDefaultRevision());
    $this->assertModerationState($english_node->getRevisionId()$english_node->language()->getId(), 'draft');

    // Revision 2 (fr)     $french_node = $english_node->addTranslation('fr', ['title' => 'French title']);
    $french_node->moderation_state->value = 'published';
    $french_node->save();
    $this->assertTrue($french_node->isPublished());
    $this->assertTrue($french_node->isDefaultRevision());
    $this->assertModerationState($french_node->getRevisionId()$french_node->language()->getId(), 'published');

    // Revision 3 (fr)
'type' => 'test',
    ]);
    $node->save();

    $translated = $node->addTranslation('fr', ['title' => 'French translation - default revision']);
    $translated->title = 'French translation - default revision';
    $translated->save();

    /** @var \Drupal\node\NodeInterface $pending */
    $pending = clone $node;
    $pending->setNewRevision(TRUE);
    $pending->isDefaultRevision(FALSE);
    $pending->title = 'Original translation - pending revision';
    $pending->save();

    /** @var \Drupal\node\NodeInterface $pending_translated */
    $pending_translated = clone $translated;
    $pending_translated->setNewRevision(TRUE);
    $pending_translated->isDefaultRevision(FALSE);
    $pending_translated->title = 'French translation - pending revision';
    $pending_translated->save();

    $view = Views::getView('test_latest_translation_affected_revision_filter');
    
$translation = $entity->addTranslation('de');
    $translation->setNewRevision();
    $translation->save();

    $entity = $this->reloadEntity($entity);

    $this->assertTrue($entity->hasTranslation('de'));

    $entity = $storage->loadRevision($old_rev_id);

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

    $entity = $this->reloadEntity($entity);

    $this->assertFalse($entity->hasTranslation('de'));
  }

  /** * Tests the translation values when saving a pending revision. */
  public function testTranslationValuesWhenSavingPendingRevisions() {
    
return new static(
      $container->get('entity_type.manager')
    );
  }

  /** * {@inheritdoc} */
  public function validate($value, Constraint $constraint) {
    $entity = !empty($value->getParent()) ? $value->getEntity() : NULL;

    if ($entity && !$entity->isNew() && !$entity->isDefaultRevision()) {
      /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
      $original = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
      $entity_langcode = $entity->language()->getId();

      // Only add the violation if the current translation does not have the       // same path alias.       if ($original->hasTranslation($entity_langcode)) {
        if ($value->alias != $original->getTranslation($entity_langcode)->path->alias) {
          $this->context->addViolation($constraint->message);
        }
      }
    }
Home | Imprint | This part of the site doesn't use cookies.