setCreatedTime example

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

    return $media;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    
// Create a node with only the fifth level term.     $settings['field_views_testing_tags'][0]['target_id'] = $fifth->id();
    $this->nodes[] = $this->drupalCreateNode($settings);

    $this->view = Views::getView(self::$testViews[0]);

    // Fix the created date to match the expectations of the order by in the     // view. Node 1 should be the most recent node and node 6 should be the     // oldest.     $time = \Drupal::time();
    foreach ($this->nodes as $i => $node) {
      $node->setCreatedTime($time->getRequestTime() - $i)->save();
    }
  }

  /** * Tests the terms with depth filter. */
  public function testTermWithDepthFilter(): void {
    // Default view has an empty value for this filter, so all nodes should be     // returned.     $expected = [
      ['nid' => 1],
      [
'field_name' => 'comment',
        'entity_type' => 'node',
        'entity_id' => $this->node->id(),
      ]);
      $comment->setOwnerId(0);
      $comment->setSubject('Test comment ' . $i);
      $comment->comment_body->value = 'Test body ' . $i;
      $comment->comment_body->format = 'full_html';

      // Ensure comments are sorted in ascending order.       $time = REQUEST_TIME + ($this->defaultDisplayResults - $i);
      $comment->setCreatedTime($time);
      $comment->changed->value = $time;

      $comment->save();
    }

    // Store all the nodes just created to access their properties on the tests.     $this->commentsCreated = Comment::loadMultiple();

    // Sort created comments in descending order.     ksort($this->commentsCreated, SORT_NUMERIC);
  }

  
'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() {
    $self_url = Url::fromUri('base:/jsonapi/comment/comment/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    
$created_time = $metadata_source_translation->getCreatedTime();
    $changed_time = $metadata_source_translation->getChangedTime();
    $published = $metadata_source_translation->isPublished();
    $author = $metadata_source_translation->getAuthor();

    $this->assertEquals($created_time$metadata_target_translation->getCreatedTime(), 'Metadata created field has the same value for both translations.');
    $this->assertEquals($changed_time$metadata_target_translation->getChangedTime(), 'Metadata changed field has the same value for both translations.');
    $this->assertEquals($published$metadata_target_translation->isPublished(), 'Metadata published field has the same value for both translations.');
    $this->assertEquals($author->id()$metadata_target_translation->getAuthor()->id(), 'Metadata author field has the same value for both translations.');

    $metadata_target_translation->setCreatedTime(time() + 50);
    $metadata_target_translation->setChangedTime(time() + 50);
    $metadata_target_translation->setPublished(TRUE);
    $metadata_target_translation->setAuthor($this->editor);

    $this->assertEquals($created_time$metadata_target_translation->getCreatedTime(), 'Metadata created field correctly not updated');
    $this->assertEquals($changed_time$metadata_target_translation->getChangedTime(), 'Metadata changed field correctly not updated');
    $this->assertEquals($published$metadata_target_translation->isPublished(), 'Metadata published field correctly not updated');
    $this->assertEquals($author->id()$metadata_target_translation->getAuthor()->id(), 'Metadata author field correctly not updated');
  }

  /** * Tests setting translatable metadata fields. */
      NodeType::create([
        'name' => 'Camelids',
        'type' => 'camelids',
      ])->save();
    }

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

    return $node;
  }

  /** * {@inheritdoc} */
  
// Create a node with only the third level term.     $settings['field_views_testing_tags'][0]['target_id'] = $third->id();
    $this->nodes[] = $this->drupalCreateNode($settings);

    $this->view = Views::getView(self::$testViews[0]);

    // Fix the created date to match the expectations of the order by in the     // view. Node 1 should be the most recent node and node 6 should be the     // oldest.     $request_time = \Drupal::time()->getRequestTime();
    foreach ($this->nodes as $i => $node) {
      $node->setCreatedTime($request_time - $i)->save();
    }
  }

  /** * Tests the terms with depth filter. */
  public function testTermWithDepthFilter(): void {
    // Default view has an empty value for this filter, so all nodes should be     // returned.     $expected = [
      ['nid' => 1],
      [
// 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} */
  protected function getExpectedDocument() {
    
return $element;
  }

  /** * {@inheritdoc} */
  public function buildEntity(array $form, FormStateInterface $form_state) {
    /** @var \Drupal\comment\CommentInterface $comment */
    $comment = parent::buildEntity($form$form_state);
    if (!$form_state->isValueEmpty('date') && $form_state->getValue('date') instanceof DrupalDateTime) {
      $comment->setCreatedTime($form_state->getValue('date')->getTimestamp());
    }
    else {
      $comment->setCreatedTime(REQUEST_TIME);
    }
    // Empty author ID should revert to anonymous.     $author_id = $form_state->getValue('uid');
    if ($comment->id() && $this->currentUser->hasPermission('administer comments')) {
      // Admin can leave the author ID blank to revert to anonymous.       $author_id = $author_id ?: 0;
    }
    if (!is_null($author_id)) {
      
      'created' => 280299600,
      // Drupal 1.0 release.       'changed' => 979534800,
    ];

    Node::create($edit)->save();
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertEquals(280299600, $node->getCreatedTime(), 'Creating a node programmatically uses programmatically set "created" timestamp.');
    $this->assertEquals(979534800, $node->getChangedTime(), 'Creating a node programmatically uses programmatically set "changed" timestamp.');

    // Update the timestamps.     $node->setCreatedTime(979534800);
    $node->changed = 280299600;

    $node->save();
    $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
    $this->assertEquals(979534800, $node->getCreatedTime(), 'Updating a node uses user-set "created" timestamp.');
    // Allowing setting changed timestamps is required, see     // Drupal\content_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp)     // for example.     $this->assertEquals(280299600, $node->getChangedTime(), 'Updating a node uses user-set "changed" timestamp.');
  }

  

  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. */
$time = time();
    foreach ($media_items as $type => $names) {
      foreach ($names as $name) {
        /** @var \Drupal\media\MediaInterface $media */
        $media = Media::create([
          'name' => $name,
          'bundle' => $type,
        ]);
        $source_field = $media->getSource()
          ->getSourceFieldDefinition($media->bundle->entity)
          ->getName();
        $media->set($source_field$name)->setCreatedTime(++$time)->save();
        $created_items[$name] = $media;
      }
    }
    return $created_items;
  }

  /** * Asserts that text appears on page after a wait. * * @param string $text * The text that should appear on the page. * @param int $timeout * Timeout in milliseconds, defaults to 10000. * * @todo replace with whatever gets added in * https://www.drupal.org/node/3061852 */
    if ($entity->getEntityType()->isRevisionable()) {
      $target_translation->setRevisionTranslationAffected(NULL);
    }

    /** @var \Drupal\user\UserInterface $user */
    $user = $this->entityTypeManager()->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);
    $metadata->setSource($source_langcode);
  }

  /** * Builds the translations overview page. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * @param string $entity_type_id * (optional) The entity type ID. * * @return array * Array of page elements to render. */
      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} */
  
'comment_body' => [
        'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
        'format' => 'plain_text',
      ],
      'entity_id' => $commented_entity->id(),
      'entity_type' => 'entity_test',
      'field_name' => 'comment',
    ]);
    $comment->setSubject('Llama')
      ->setOwnerId(static::$auth ? $this->account->id() : 0)
      ->setPublished()
      ->setCreatedTime(123456789)
      ->setChangedTime(123456789);
    $comment->save();

    return $comment;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    $author = User::load($this->entity->getOwnerId());
    
Home | Imprint | This part of the site doesn't use cookies.