createWithSampleValues example

      $key = $field_definition->getTargetEntityTypeId() . ':' . $options['target_type'] . ':' . $bundle;

      // If entity generation was attempted but did not finish, do not continue.       if (isset($recursion_tracker[$key])) {
        return [];
      }

      // Mark this as an attempt at generation.       $recursion_tracker[$key] = TRUE;

      // Mark the sample entity as being a preview.       $values['entity'] = $entity_storage->createWithSampleValues($bundle['in_preview' => TRUE]);

      // Remove the indicator once the entity is successfully generated.       unset($recursion_tracker[$key]);
      return $values;
    }
  }

  /** * Gets a bundle for a given entity type and selection options. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * @param array $selection_settings * An array of selection settings. * * @return string|null * Either the bundle string, or NULL if there is no bundle. */
'mode' => 'default',
      'status' => TRUE,
    ]);
    $this->display
      ->setComponent('test_field_display_configurable', ['weight' => 5])
      ->save();

    // Create an entity with fields that are configurable and non-configurable.     $entity_storage = $this->container->get('entity_type.manager')->getStorage('entity_test_base_field_display');
    // @todo Remove langcode workarounds after resolving     // https://www.drupal.org/node/2915034.     $this->entity = $entity_storage->createWithSampleValues('entity_test_base_field_display', [
      'langcode' => 'en',
      'langcode_default' => TRUE,
    ]);
    $this->entity->save();
  }

  /** * Installs the Layout Builder. * * Also configures and reloads the entity display. */
  
public function get($entity_type_id$bundle_id) {
    $tempstore = $this->tempStoreFactory->get('layout_builder.sample_entity');
    if ($entity = $tempstore->get("$entity_type_id.$bundle_id")) {
      return $entity;
    }

    $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
    if (!$entity_storage instanceof ContentEntityStorageInterface) {
      throw new \InvalidArgumentException(sprintf('The "%s" entity storage is not supported', $entity_type_id));
    }

    $entity = $entity_storage->createWithSampleValues($bundle_id);
    // Mark the sample entity as being a preview.     $entity->in_preview = TRUE;
    $tempstore->set("$entity_type_id.$bundle_id", $entity);
    return $entity;
  }

  /** * {@inheritdoc} */
  public function delete($entity_type_id$bundle_id) {
    $tempstore = $this->tempStoreFactory->get('layout_builder.sample_entity');
    
$this->entityValidateAndSave($entity);
  }

  /** * Tests the ::generateSampleValue() method when it has a circular reference. */
  public function testGenerateSampleValueCircularReference() {
    // Delete the existing entity.     $this->entityStringId->delete();

    $entity_storage = \Drupal::entityTypeManager()->getStorage('entity_test');
    $entity = $entity_storage->createWithSampleValues('entity_test');
    $this->assertInstanceOf(EntityTestStringId::class$entity->field_test_entity_test_string_id->entity);
    $this->assertInstanceOf(EntityTest::class$entity->field_test_entity_test_string_id->entity->field_test_entity_test->entity);
  }

  /** * Tests referencing content entities with string IDs. */
  public function testContentEntityReferenceItemWithStringId() {
    $entity = EntityTest::create();
    $entity->field_test_entity_test_string_id->target_id = $this->entityStringId->id();
    $entity->save();
    
$entity = $this->prophesize(ContentEntityInterface::class)->willImplement(\IteratorAggregate::class);
      $entity->getEntityTypeId()->willReturn('test_content');
      $entity->getIterator()->willReturn(new \ArrayIterator([]));
      $entity->bundle()->willReturn($bundle);
      $content_entity_storage->create(['the_bundle_key' => $bundle])
        ->willReturn($entity->reveal())
        ->shouldBeCalled();
    }

    // Creating entities with sample values can lead to performance issues when     // called many times. Ensure that createWithSampleValues() is not called.     $content_entity_storage->createWithSampleValues(Argument::any())->shouldNotBeCalled();

    $entity_type = new EntityType([
      'id' => 'test_content',
      'entity_keys' => [
        'bundle' => 'the_bundle_key',
      ],
    ]);

    $this->entityTypeManager->getStorage('test_content')->willReturn($content_entity_storage->reveal());

    $this->entityTypeManager->getDefinition('test_content')->willReturn($entity_type);
    

  public function testModerationStateSampleValues() {
    $this->container->get('current_user')->setAccount(
      $this->createUser([
        'use editorial transition create_new_draft',
        'use editorial transition publish',
      ])
    );
    $sample = $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->createWithSampleValues('example');
    $this->assertCount(0, $sample->validate());
    $this->assertEquals('draft', $sample->moderation_state->value);
  }

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

  public function testDisallowedEntityCreateInNonDefaultWorkspace($entity_type_id$allowed) {
    $this->initializeWorkspacesModule();
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager->getStorage($entity_type_id);

    // Switch to a non-default workspace and check whether creating a new entity     // is allowed.     $this->switchToWorkspace('stage');

    $entity = $storage->createWithSampleValues($entity_type_id);
    if ($entity_type_id === 'workspace') {
      $entity->id = 'test';
    }

    if (!$allowed) {
      $this->expectException(EntityStorageException::class);
      $this->expectExceptionMessage('This entity can only be saved in the default workspace.');
    }
    $entity->save();
  }

  
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;

/** * Defines the storage handler class for path_alias entities. */
class PathAliasStorage extends SqlContentEntityStorage {

  /** * {@inheritdoc} */
  public function createWithSampleValues($bundle = FALSE, array $values = []) {
    $entity = parent::createWithSampleValues($bundle['path' => '/<front>'] + $values);
    // Ensure the alias is only 255 characters long.     $entity->set('alias', substr('/' . $entity->get('alias')->value, 0, 255));
    return $entity;
  }

}
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $definition) {
      if ($definition->entityClassImplements(FieldableEntityInterface::class)) {
        $label = $definition->getKey('label');
        $values = [];
        if ($label) {
          $title = $this->randomString();
          $values[$label] = $title;
        }
        // Create sample entities with bundles.         if ($bundle_type = $definition->getBundleEntityType()) {
          foreach ($this->entityTypeManager->getStorage($bundle_type)->loadMultiple() as $bundle) {
            $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues($bundle->id()$values);
            $violations = $entity->validate();
            $this->assertCount(0, $violations);
            if ($label) {
              $this->assertEquals($title$entity->label());
            }
          }
        }
        // Create sample entities without bundles.         else {
          $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues(FALSE, $values);
          $violations = $entity->validate();
          
Home | Imprint | This part of the site doesn't use cookies.