setPublished example


  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', []);

    $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->setOutdated(!empty($values['outdated']));
    if (!empty($values['retranslate'])) {
      $this->retranslate($entity$form_langcode);
    }
  }

  /** * Form validation handler for ContentTranslationHandler::entityFormAlter(). * * Validates the submitted content translation metadata. */
/** @var \Drupal\Core\Entity\RevisionableStorageInterface $entityStorage */
    $entityStorage = \Drupal::entityTypeManager()->getStorage('block_content');

    $loadRevisionId = NULL;
    if (!$isLatest) {
      // Save a historical revision, then setup for a new revision to be saved.       $this->blockEntity->save();
      $loadRevisionId = $this->blockEntity->getRevisionId();
      $this->blockEntity = $entityStorage->createRevision($this->blockEntity);
    }

    $published ? $this->blockEntity->setPublished() : $this->blockEntity->setUnpublished();
    $reusable ? $this->blockEntity->setReusable() : $this->blockEntity->setNonReusable();

    $user = User::create([
      'name' => 'Someone',
      'mail' => 'hi@example.com',
    ]);

    if ($permissions) {
      foreach ($permissions as $permission) {
        $this->role->grantPermission($permission);
      }
      
    $comment = Comment::create([
      'comment_body' => [
        'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
        'format' => 'plain_text',
      ],
      'entity_id' => $this->commentedEntity->id(),
      'entity_type' => 'entity_test',
      'field_name' => 'comment',
    ]);
    $comment->setSubject('Llama')
      ->setOwnerId($this->account->id())
      ->setPublished()
      ->setCreatedTime(123456789)
      ->setChangedTime(123456789);
    $comment->save();

    return $comment;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    
// Unpublish the top book page and confirm that the created book title is     // not displayed for anonymous.     $this->book->setUnpublished();
    $this->book->save();

    $this->drupalGet('book');
    $this->assertSession()->pageTextNotContains($this->book->label());

    // Publish the top book page and unpublish a page in the book and confirm     // that the created book title is displayed for anonymous.     $this->book->setPublished();
    $this->book->save();
    $nodes[0]->setUnpublished();
    $nodes[0]->save();

    $this->drupalGet('book');
    $this->assertSession()->pageTextContains($this->book->label());

    // Unpublish the top book page and confirm that the created book title is     // displayed for user which has 'view own unpublished content' permission.     $this->drupalLogin($this->bookAuthor);
    $this->book->setUnpublished();
    
// Create a "Camelids" node type.       NodeType::create([
        'name' => 'Camelids',
        'type' => 'camelids',
      ])->save();
    }

    // Create a "Llama" node.     $node = Node::create(['type' => 'camelids']);
    $node->setTitle('Llama')
      ->setOwnerId(static::$auth ? $this->account->id() : 0)
      ->setPublished()
      ->setCreatedTime(123456789)
      ->setChangedTime(123456789)
      ->setRevisionCreationTime(123456789)
      ->set('path', '/llama')
      ->save();

    return $node;
  }

  /** * {@inheritdoc} */
'field_test_node' => [
        'entity' => $node,
      ],
    ]);

    $errors = $entity->validate();
    $this->assertCount(1, $errors);
    $this->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $title])$errors[0]->getMessage());
    $this->assertEquals('field_test_node.0.entity', $errors[0]->getPropertyPath());

    // Publish the node and try again.     $node->setPublished();
    $errors = $entity->validate();
    $this->assertCount(0, $errors);

    // Test with a mix of valid and invalid nodes.     $unsaved_unpublished_node_title = $this->randomString();
    $unsaved_unpublished_node = Node::create([
      'title' => $unsaved_unpublished_node_title,
      'type' => 'node',
      'status' => NodeInterface::NOT_PUBLISHED,
    ]);

    
    // reference field blocks, test that the cache tags of the referenced entity     // are still bubbled to page cache.     $referencing_node_url = $referencing_node->toUrl();
    $this->verifyPageCacheContainsTags($referencing_node_url, 'MISS');
    $this->verifyPageCacheContainsTags($referencing_node_url, 'HIT', $referenced_node->getCacheTags());
    // Since the referenced node is inaccessible, it should not appear on the     // referencing node.     $this->drupalGet($referencing_node_url);
    $assert_session->linkNotExists('The referenced node title');

    // Publish the referenced entity.     $referenced_node->setPublished()
      ->save();

    // Revisit the node with the reference field without clearing cache. Now     // that the referenced node is published, it should appear.     $this->verifyPageCacheContainsTags($referencing_node_url, 'MISS');
    $this->verifyPageCacheContainsTags($referencing_node_url, 'HIT', $referenced_node->getCacheTags());
    $this->drupalGet($referencing_node_url);
    $assert_session->linkExists('The referenced node title');
  }

  /** * Verify that when loading a given page, it's a page cache hit or miss. * * @param \Drupal\Core\Url $url * The page for this URL will be loaded. * @param string $hit_or_miss * 'HIT' if a page cache hit is expected, 'MISS' otherwise. * @param array|false $tags * When expecting a page cache hit, you may optionally specify an array of * expected cache tags. While FALSE, the cache tags will not be verified. * This tests whether all expected tags are in the page cache tags, not that * expected tags and page cache tags are identical. */
      if (!$entity->isSyncing()) {
        $update_default_revision = $entity->isNew()
          || $current_state->isDefaultRevisionState()
          || !$content_moderation_info->isDefaultRevisionPublished($entity);

        $entity->isDefaultRevision($update_default_revision);
      }

      // Update publishing status if it can be updated and if it needs updating.       $published_state = $current_state->isPublishedState();
      if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) {
        $published_state ? $entity->setPublished() : $entity->setUnpublished();
      }
    }
  }

  /** * {@inheritdoc} */
  public function generateSampleItems($count = 1) {
    // No sample items generated since the starting moderation state is always     // computed based on the default state of the associated workflow.   }

}
return $query;
  }

  /** * {@inheritdoc} */
  public function createNewEntity($entity_type_id$bundle$label$uid) {
    $comment = parent::createNewEntity($entity_type_id$bundle$label$uid);

    // In order to create a referenceable comment, it needs to published.     /** @var \Drupal\comment\CommentInterface $comment */
    $comment->setPublished();

    return $comment;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    // Mirror the conditions checked in buildEntityQuery().     if (!$this->currentUser->hasPermission('administer comments')) {
      
'create content translations' => TRUE,
        'access content' => TRUE,
      ]
    );
    $node->setUnpublished()->save();
    $this->drupalGet($node->toUrl('drupal:content-translation-overview'));
    $this->assertSession()->statusCodeEquals(403);
    $this->drupalLogout();

    // Ensure the 'Translate' local task does not show up anymore when disabling     // translations for a content type.     $node->setPublished()->save();
    user_role_change_permissions(
      Role::AUTHENTICATED_ID,
      [
        'administer content translation' => TRUE,
        'administer languages' => TRUE,
      ]
    );
    $this->drupalPlaceBlock('local_tasks_block');
    $this->drupalLogin($this->baseUser2);
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()->linkByHrefExists('node/' . $node->id() . '/translations');
    


  /** * Tests field item list translation support with unmoderated content. */
  public function testTranslationWithExistingUnmoderatedContent() {
    $node = Node::create([
      'title' => 'Published en',
      'langcode' => 'en',
      'type' => 'unmoderated',
    ]);
    $node->setPublished();
    $node->save();

    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'unmoderated');
    $workflow->save();

    $translation = $node->addTranslation('de');
    $translation->moderation_state = 'draft';
    $translation->save();

    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    
return $query;
  }

  /** * {@inheritdoc} */
  public function createNewEntity($entity_type_id$bundle$label$uid) {
    $term = parent::createNewEntity($entity_type_id$bundle$label$uid);

    // In order to create a referenceable term, it needs to published.     /** @var \Drupal\taxonomy\TermInterface $term */
    $term->setPublished();

    return $term;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    // Mirror the conditions checked in buildEntityQuery().     if (!$this->currentUser->hasPermission('administer taxonomy')) {
      
$post_file->save();

    // Create a "Llama" media item.     $media = Media::create([
      'bundle' => 'camelids',
      'field_media_file' => [
        'target_id' => $file->id(),
      ],
    ]);
    $media
      ->setName('Llama')
      ->setPublished()
      ->setCreatedTime(123456789)
      ->setOwnerId($this->account->id())
      ->setRevisionUserId($this->account->id())
      ->save();

    return $media;
  }

  /** * {@inheritdoc} */
  
    // revision to be created and should not update the default status of a     // revision. This is useful if changes are being made to entities or     // revisions which are not part of editorial updates triggered by normal     // content changes.     if (!$entity->isSyncing()) {
      $entity->setNewRevision(TRUE);
      $entity->isDefaultRevision($default_revision);
    }

    // Update publishing status if it can be updated and if it needs updating.     if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) {
      $published_state ? $entity->setPublished() : $entity->setUnpublished();
    }
  }

  /** * {@inheritdoc} */
  public function enforceRevisionsEntityFormAlter(array &$form, FormStateInterface $form_state$form_id) {
  }

  /** * {@inheritdoc} */

  protected function createEntity() {
    // Create a "Camelids" node type.     NodeType::create([
      'name' => 'Camelids',
      'type' => 'camelids',
    ])->save();

    // Create a "Llama" node.     $node = Node::create(['type' => 'camelids']);
    $node->setTitle('Llama')
      ->setPublished()
      ->save();

    return $node;
  }

  /** * {@inheritdoc} */
  protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) {
    return ['timezone'];
  }

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