hasPendingRevision example


    $entity->save();

    // Add a translation as a new revision.     $translated = $entity->addTranslation('de');
    $translated->moderation_state = 'published';
    $translated->setNewRevision(TRUE);

    // Test a scenario where the default revision exists with the default     // language in a published state and a non-default language in an unsaved     // state.     $this->assertFalse($this->moderationInformation->hasPendingRevision($translated));

    // Save the translation and assert there is no pending revision.     $translated->save();
    $this->assertFalse($this->moderationInformation->hasPendingRevision($translated));

    // Create a new draft for the translation and assert there is a pending     // revision.     $translated->moderation_state = 'draft';
    $translated->setNewRevision(TRUE);
    $translated->save();
    $this->assertTrue($this->moderationInformation->hasPendingRevision($translated));
  }

  public static function bundleFormRedirect(array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $form_state->getFormObject()->getEntity();

    $moderation_info = \Drupal::getContainer()->get('content_moderation.moderation_information');
    if ($moderation_info->hasPendingRevision($entity) && $entity->hasLinkTemplate('latest-version')) {
      $entity_type_id = $entity->getEntityTypeId();
      $form_state->setRedirect("entity.$entity_type_id.latest_version", [$entity_type_id => $entity->id()]);
    }
  }

}
/** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this->prophesize($entity_class);
    $entity->getCacheContexts()->willReturn([]);
    $entity->getCacheTags()->willReturn([]);
    $entity->getCacheMaxAge()->willReturn(0);
    if (is_subclass_of($entity_class, EntityOwnerInterface::class)) {
      $entity->getOwnerId()->willReturn($is_owner ? 42 : 3);
    }

    /** @var \Drupal\content_moderation\ModerationInformation $mod_info */
    $mod_info = $this->prophesize(ModerationInformation::class);
    $mod_info->hasPendingRevision($entity->reveal())->willReturn($has_pending_revision);

    $route = $this->prophesize(Route::class);

    $route->getOption('_content_moderation_entity_type')->willReturn($entity_type);

    $route_match = $this->prophesize(RouteMatch::class);
    $route_match->getParameter($entity_type)->willReturn($entity->reveal());

    $lrc = new LatestRevisionCheck($mod_info->reveal());

    /** @var \Drupal\Core\Access\AccessResult $result */
    

  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
    // This tab should not show up unless there's a reason to show it.     $entity = $this->loadEntity($route$route_match);
    if ($this->moderationInfo->hasPendingRevision($entity)) {
      // Check the global permissions first.       $access_result = AccessResult::allowedIfHasPermissions($account['view latest version', 'view any unpublished content']);
      if (!$access_result->isAllowed()) {
        // Check entity owner access.         $owner_access = AccessResult::allowedIfHasPermissions($account['view latest version', 'view own unpublished content']);
        $owner_access = $owner_access->andIf((AccessResult::allowedIf($entity instanceof EntityOwnerInterface && ($entity->getOwnerId() == $account->id()))));
        $access_result = $access_result->orIf($owner_access);
      }

      return $access_result->addCacheableDependency($entity);
    }

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