createEntity example

case 'PATCH':
      case 'DELETE':
        $this->grantPermissionsToTestedRole(['use editorial transition publish', 'use editorial transition create_new_draft']);
        break;
    }
  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    $entity = parent::createEntity();
    if (!$this->workflow) {
      $this->workflow = $this->createEditorialWorkflow();
    }
    $this->workflow->getTypePlugin()->addEntityTypeAndBundle($entity->getEntityTypeId()$entity->bundle());
    $this->workflow->save();

    return $entity;
  }

  /** * {@inheritdoc} */
    $this->drupalGet('admin/structure/types/manage/article/fields');
    $this->drupalGet('admin/structure/types/manage/article/fields/node.article.' . $this->fieldName . '/delete');
    $this->submitForm([], 'Delete');
    $this->drupalGet('admin/structure/types/manage/article/fields/node.article.field_tags/delete');
    $this->submitForm([], 'Delete');
    $this->drupalGet('admin/structure/types/manage/article/fields/node.article.field_image/delete');
    $this->submitForm([], 'Delete');

    // Add a node.     $default_langcode = $this->langcodes[0];
    $values[$default_langcode] = ['title' => [['value' => $this->randomMachineName()]]];
    $this->entityId = $this->createEntity($values[$default_langcode]$default_langcode);
    $storage = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId);
    $storage->resetCache([$this->entityId]);
    $entity = $storage->load($this->entityId);

    // Add a content translation.     $langcode = 'fr';
    $language = ConfigurableLanguage::load($langcode);
    $values[$langcode] = ['title' => [['value' => $this->randomMachineName()]]];

    $entity_type_id = $entity->getEntityTypeId();
    

  public function testGetCurrentUserId() {
    // This test does not assert anything that can be workspace-specific.     $this->markTestSkipped();
  }

  /** * {@inheritdoc} */
  protected function createEntity($entity_type_id$moderation_state = 'published', $create_workflow = TRUE) {
    $entity = $this->workspaceManager->executeOutsideWorkspace(function D) use ($entity_type_id$moderation_state$create_workflow) {
      return parent::createEntity($entity_type_id$moderation_state$create_workflow);
    });

    return $entity;
  }

  /** * {@inheritdoc} */
  protected function createEditorialWorkflow() {
    $workflow = $this->workspaceManager->executeOutsideWorkspace(function D) {
      return $this->traitCreateEditorialWorkflow();
    });
// Turn off translatability for the metadata fields on the current bundle.     $metadata_fields = ['created', 'changed', 'uid', 'status'];
    foreach ($metadata_fields as $field_name) {
      $fields[$field_name]
        ->getConfig($this->bundle)
        ->setTranslatable(FALSE)
        ->save();
    }

    // Create a new test entity with original values in the default language.     $default_langcode = $this->langcodes[0];
    $entity_id = $this->createEntity(['title' => $this->randomString()]$default_langcode);
    $storage = \Drupal::entityTypeManager()->getStorage($this->entityTypeId);
    $storage->resetCache();
    $entity = $storage->load($entity_id);

    // Add a content translation.     $langcode = 'it';
    $values = $entity->toArray();
    // Apply a default value for the metadata fields.     foreach ($metadata_fields as $field_name) {
      unset($values[$field_name]);
    }
    
/** * {@inheritdoc} */
  protected static $modules = ['layout_builder'];

  /** * {@inheritdoc} */
  protected function createEntity() {
    /** @var \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay $entity */
    $entity = parent::createEntity();
    $entity
      ->enableLayoutBuilder()
      ->setOverridable()
      ->save();
    $this->assertCount(1, $entity->getThirdPartySetting('layout_builder', 'sections'));
    return $entity;
  }

  /** * {@inheritdoc} */
  
$entity->setSalesChannelDomainId($dataBag->get('salesChannelDomainId'));
        $entity->setCurrencyId($dataBag->get('currencyId'));

        return $entity;
    }

    private function generateExportPreview(RequestDataBag $dataBag, Context $context): ?ProductExportResult
    {
        $salesChannelDomain = $this->getSalesChannelDomain($dataBag->get('salesChannelDomainId')$context);
        $salesChannel = $this->getSalesChannel($dataBag->get('salesChannelId')$context);

        $productExportEntity = $this->createEntity($dataBag);
        $productExportEntity->setSalesChannelDomain($salesChannelDomain);
        $productExportEntity->setStorefrontSalesChannelId($salesChannelDomain->getSalesChannelId());
        $productExportEntity->setSalesChannel($salesChannel);

        $exportBehavior = new ExportBehavior(true, true, true);

        return $this->productExportGenerator->generate($productExportEntity$exportBehavior);
    }

    private function getSalesChannelDomain(string $salesChannelDomainId, Context $context): SalesChannelDomainEntity
    {
        

  protected function setupEntity(UserInterface $user = NULL) {
    $default_langcode = $this->langcodes[0];

    // Create a test entity.     $user = $user ?: $this->drupalCreateUser();
    $values = [
      'name' => $this->randomMachineName(),
      'user_id' => $user->id(),
      $this->fieldName => [['value' => $this->randomMachineName(16)]],
    ];
    $id = $this->createEntity($values$default_langcode);
    $storage = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId);

    // Create a translation that is not published to test view access.     $this->drupalLogin($this->translator);
    $add_translation_url = Url::fromRoute("entity.$this->entityTypeId.content_translation_add", [$this->entityTypeId => $id, 'source' => $default_langcode, 'target' => $this->langcodes[2]]);
    $edit = [
      'name[0][value]' => 'translation name',
      'content_translation[status]' => FALSE,
    ];
    $this->drupalGet($add_translation_url);
    
$this->drupalGet($url);
        $this->assertSession()->pageTextContains("{$entity->getTranslation($langcode)->label()} [{$languages[$langcode]->getName()} translation]");
      }
    }
  }

  /** * Tests translated user deletion. */
  public function testTranslatedUserDeletion() {
    $this->drupalLogin($this->administrator);
    $entity_id = $this->createEntity($this->getNewEntityValues('en'), 'en');

    $entity = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId)
      ->load($entity_id);
    $translated_entity = $entity->addTranslation('fr');
    $translated_entity->save();

    $url = $entity->toUrl(
      'edit-form',
      ['language' => $this->container->get('language_manager')->getLanguage('en')]
    );
    


  /** * Tests translate link on vocabulary term list. */
  public function testTranslateLinkVocabularyAdminPage() {
    $this->drupalLogin($this->drupalCreateUser(array_merge(parent::getTranslatorPermissions()['access administration pages', 'administer taxonomy'])));

    $values = [
      'name' => $this->randomMachineName(),
    ];
    $translatable_tid = $this->createEntity($values$this->langcodes[0]$this->vocabulary->id());

    // Create an untranslatable vocabulary.     $untranslatable_vocabulary = Vocabulary::create([
      'name' => 'untranslatable_voc',
      'description' => $this->randomMachineName(),
      'vid' => 'untranslatable_voc',
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'weight' => mt_rand(0, 10),
    ]);
    $untranslatable_vocabulary->save();

    

  public function doTestOverview($index) {
    $this->drupalLogin($this->currentAccount);

    // Create a test node.     $values = [
      'title' => "Test $index.1 EN",
      'moderation_state' => 'published',
    ];
    $id = $this->createEntity($values, 'en');
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->storage->load($id);

    // Add a draft translation and check that it is available only in the latest     // revision.     $add_translation_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_add",
      [
        $entity->getEntityTypeId() => $id,
        'source' => 'en',
        'target' => 'it',
      ],
      [
    ConfigurableLanguage::createFromLangcode('fr')->save();

    $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);
    
'cardinality' => 1,
        'translatable' => FALSE,
      ])->save();
      FieldConfig::create([
        'entity_type' => 'entity_test',
        'field_name' => 'field_test_internal',
        'bundle' => 'entity_test',
        'label' => 'Test field with internal and non-internal properties',
      ])->save();
    }

    $entity = parent::createEntity();
    $entity->field_test_internal = [
      'value' => 'This value shall not be internal!',
    ];
    $entity->save();
    return $entity;
  }

  /** * {@inheritdoc} */
  protected function getNormalizedPostEntity() {
    

  public function testEntityCreation() {
    // Create a cache entry that is tagged with a shortcut set cache tag.     $cache_tags = ['config:shortcut.set.default'];
    \Drupal::cache('render')->set('foo', 'bar', CacheBackendInterface::CACHE_PERMANENT, $cache_tags);

    // Verify a cache hit.     $this->verifyRenderCache('foo', $cache_tags);

    // Now create a shortcut entity in that shortcut set.     $this->createEntity();

    // Verify a cache miss.     $this->assertFalse(\Drupal::cache('render')->get('foo'), 'Creating a new shortcut invalidates the cache tag of the shortcut set.');
  }

  /** * Tests visibility and cacheability of shortcuts in the toolbar. */
  public function testToolbar() {
    $this->drupalPlaceBlock('page_title_block', ['id' => 'title']);

    

  protected function getTranslatorPermissions() {
    return array_merge(parent::getTranslatorPermissions()['access shortcuts', 'administer shortcuts', 'access toolbar']);
  }

  /** * {@inheritdoc} */
  protected function createEntity($values$langcode$bundle_name = NULL) {
    $values['link']['uri'] = 'internal:/user';
    return parent::createEntity($values$langcode$bundle_name);
  }

  /** * {@inheritdoc} */
  protected function getNewEntityValues($langcode) {
    return ['title' => [['value' => $this->randomMachineName()]]] + parent::getNewEntityValues($langcode);
  }

  protected function doTestBasicTranslation() {
    parent::doTestBasicTranslation();

    

  protected function setUp(): void {
    parent::setUp();

    // Calculate REST Resource config entity ID.     static::$resourceConfigId = 'entity.' . static::$entityTypeId;

    $this->entityStorage = $this->container->get('entity_type.manager')
      ->getStorage(static::$entityTypeId);

    // Create an entity.     $this->entity = $this->createEntity();

    if ($this->entity instanceof FieldableEntityInterface) {
      // Add access-protected field.       FieldStorageConfig::create([
        'entity_type' => static::$entityTypeId,
        'field_name' => 'field_rest_test',
        'type' => 'text',
      ])
        ->setCardinality(1)
        ->save();
      FieldConfig::create([
        
Home | Imprint | This part of the site doesn't use cookies.