entitySaveReload example

    FieldStorageConfig::create($field_storage_definition)->save();
    FieldConfig::create($field_definition)->save();
    $field_storage = FieldStorageConfig::load('entity_test.' . $field_storage_definition['field_name']);
    $this->assertFalse($field_storage->isDeleted());
    $field = FieldConfig::load('entity_test.' . $field_definition['bundle'] . '.' . $field_definition['field_name']);
    $this->assertFalse($field->isDeleted());

    // Save an entity with data for the field     $entity = EntityTest::create();
    $values[0]['value'] = mt_rand(1, 127);
    $entity->{$field_storage->getName()}->value = $values[0]['value'];
    $entity = $this->entitySaveReload($entity);

    // Verify the field is present on load     $this->assertCount(1, $entity->{$field_storage->getName()}, "Data in previously deleted field saves and loads correctly");
    foreach ($values as $delta => $value) {
      $this->assertEquals($values[$delta]['value']$entity->{$field_storage->getName()}[$delta]->value, "Data in previously deleted field saves and loads correctly");
    }
  }

  public function testUpdateFieldType() {
    $field_storage = FieldStorageConfig::create([
      'field_name' => 'field_type',
      
$entity_type = 'entity_test';
    $this->createFieldWithStorage('', $entity_type);

    $entity_init = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create(['id' => 1]);

    // Insert: Field is NULL.     $entity = clone $entity_init;
    $entity->{$this->fieldTestData->field_name} = NULL;
    $entity->enforceIsNew();
    $entity = $this->entitySaveReload($entity);
    $this->assertTrue($entity->{$this->fieldTestData->field_name}->isEmpty(), 'Insert: NULL field results in no value saved');

    // All saves after this point should be updates, not inserts.     $entity_init->enforceIsNew(FALSE);

    // Add some real data.     $entity = clone($entity_init);
    $values = $this->_generateTestFieldValues(1);
    $entity->{$this->fieldTestData->field_name} = $values;
    $entity = $this->entitySaveReload($entity);
    $this->assertEquals($values$entity->{$this->fieldTestData->field_name}->getValue(), 'Field data saved');

    
// Non-cacheable entity type.     $entity_type = 'entity_test';
    $cid = "values:$entity_type:" . $entity_init->id();

    // Check that no initial cache entry is present.     $this->assertFalse(\Drupal::cache('entity')->get($cid), 'Non-cached: no initial cache entry');

    // Save, and check that no cache entry is present.     $entity = clone($entity_init);
    $entity->{$this->fieldTestData->field_name}->setValue($values);
    $entity = $this->entitySaveReload($entity);
    $cid = "values:$entity_type:" . $entity->id();
    $this->assertFalse(\Drupal::cache('entity')->get($cid), 'Non-cached: no cache entry on insert and load');

    // Cacheable entity type.     $entity_type = 'entity_test_rev';
    $this->createFieldWithStorage('_2', $entity_type);

    $entity_init = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create([
        'type' => $entity_type,
      ]);
->create(['type' => $this->field->getTargetBundle()]);
    $field_translations = [];
    $available_langcodes = array_keys($this->container->get('language_manager')->getLanguages());
    $entity->langcode->value = reset($available_langcodes);
    foreach ($available_langcodes as $langcode) {
      $field_translations[$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
      $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
      $translation->{$this->fieldName}->setValue($field_translations[$langcode]);
    }

    // Save and reload the field translations.     $entity = $this->entitySaveReload($entity);

    // Check if the correct values were saved/loaded.     foreach ($field_translations as $langcode => $items) {
      $result = TRUE;
      foreach ($items as $delta => $item) {
        $result = $result && $item['value'] == $entity->getTranslation($langcode)->{$this->fieldName}[$delta]->value;
      }
      $this->assertTrue($resultnew FormattableMarkup('%language translation correctly handled.', ['%language' => $langcode]));
    }

    // Test default values.
Home | Imprint | This part of the site doesn't use cookies.