You are here

protected function ContentTranslationTestBase::createEntity in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/content_translation/tests/src/Functional/ContentTranslationTestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationTestBase::createEntity()
  2. 9 core/modules/content_translation/tests/src/Functional/ContentTranslationTestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationTestBase::createEntity()

Creates the entity to be translated.

Parameters

array $values: An array of initial values for the entity.

string $langcode: The initial language code of the entity.

string $bundle_name: (optional) The entity bundle, if the entity uses bundles. Defaults to NULL. If left NULL, $this->bundle will be used.

Return value

string The entity id.

File

core/modules/content_translation/tests/src/Functional/ContentTranslationTestBase.php, line 216

Class

ContentTranslationTestBase
Base class for content translation tests.

Namespace

Drupal\Tests\content_translation\Functional

Code

protected function createEntity($values, $langcode, $bundle_name = NULL) {
  $entity_values = $values;
  $entity_values['langcode'] = $langcode;
  $entity_type = \Drupal::entityTypeManager()
    ->getDefinition($this->entityTypeId);
  if ($bundle_key = $entity_type
    ->getKey('bundle')) {
    $entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
  }
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityTypeId);
  if (!$storage instanceof SqlContentEntityStorage) {
    foreach ($values as $property => $value) {
      if (is_array($value)) {
        $entity_values[$property] = [
          $langcode => $value,
        ];
      }
    }
  }
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityTypeId)
    ->create($entity_values);
  $entity
    ->save();
  return $entity
    ->id();
}