reloadEntity example

$this->entityTypeManager = $this->container->get('entity_type.manager');
  }

  /** * Tests basic monolingual content moderation through the API. * * @dataProvider basicModerationTestCases */
  public function testBasicModeration($entity_type_id) {
    $entity = $this->createEntity($entity_type_id, 'draft');
    $entity = $this->reloadEntity($entity);
    $this->assertEquals('draft', $entity->moderation_state->value);

    $entity->moderation_state->value = 'published';
    $entity->save();

    $entity = $this->reloadEntity($entity);
    $this->assertEquals('published', $entity->moderation_state->value);

    // Change the state without saving the node.     $content_moderation_state = ContentModerationState::load(1);
    $content_moderation_state->set('moderation_state', 'draft');
    
$entity->save();
    $old_rev_id = $entity->getRevisionId();

    $translation = $entity->addTranslation('de');
    $translation->setNewRevision();
    $translation->save();

    // Verify that the saved translation for the new translation has a newer     // revision ID.     $this->assertGreaterThan($old_rev_id$translation->getRevisionId());
    // Verify that the entity from the storage has a newer revision ID.     $this->assertGreaterThan($old_rev_id$this->reloadEntity($entity)->getRevisionId());
  }

  /** * Tests if the translation object has the right revision id after new revision. */
  public function testRevertRevisionAfterTranslation() {
    $user = $this->createUser();
    $storage = $this->entityTypeManager->getStorage('entity_test_mulrev');

    // Create a test entity.     $entity = EntityTestMulRev::create([
      


  /** * Installs the Layout Builder. * * Also configures and reloads the entity display. */
  protected function installLayoutBuilder() {
    $this->container->get('module_installer')->install(['layout_builder']);
    $this->refreshServices();

    $this->display = $this->reloadEntity($this->display);
    $this->display->enableLayoutBuilder()->save();
    $this->entity = $this->reloadEntity($this->entity);
  }

  /** * Enables overrides for the display and reloads the entity. */
  protected function enableOverrides() {
    $this->display->setOverridable()->save();
    $this->entity = $this->reloadEntity($this->entity);
  }

  
$entity->langcode->value = $langcode;
    $entity->save();
    $this->assertFieldStorageLangcode($entity, 'Field language successfully changed.');
    $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    $entity->langcode->value = $langcode;
    $entity->save();
    $this->assertFieldStorageLangcode($entity, 'Field language successfully changed to language neutral.');

    // Test that after switching field translatability things keep working as     // before.     $this->toggleFieldTranslatability($entity_type$entity_type);
    $entity = $this->reloadEntity($entity);
    foreach ([$this->fieldName, $this->untranslatableFieldName] as $field_name) {
      $this->assertEquals($values[$field_name]$entity->get($field_name)->value, 'Field language works as expected after switching translatability.');
    }

    // Test that after disabling field translatability translated values are not     // loaded.     $this->toggleFieldTranslatability($entity_type$entity_type);
    $entity = $this->reloadEntity($entity);
    $entity->langcode->value = $this->langcodes[0];
    $translation = $entity->addTranslation($this->langcodes[1]);
    $translated_value = $this->randomMachineName();
    
'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
    ];
    $this->assertFieldAttributes($this->entity, $expected_fields);

    $this->installLayoutBuilder();

    // Without using Layout Builder for an override, the result has not changed.     $this->assertFieldAttributes($this->entity, $expected_fields);

    // Add a layout override.     $this->enableOverrides();
    $this->entity = $this->reloadEntity($this->entity);
    $this->entity->get(OverridesSectionStorage::FIELD_NAME)->appendSection(new Section('layout_onecol'));
    $this->entity->save();

    // The rendered entity has now changed. The non-configurable field is shown     // outside the layout, the configurable field is not shown at all, and the     // layout itself is rendered (but empty).     $new_expected_fields = [
      'field field--name-name field--type-string field--label-hidden field__item',
      'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
      'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
    ];
    
'name' => $this->randomString(),
      'user_id' => $user->id(),
      'language' => 'en',
    ]);

    $entity->addTranslation('de');
    $entity->save();
    $fields = array_keys($entity->getFieldDefinitions());

    // Reload the entity, clone it and check that both entity objects reference     // different field instances.     $entity = $this->reloadEntity($entity);
    $clone = clone $entity;

    $different_references = TRUE;
    foreach ($fields as $field_name) {
      if ($entity->get($field_name) === $clone->get($field_name)) {
        $different_references = FALSE;
      }
    }
    $this->assertTrue($different_references, 'The entity object and the cloned entity object reference different field item list objects.');

    // Reload the entity, initialize one translation, clone it and check that
$this->assertEquals($different_bundle_node->id()$referencing_entity->field_test[2]->target_id);
    $this->assertEquals($deleted_node->id()$referencing_entity->field_test[3]->target_id);

    $violations = $referencing_entity->field_test->validate();
    $this->assertCount(0, $violations);

    // Remove one of the referenceable bundles and check that a pre-existing node     // of that bundle can not be referenced anymore.     $field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test');
    $field->setSetting('handler_settings', ['target_bundles' => ['article']]);
    $field->save();
    $referencing_entity = $this->reloadEntity($referencing_entity);

    $violations = $referencing_entity->field_test->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals(t('This entity (%type: %id) cannot be referenced.', [
      '%type' => 'node',
      '%id' => $different_bundle_node->id(),
    ])$violations[0]->getMessage());

    // Delete the last node and check that the pre-existing reference is not     // valid anymore.     $deleted_node->delete();

    
$translation->{$default_langcode_key}->value = !$default;
        $this->fail('The default translation flag cannot be changed.');
      }
      catch (\LogicException $e) {
        // Expected exception; just continue testing.       }

      $this->assertEquals($default$translation->{$default_langcode_key}->value);
    }

    // Check that after loading an entity the language is the default one.     $entity = $this->reloadEntity($entity);
    $this->assertEquals($default_langcode$entity->language()->getId(), 'The loaded entity is the original one.');

    // Add another translation and check that everything works as expected. A     // new translation object can be obtained also by just specifying a valid     // language.     $langcode2 = $this->langcodes[2];
    $translation = $entity->addTranslation($langcode2);
    $value = $entity !== $translation && $translation->language()->getId() == $langcode2 && $entity->hasTranslation($langcode2);
    $this->assertTrue($value, 'A new translation object can be obtained also by specifying a valid language.');
    $this->assertEquals($default_langcode$entity->language()->getId(), 'The original language has been preserved.');
    $translation->save();
    

  protected function assertSavedFieldItemValue(EntityTest $entity, string $expected_value): void {
    $entity->setNewRevision(TRUE);
    $entity->save();
    $base_field_expected_value = str_replace($this->fieldName, 'field_test_item', $expected_value);
    $this->assertEquals($base_field_expected_value$entity->field_test_item->value);
    $this->assertEquals($expected_value$entity->{$this->fieldName}->value);
    $entity = $this->reloadEntity($entity);
    $this->assertEquals($base_field_expected_value$entity->field_test_item->value);
    $this->assertEquals($expected_value$entity->{$this->fieldName}->value);
  }

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