setOwnerId example

public static function create(ContainerInterface $container, array $configuration$plugin_id$plugin_definition) {
    return new static($configuration$plugin_id$plugin_definition,
      $container->get('database')
    );
  }

  /** * {@inheritdoc} */
  public function execute($entity = NULL) {
    /** @var \Drupal\node\NodeInterface $entity */
    $entity->setOwnerId($this->configuration['owner_uid'])->save();
  }

  /** * {@inheritdoc} */
  public function defaultConfiguration() {
    return [
      'owner_uid' => '',
    ];
  }

  
'entity_id' => $host->id(),
      'comment_type' => 'entity_test',
      'status' => 1,
    ]);
    $comment_anonymous->save();

    // The entity fields for name and mail have retained their values when     // comment belongs to an anonymous user.     $this->assertNotNull($comment_anonymous->name->value);
    $this->assertNotNull($comment_anonymous->mail->value);

    $comment_anonymous->setOwnerId(1)
      ->save();
    // The entity fields for name and mail have no meaning if the user is not     // Anonymous.     $this->assertNull($comment_anonymous->name->value);
    $this->assertNull($comment_anonymous->mail->value);
  }

}
    $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $field_storage->setTranslatable(TRUE);
    $field_storage->save();
    $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $this->assertTrue($field_storage->isTranslatable(), 'Field body is translatable.');

    // Create a body translation and check the form language.     $langcode2 = $this->langcodes[1];
    $translation = $node->addTranslation($langcode2);
    $translation->title->value = $this->randomString();
    $translation->body->value = $this->randomMachineName(16);
    $translation->setOwnerId($web_user->id());
    $node->save();
    $this->drupalGet($langcode2 . '/node/' . $node->id() . '/edit');
    $form_langcode = \Drupal::state()->get('entity_test.form_langcode');
    $this->assertSame($form_langcode$langcode2, "Node edit form language is $langcode2.");
  }

}
/** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
      $translation = $this->getTranslation($langcode);

      // If no owner has been set explicitly, make the anonymous user the owner.       if (!$translation->getOwner()) {
        $translation->setOwnerId(0);
      }
    }

    // If no revision author has been set explicitly, make the node owner the     // revision author.     if (!$this->getRevisionUser()) {
      $this->setRevisionUserId($this->getOwnerId());
    }
  }

  /** * {@inheritdoc} */

    ])
      ->setLabel('Test file field')
      ->setTranslatable(FALSE);
    $this->field->save();

    // Create an entity that a file can be attached to.     $this->entity = EntityTest::create([
      'name' => 'Llama',
      'type' => 'entity_test',
    ]);
    $this->entity->setOwnerId(isset($this->account) ? $this->account->id() : 0);
    $this->entity->save();

    // Provision entity_test resource.     $this->resourceConfigStorage->create([
      'id' => 'entity.entity_test',
      'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
      'configuration' => [
        'methods' => ['POST'],
        'formats' => [static::$format],
        'authentication' => [static::$auth],
      ],
      
public function testRelationships() {}

  /** * {@inheritdoc} */
  protected function createEntity() {
    // Create an entity that a file can be attached to.     $entity_test = EntityTest::create([
      'name' => 'Llama',
      'type' => 'entity_test',
    ]);
    $entity_test->setOwnerId($this->account->id());
    $entity_test->save();

    return $entity_test;
  }

  /** * Tests using the file upload POST route; needs second request to "use" file. */
  public function testPostFileUpload() {
    $uri = Url::fromUri('base:' . static::$postUri);

    
// 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() {
    $file = File::load(1);
    
break;
    }
  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    $entity_test_label = EntityTestLabel::create([
      'name' => 'label_llama',
    ]);
    $entity_test_label->setOwnerId(0);
    $entity_test_label->save();
    return $entity_test_label;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    $author = User::load(0);
    $normalization = [
      'uuid' => [
        [
->getStorage(static::$entityTypeId)
      ->create([
        'name' => 'Llama',
        'type' => static::$entityTypeId,
        // Set a value for the internal field to confirm that it will not be         // returned in normalization.         // @see entity_test_entity_base_field_info().         'internal_string_field' => [
          'value' => 'This value shall not be internal!',
        ],
      ]);
    $entity_test->setOwnerId(0);
    $entity_test->save();

    return $entity_test;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    $author = User::load(0);
    $normalization = [
      
    // field value changes.     return !$this->get('thumbnail')->entity || $is_new || $this->hasSourceFieldChanged();
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    if (!$this->getOwner()) {
      $this->setOwnerId(0);
    }

    // If no thumbnail has been explicitly set, use the default thumbnail.     if ($this->get('thumbnail')->isEmpty()) {
      $this->thumbnail->target_id = $this->loadThumbnail()->id();
    }
  }

  /** * {@inheritdoc} */
  
// Re-create user.     $this->webUser = $this->drupalCreateUser([
      'access comments',
      'post comments',
      'create article content',
      'edit own comments',
      'node test view',
      'skip comment approval',
    ]);

    // Set the author of the created node to the web_user uid.     $this->node->setOwnerId($this->webUser->id())->save();
  }

  /** * Tests that threaded comments can be viewed. */
  public function testThreadedCommentView() {
    // Set comments to have subject required and preview disabled.     $this->drupalLogin($this->adminUser);
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);
    
$values = [
      $entity_type->getKey('label') => $label,
    ];

    if ($bundle_key = $entity_type->getKey('bundle')) {
      $values[$bundle_key] = $bundle;
    }

    $entity = $this->entityTypeManager->getStorage($entity_type_id)->create($values);

    if ($entity instanceof EntityOwnerInterface) {
      $entity->setOwnerId($uid);
    }

    return $entity;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    return array_filter($entitiesfunction D$entity) {
      $target_bundles = $this->getConfiguration()['target_bundles'];
      
/** * {@inheritdoc} */
  protected function createEntity() {
    $entity = EntityTestMapField::create([
      'name' => 'Llama',
      'type' => 'entity_test_map_field',
      'data' => [
        static::$mapValue,
      ],
    ]);
    $entity->setOwnerId(0);
    $entity->save();
    return $entity;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    $author = User::load(0);
    return [
      'uuid' => [
        [
$this->grantPermissionsToTestedRole(['access content']);

    // Create data.     $user_a = User::create([])->setUsername('A')->activate();
    $user_a->save();
    $user_b = User::create([])->setUsername('B')->set('field_favorite_animal', 'stegosaurus')->block();
    $user_b->save();
    $node_a = Node::create(['type' => 'x'])->setTitle('Owned by A')->setOwner($user_a);
    $node_a->save();
    $node_b = Node::create(['type' => 'x'])->setTitle('Owned by B')->setOwner($user_b);
    $node_b->save();
    $node_anon_1 = Node::create(['type' => 'x'])->setTitle('Owned by anon #1')->setOwnerId(0);
    $node_anon_1->save();
    $node_anon_2 = Node::create(['type' => 'x'])->setTitle('Owned by anon #2')->setOwnerId(0);
    $node_anon_2->save();
    $node_auth_1 = Node::create(['type' => 'x'])->setTitle('Owned by auth #1')->setOwner($this->account);
    $node_auth_1->save();

    $favorite_animal_test_url = Url::fromRoute('jsonapi.user--user.collection')->setOption('query', ['filter[field_favorite_animal]' => 'stegosaurus']);

    // Test.     $collection_url = Url::fromRoute('jsonapi.node--x.collection');
    $request_options = [];
    
// Create a "Llama" comment.     $comment = Comment::create([
      '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} */
  
Home | Imprint | This part of the site doesn't use cookies.